HITL and SITL for crazyflie

Discussions about all things Bitcraze
Post Reply
wuwushrek
Beginner
Posts: 3
Joined: Mon Apr 16, 2018 1:05 pm

HITL and SITL for crazyflie

Post by wuwushrek » Mon Apr 16, 2018 1:46 pm

Hi,
After some experiences with the PX4 autopilot, I recently start working with crazyflie 2.0. One of my goal is to implement embedded online verification tool for this board. What I am working on is to formally prove correctness of low level control algorithm and some high level planning decision.
Thus I will want to simulate all the algorithms and modifications embedded before testing it on real quad. I decided to implement support for HITL and SITL for crazyflie 2.0. For the moment, I have a gazebo model for crazyflie 2.0 based on [https://github.com/bitcraze/bitcraze-me ... _model.skp] and where all the parameters (inertia, motor params , drag coefficients) are taken [http://mikehamer.info/assets/papers/Cra ... elling.pdf] and the rotor simulation is achieved thanks to [https://github.com/ethz-asl/rotors_simulator].

My goal is to achieve that with minimum modif on the crazyflie software. My questions are :
1) Since IMU , barometer, LPS will come from gazebo ( or whatever), I guess that using the Radio link will not fit for the high rate at which I will send data to the board ? In that case how can I achieve the communication over the micro usb on the stm32F4 board ? I plan to continue working with the CRTP for com
2) I guess I will need just to duplicate [https://github.com/bitcraze/crazyflie-f ... sors_cf2.c] (for both HITL and SITL) in order to receive information from the micro usb link ? Will this be sufficient ? How does the micro usb link is able to communicate with the sensor module ?

Thanks in advance

arnaud
Bitcraze
Posts: 1592
Joined: Tue Feb 06, 2007 12:36 pm

Re: HITL and SITL for crazyflie

Post by arnaud » Tue Apr 17, 2018 6:58 am

Hi and welcome!

1) You can communicate with the Crazyflie using CRTP over USB, this is supported by the Crazyflie firmware and python lib. All you should need is to connect Crazyflie via usb and connect to "usb://0" instead of "radio://0/channel/datarate".
2) Yes I think you can just create a file name sensors_hitl.c that implements all sensor functions and gets sensor values from a CRTP port. The firmware is already setup to compile different sensors (for instance the bosch sensors driver can be compiled instead of the stock one) so you should be able to add "SENSORS=hitl" to tools/make/config.mk and your virtual sensors will be compiled instead of the cf2 stock sensors.

The easiest would be to communicate via CRTP. CRTP routes packet using ports and you can setup a callback that would be called when a packet arrives for your port. For a simple example you can look at the localization service: https://github.com/bitcraze/crazyflie-f ... vice.c#L91. Just pick a free port (it is a 4bit number from 0 to 15).

Just a note: it is possible that the current USB implementation yields lower performance than the max allowed by USB full speed, it should still be much better than the radio but if you hit performance problems it is likely possible to improve the USB bandwidth.

What you are doing is very interesting, please keep us updated :-).

wuwushrek
Beginner
Posts: 3
Joined: Mon Apr 16, 2018 1:05 pm

Re: HITL and SITL for crazyflie

Post by wuwushrek » Sat Apr 21, 2018 7:14 pm

Thank you so much for the fast reply and the tips.
I'm so close to the goal now. I will update you with the results.

Two more questions :
1) https://github.com/bitcraze/crazyflie-f ... rtp.c#L181 , is this function blocking ? If not , when there is no data, the task automatically sleep for 10ms thus the RX and TX tasks have (let say) a frequency of 100Hz right ? This can be a problem for sending IMU at high frequency...
2) Just to be sure, the crtp_localization_service handles external position like (simulated) LPS right ? I don't see any reply from the task to the radio dongle. However it is state here : https://wiki.bitcraze.io/doc:crazyradio:usb:index that the radio dongle wait automatically for and acknowledge. Where am I wrong ?

Thanks in advance

arnaud
Bitcraze
Posts: 1592
Joined: Tue Feb 06, 2007 12:36 pm

Re: HITL and SITL for crazyflie

Post by arnaud » Mon Apr 23, 2018 11:28 am

Hi,

1) When used with the radio this function will timeout after 100ms, USB will have similar timeout but if there is a packet it will returns the packet directly so it does not cause any latency problem. The reason there is a timeout at all is to handle switching link at runtime. The 10ms delay in this loop is only executed when no link is selected which is not the case if you have an active connection.
2) crtp_localization_service is a collection of localization-related services, it includes sending position to the Crazyflie (ex. from a MOCAP) and things like sending back a stream of LPS measurement back to the PC. As for the communication, I think you are mixing up high and very low level. You can send a packet back to the ground with the high-level crtpSendPacket function. At low level Crazyradio is in PTX mode and Crazyflie in PRX mode in the terminology used by the radio chip. This means that Crazyradio sends packet (our data upstream) and Crazyflie sends back ACK with payload (our data downstream). The crazyflie lib takes care of sending enough packets to implement a continious stream of data up and down so you should not have to worry about that at CRTP level. The same is true for the USB link, you can expect to have a continuous stream of CTRP packet up and down.

wuwushrek
Beginner
Posts: 3
Joined: Mon Apr 16, 2018 1:05 pm

Re: HITL and SITL for crazyflie

Post by wuwushrek » Mon Apr 23, 2018 11:36 am

Awesome !

Yes indeed I was mixing up high and very low level. Thank you for the clarifications :)

Post Reply