Publishing PWM values for motors

Firmware/software/electronics/mechanics
malintha
Beginner
Posts: 17
Joined: Thu Dec 07, 2017 4:58 pm

Publishing PWM values for motors

Post by malintha »

Hi all,

I'm working on controlling a Crazyflie 2.0 using this controller [1]. The output of the controller is simply the thrusts of each motor (Let's say PWM values) and I'm getting them from my controller node on ROS. Now I'm looking to publish these values to the drone.

I looked into the new commander_generic endpoint, but I couldn't figure out a way to map the setpoint arguments with my inputs (4 PWM values). So my plan is to,
1) introduce a new type to the crtpSetPointRequest which accepts the 4 thrust values.
2) create a new packet type and the decoder,
3) set these thrust values to the motor within the stock power distribution code.
4) change the ROS crazyflie server-side code accordingly.

I might be completely wrong here, so is there any simpler/better way to do this? I also want to skip the stabilizer in this case. Can we do that by setting any parameters?

[1] https://pdfs.semanticscholar.org/2e83/b ... 690afc.pdf
tobias
Bitcraze
Posts: 2339
Joined: Mon Jan 28, 2013 7:17 pm
Location: Sweden

Re: Publishing PWM values for motors

Post by tobias »

My first comment is if latency is a problem? Currently the latency of the link is 1-2ms due to USB handling. Or is the PWM values you send control inputs?
malintha
Beginner
Posts: 17
Joined: Thu Dec 07, 2017 4:58 pm

Re: Publishing PWM values for motors

Post by malintha »

Thanks Tobias for the reply. The PWM values are controller outputs coming from my controller in ROS side. They need to be directly posted to the crazyflie.
tobias
Bitcraze
Posts: 2339
Joined: Mon Jan 28, 2013 7:17 pm
Location: Sweden

Re: Publishing PWM values for motors

Post by tobias »

I'm guessing the question is if the latency will be a problem or not. There are some parameters were you can set the PWM values directly. This you should be able to do from ROS. Though I'm pretty sure latency will be to big to achieve stability.

The latency can be improved by creating a dedicated packet for the PWM values, preferably a generic commander packet that contains only PWM values and short-circuit the internal controllers.
malintha
Beginner
Posts: 17
Joined: Thu Dec 07, 2017 4:58 pm

Re: Publishing PWM values for motors

Post by malintha »

Hi Tobias,

I was able to publish the motor values directly to the drone from the ROS end by creating a new message type for the genericCommander type. So basically, what I did was;
1) Disabled the internal controllers of CF.
2) Created a new message type for motor ratios, that uses the genericCommander port.
3) Wrote a new controller on ROS, then published the output motor ratios to crazyflie.

I used Vicon to do the localization and the external control loop (trajectory and attitude both) runs at 200Hz. For some reason, the actual results are drastically different from the simulation results. In the simulation, I was able to achieve a perfect hover, circle and helix motions whereas the actual drone deviated hugely (~15cm) from the setpoint. As you have said, I think the latency could be an issue here. So right now I'm working on investigating it more deeply and seeing the possibility of implementinng the attitude controller onboard.
tobias
Bitcraze
Posts: 2339
Joined: Mon Jan 28, 2013 7:17 pm
Location: Sweden

Re: Publishing PWM values for motors

Post by tobias »

Great that you managed to get things up and flying! I remember someone managed to get down to a total of 4ms delay but can't find that info now.

Probably rate controller in enough to have on board. Keep us posted!
whoenig
Expert
Posts: 395
Joined: Mon Oct 27, 2014 2:55 am

Re: Publishing PWM values for motors

Post by whoenig »

VICON will add an additional latency of ~14ms, so the total latency for the system is quite high for attitude control. For best performance you would need to write your controller in C - you can still use the VICON to feed data into the EKF, but the on-board EKF has the advantage that it uses the on-board sensors for state estimation as well. You can look at controller_mellinger as an example. This is actually very similar to Lee's controller (Lee has a few more feed forward terms.)
malintha
Beginner
Posts: 17
Joined: Thu Dec 07, 2017 4:58 pm

Re: Publishing PWM values for motors

Post by malintha »

Thanks a lot, tobias and whoenig! I did a latency test by simultaneously measuring the same movement using IMU and VICON/ROS and it turned out that the mean latency was 24.75ms with a SD of 4ms, out of 50 readings (IMU readings had to be leveraged with VICON by a small offset). I think its believable, considering the VICON delay and the overhead from ROS stack. I found that Landry[1] has observed a delay ~28ms. So yes, I'm working on implementing it in the firmware using C. For Lee's controller, we'll have to rotate the internal IMU readings using a rotation matrix as it uses "+" configuration. Also, some matrix operations also need to be implemented onboard. But considering the 24.75ms delay, that'll improve the performances by a notable margin.

[1] http://groups.csail.mit.edu/robotics-ce ... ndry15.pdf
malintha
Beginner
Posts: 17
Joined: Thu Dec 07, 2017 4:58 pm

Re: Publishing PWM values for motors

Post by malintha »

Hi All,

I have implemented the code for the geometric controller (Lee's controller) onboard and was able to get the attitude controlling part working successfully. So when the drone is on, it was able to stabilize and float around. However, the issue was getting the position controlling part tuned. It seemed to be quite tricky as four tuning gains are highly correlated. For a simple taking-off, it goes up, stays there for 4-5 seconds and then simply collapses. I have read from Marcus's thesis that this controller could be tricky to tune.
However, my initial guess about the culprit is that the inertia matrix which I am not so certain about. This is my inertia matrix for the + configuration. {{3.4551e-5, 0, 0}, {0, 3.4551e-5, 0}, {0, 0, 5.0547e-5}}
Has anybody got a better matrix for + configuration? (I'm also using a customized frame for putting the vicon markers on, which weighs around 5g and I have not taken the inertia values of the frame into the consideration).
natolambert
Beginner
Posts: 4
Joined: Tue Aug 14, 2018 9:30 pm

Re: Publishing PWM values for motors

Post by natolambert »

All,

I have been trying to do the same direct PWM setting with ROS+Crazyflie, but have been running into timing issues.

Here is how we are sending out PWM values:
1. Value generated in controller python script acting as a ROSnode.
2. We changed an existing crtpCommander packet type (cmdVel) to store packaged 16 bit PWM values (four uint16's into one float to try and send one PWM signal per packet) with an added packet_ID for tracking.
3. On firmware, decode the crtpCommander packet to directly call powerdistribution and update the PWM values.
4. We record the PWM values set and the PWMid with log blocks. Our state variables are also packed into compressed packets to try and increase frequency. With the CFClient we demonstrated logging state and PWM values at 250hz if the timestamps are to be believed.

The problem is, no matter the control frequency of sending packets, we see a received PWM packet lag of ~400ms. This number was verified by changing the outgoing frequency and comparing the number of packets behind the logged PWMid was verses the previous sent PWM.

We were curious if your code was publicly available, as it does the same thing, or if anyone else has encountered long timer delay in the ROS system. We are new to ROS so were not sure if we needed to disable internal functions to get the speed up.

Thanks,
N
Post Reply