Crazyradio with Matlab

Firmware/software/electronics
Post Reply
t_walker_21
Beginner
Posts: 17
Joined: Wed Feb 11, 2015 7:39 pm

Crazyradio with Matlab

Post by t_walker_21 » Wed Feb 11, 2015 9:31 pm

Hello All,

I am attempting to communicate to the Crazyradio with MATLAB, and I am trying find a way that I can interface between the two. Is there any way I can configure the radio dongle as a serial port or is there any way I can setup the dongle such that MATLAB will recognize and be able to communicate with it?
I have the CF client installed within the Virtual Machine, AND I have the IOS app used to control the Quad installed on my IPhone. However; I need to establish a means of controlling the Quad through Matlab. So, any information on how I can get MatLab to recognize the USB dongle would be extremely helpful.


Thanks

poizone
Member
Posts: 59
Joined: Thu Feb 05, 2015 3:59 am
Location: Ohio, USA

Re: Crazyradio with Matlab

Post by poizone » Thu Feb 12, 2015 4:51 pm

You'll need to write the interface yourself. The protocol information is available on the wiki at http://wiki.bitcraze.se/doc:crazyradio:usb:index. The relevent USB driver code is written in python. You can find it on the crazyradio-firmware github at https://github.com/bitcraze/crazyradio- ... zyradio.py.
One day our flies will drown out the sun in an autonomous Skynet of whirring motors and blinking lights.

t_walker_21
Beginner
Posts: 17
Joined: Wed Feb 11, 2015 7:39 pm

Re: Crazyradio with Matlab

Post by t_walker_21 » Mon Feb 16, 2015 5:45 pm

Thank you for the information.

t_walker_21
Beginner
Posts: 17
Joined: Wed Feb 11, 2015 7:39 pm

Re: Crazyradio with Matlab

Post by t_walker_21 » Mon Feb 16, 2015 6:22 pm

Can you give me information on going about rewriting the usb driver?

poizone
Member
Posts: 59
Joined: Thu Feb 05, 2015 3:59 am
Location: Ohio, USA

Re: Crazyradio with Matlab

Post by poizone » Tue Feb 17, 2015 11:32 pm

t_walker_21 wrote:Can you give me information on going about rewriting the usb driver?
As a humble undergrad student, I haven't dealt with this sort of thing before. I can say that the radio chip used is very common in development and embedded circles, so google and the wiki would be the best place to start. I'm also not familiar enough with Matlab to give you the best place to start interfacing between them. It shouldn't require a rewrite so much as a Matlab interpreter that uses your Matlab data to generate the proper packets to send through the radio. These could be based on code found in the Python Client implementation.
One day our flies will drown out the sun in an autonomous Skynet of whirring motors and blinking lights.

t_walker_21
Beginner
Posts: 17
Joined: Wed Feb 11, 2015 7:39 pm

Re: Crazyradio with Matlab

Post by t_walker_21 » Fri Feb 20, 2015 5:07 pm

Cool, thanks for the information.

NisipM
Beginner
Posts: 1
Joined: Mon Feb 08, 2016 7:13 pm
Location: Florida

Re: Crazyradio with Matlab

Post by NisipM » Mon Feb 08, 2016 7:15 pm

Has anyone had any success with using Matlab to communicate to the CrazyFlie?

MCFurry
Beginner
Posts: 3
Joined: Tue Jan 05, 2016 12:10 pm

Re: Crazyradio with Matlab

Post by MCFurry » Tue Feb 09, 2016 1:00 pm

The way I did it is to write either a mex-function for interfacing with matlab, or a c-style level 2 s-function to communicate with simulink. Both functions are then linked to libcflie. It works pretty well. Although, you can only send the thrust,roll,pitch,yaw control values to the copter and retrieve the current roll,pitch,yaw and battery voltage from the copter. I'm still trying to figure out how to include parameters as well (for example to enable altitude-hold).

Fetz93
Beginner
Posts: 1
Joined: Fri Aug 05, 2016 12:09 pm

Re: Crazyradio with Matlab

Post by Fetz93 » Fri Aug 05, 2016 12:40 pm

Hi all, I have the same problem.

I want to communicate with the crazyflie 2.0 with the crazyradio PA and MATLAB simulink.
It has to work on Windows and as real-time code in Matlab, because I want to realize a position Control of multiple crazyflies in Simulink with Optitrack.

I found that there is already a c++ library cflie, but it is written for unix:
https://github.com/fairlight1337/libcflie
Also someone managed to control the crazyflie with simulink. This is the model i found and want to test:
https://github.com/aruneinstein/crazyflie-client-matlab

