HITL and SITL for crazyflie

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

HITL and SITL for crazyflie

Post by wuwushrek »

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: 2538
Joined: Tue Feb 06, 2007 12:36 pm

Re: HITL and SITL for crazyflie

Post by arnaud »

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: 15
Joined: Mon Apr 16, 2018 1:05 pm

Re: HITL and SITL for crazyflie

Post by wuwushrek »

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: 2538
Joined: Tue Feb 06, 2007 12:36 pm

Re: HITL and SITL for crazyflie

Post by arnaud »

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: 15
Joined: Mon Apr 16, 2018 1:05 pm

Re: HITL and SITL for crazyflie

Post by wuwushrek »

Awesome !

Yes indeed I was mixing up high and very low level. Thank you for the clarifications :)
wuwushrek
Beginner
Posts: 15
Joined: Mon Apr 16, 2018 1:05 pm

Re: HITL and SITL for crazyflie

Post by wuwushrek »

Hi arnaud,

Few weeks ago I finally got the SITL and the HITL version to work with Gazebo 7/8. At the moment HITL only uses USB cable for the simulation because of the data rate which can't be support by the Radio. Well SITL uses socket(as it is usually done) and a portable version of FReeRTOS for Linux, so it is possible to launch the SITL instance and Gazebo on different computers. I haven't tested yet the swarm scenarios in simulation but there is no reason why it shouldn't working. I have made my modification from your master branch from 2 weeks ago. Basically, except from some #ifdef that I added in order to hide low level include and dependencies (only for SITL) there is no other modifications except 4-5 files I created.

If you are still interested and have time, I can create some kind of test branch or give you my version in order for you to test it. Some improvements may be required, but so far I hadn't have any problems with my tests.
arnaud
Bitcraze
Posts: 2538
Joined: Tue Feb 06, 2007 12:36 pm

Re: HITL and SITL for crazyflie

Post by arnaud »

Hi,

Thanks for updating us, this sound great! I am definitely interested in testing it if you could push it somewhere. This is something that would be very nice to merge into the master branch eventually.
nemaproblema
Beginner
Posts: 3
Joined: Thu May 31, 2018 9:55 am

Re: HITL and SITL for crazyflie

Post by nemaproblema »

Hi all,

wuwushrek we also need sitl and hitl for our experiments,
so could you please check in to a public branch and post it here
so we could test both branch and our controllers?
tugayalperen
Beginner
Posts: 19
Joined: Mon May 14, 2018 8:33 am

Re: HITL and SITL for crazyflie

Post by tugayalperen »

Looks like there are other that are waiting to test your interesting work :D We will also be very grateful if we can use it..
wuwushrek
Beginner
Posts: 15
Joined: Mon Apr 16, 2018 1:05 pm

Re: HITL and SITL for crazyflie

Post by wuwushrek »

Ok great to know I wasn't the only one who needed it. Sorry for the delay, I will push everything from the gitlab group to my github at the end of the week-end and put the link here.
Regards
Post Reply