Now I tried to adapt the c++ library to work with windows. I have managed to get the library of this model working and could compile the s-function and control it in simulink.
The problem is, that i have to generate real-time code with simulink coder and here i get too many errors which i can't fix.
Do you have any idea, how i could get this library in rtwt (Real-Time Windows Target) running?
MCFurry wrote:The way I did it is to write either a mex-function for interfacing with matlab, or a c-style level 2 s-function to communicate with simulink. Both functions are then linked to libcflie. It works pretty well. Although, you can only send the thrust,roll,pitch,yaw control values to the copter and retrieve the current roll,pitch,yaw and battery voltage from the copter. I'm still trying to figure out how to include parameters as well (for example to enable altitude-hold).
Can you please share your s-function?
Are you able to generate real-time code with it?
Do you use it with unix, or with windows?

Any help is appreciated!

quellofool
Beginner
Posts: 5
Joined: Tue Aug 09, 2016 5:27 pm

Re: Crazyradio with Matlab

Post by quellofool » Wed Sep 07, 2016 5:08 pm

So this was my approach and so far I'm able to send commands to a single crazyflie:

I started with this API I found on GitHub that builds MATLAB MEX functions utilizing ZMQ's C Library: https://github.com/fagg/matlab-zmq

The documentation and commenting is pretty good. Once it's installed make sure to run their test scripts to ensure everything is built correctly. I then wrote this script to test sending inputs to the BitCraze client:

Code: Select all

%% CrazyFlie ZMQ Send - Test Script
% 
% This script is intended to demonstrate how to send control inputs
% to the crazyflie using the ZMQ input socket via matlab. The functionality is 
% identical to the python ZMQ input script: thrust inputs (25% - 45% - 0%) are 
% sent at 1 second intervals.
%


context = zmq.core.ctx_new();
socket  = zmq.core.socket(context, 'ZMQ_PUSH');
address = 'tcp://127.0.0.1:1212';
zmq.core.connect(socket, address);

cmd = struct('version',{1},'ctrl',struct('roll',0,'pitch',0,'yaw',0,'thrust',0));
cmdMessage = sprintf('{ "version": %d, "ctrl": { "roll": %f, "pitch": %f, "yaw": %f, "thrust": %f } }', cmd.version, cmd.ctrl.roll, cmd.ctrl.pitch, cmd.ctrl.yaw, cmd.ctrl.thrust);

fprintf('Sending Control inputs! \n \n \n');

%Thrust Unlock,
cmd.ctrl.thrust = 0;
cmdMessage = sprintf('{ "version": %d, "ctrl": { "roll": %f, "pitch": %f, "yaw": %f, "thrust": %f } }', cmd.version, cmd.ctrl.roll, cmd.ctrl.pitch, cmd.ctrl.yaw, cmd.ctrl.thrust);
zmq.core.send(socket, uint8(cmdMessage));

pause(0.01);
fprintf('Sending: %s \n',cmdMessage);
cmd.ctrl.thrust = 25;
cmdMessage = sprintf('{ "version": %d, "ctrl": { "roll": %f, "pitch": %f, "yaw": %f, "thrust": %f } }', cmd.version, cmd.ctrl.roll, cmd.ctrl.pitch, cmd.ctrl.yaw, cmd.ctrl.thrust);
zmq.core.send(socket, uint8(cmdMessage));

pause(1.00);
fprintf('Sending: %s \n',cmdMessage);
cmd.ctrl.thrust = 45;
cmdMessage = sprintf('{ "version": %d, "ctrl": { "roll": %f, "pitch": %f, "yaw": %f, "thrust": %f } }', cmd.version, cmd.ctrl.roll, cmd.ctrl.pitch, cmd.ctrl.yaw, cmd.ctrl.thrust);
zmq.core.send(socket, uint8(cmdMessage));

pause(1.00);
fprintf('Sending: %s \n',cmdMessage);
cmd.ctrl.thrust = 1;
cmdMessage = sprintf('{ "version": %d, "ctrl": { "roll": %f, "pitch": %f, "yaw": %f, "thrust": %f } }', cmd.version, cmd.ctrl.roll, cmd.ctrl.pitch, cmd.ctrl.yaw, cmd.ctrl.thrust);
zmq.core.send(socket, uint8(cmdMessage));

pause(1.00);
fprintf('Sending: %s \n',cmdMessage);
cmd.ctrl.thrust = 0;
cmdMessage = sprintf('{ "version": %d, "ctrl": { "roll": %f, "pitch": %f, "yaw": %f, "thrust": %f } }', cmd.version, cmd.ctrl.roll, cmd.ctrl.pitch, cmd.ctrl.yaw, cmd.ctrl.thrust);
zmq.core.send(socket, uint8(cmdMessage));

zmq.core.disconnect(socket, address);
zmq.core.close(socket);

zmq.core.ctx_shutdown(context);
zmq.core.ctx_term(context);
For SIMULINK integration, you need to utilize an initialization script to open the socket, a function block to send data to the socket, and a termination script to close the socket. Note that all of these MEX/Matlab functions will need to be declared extrinsic in the scripts and function block using the coder.extrinsic() function. Also, you will need to handle cast the 'socket' variable as a double since the ZMQ socket function returns a uint64 and for whatever reason SIMULINK doesn't support int64/uint64 types.

Let me know if there are any questions. Good Luck.

Post Reply