Individual control of the motors on the crazyflie

Discussions and questions about the Crazyflie Nano Quadcopter
Post Reply
Rasmus J.
Beginner
Posts: 3
Joined: Wed Nov 20, 2019 8:40 am

Individual control of the motors on the crazyflie

Post by Rasmus J. »

HI Bitcraze,

Me and my university group has decided to use the Crazyflie platform to show a conceptual approach to constructing control algorithms for z and x/y axis control through system identification and PID tuning.

However, we want to run all of our code off of an crazyflie, including the PID calculation, and do all the motor control ourselves.
What would be the best approach to using the PWM functionality along with logging of the Gyro and accelerometer ?
because so far we cant find heads and tails in the code other than the SyncCrazyflie library being used to connect to the crazyflie via the crazyradio :)?
So we are a bit confused as to how we can update the PWM for each motor and send it to the crazyflie in "real time" (although we know that the crazyradio has a delay of 1-2 ms of course)

Best Regards,
Rasmus J.
arnaud
Bitcraze
Posts: 2538
Joined: Tue Feb 06, 2007 12:36 pm

Re: Individual control of the motors on the crazyflie

Post by arnaud »

There is currently no radio packet that directly feeds motors PWM but there is parameters that can be use to control the motors PWM independently, though it requires to send 4 packets (one per motor) which will increase even more your latency. The parameters can be useful to start with anyway, they are defined there: https://github.com/bitcraze/crazyflie-f ... #L114-L120.

In order to add a packet containing the PWM, the most 'correct' way might be to add motors PWM to the setpoint structure (https://github.com/bitcraze/crazyflie-f ... pes.h#L171) and then create a new commander packet with PWMs both on the Crazyflie (https://github.com/bitcraze/crazyflie-f ... eric.c#L39) and python (https://github.com/bitcraze/crazyflie-l ... der.py#L38) side. In the stabilizer loop (stabilizer.c) the PWM setpoints could then be copied directly in the control structure.

As for getting accelerometer and gyro measurement, you need to use the log subsystem to log the measurements. One possible limitation is that the log is currently setup to allow a maximum log rate of 100Hz. It is quite easy to hack that to allow up to 1KHz but the radio bandwidth is only of ~800packet/seconds both way so 1KHz is not achievable through the ratio.

One higher level note: Full control down to PWM though the radio has only been achieved once as far as I know and the latency and jitter caused by the radio link can be a challenge. It would be easier to control the Crazyflie just one step up by sending attitude rate and leaving the rate PID in the Crazyflie. This would make the latency less of a problem and as a bonus I think the Crazyflie is already setup to accept rate setpoint in the current PID implementation.
Rasmus J.
Beginner
Posts: 3
Joined: Wed Nov 20, 2019 8:40 am

Re: Individual control of the motors on the crazyflie

Post by Rasmus J. »

Thank you for the fast response :)!

We will try and proceed as you have described and see where it gets us :)
although we have to implement our own PID control algorithm, so unless there is a way to tune the P I and D values then we need to implement it ourselves

Br,
Rasmus J
arnaud
Bitcraze
Posts: 2538
Joined: Tue Feb 06, 2007 12:36 pm

Re: Individual control of the motors on the crazyflie

Post by arnaud »

You can tune all parameters of the existing PID's using the parameter subsystem: https://github.com/bitcraze/crazyflie-f ... #L201-L211
Rasmus J.
Beginner
Posts: 3
Joined: Wed Nov 20, 2019 8:40 am

Re: Individual control of the motors on the crazyflie

Post by Rasmus J. »

Hi again, we now have it hovering and being able to control the thrust through the ZMQ interface, however we have encountered another difficulty.

Now we want the drone to roll and pitch to move it in the horisontal plane, but it seems as though we either lack the understanding of the parameters or the code.

Lets sau we want the drone to roll at an angle of 10 degrees

We have the following code :

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# Test af vinkelkontrol


import time

try:
import zmq
except ImportError as e:
raise Exception("ZMQ library probably not installed ({})".format(e))

context = zmq.Context()
sender = context.socket(zmq.PUSH)
bind_addr = "tcp://127.0.0.1:{}".format(1024 + 188)
sender.connect(bind_addr)

cmdmess = {
"version": 1,
"ctrl": {
"roll": 0.0,
"pitch": 0.0,
"yaw": 0.0,
"thrust": 99
}
}
print("starting to send control commands!")
# Herfra er det imperativt eksekveret

# Oplåsning af "thrust protection"
cmdmess["ctrl"]["thrust"] = 0
sender.send_json(cmdmess)


print("slut på thrustlock ")

#D
# Dronen flyver opad
cmdmess["ctrl"]["thrust"] = 62
sender.send_json(cmdmess)
time.sleep(1)


#Dronen holder stille i luften i 3 sek (ved fuld opladet batt og 3 viconkugler)
print("lander!!!!!!!!")
cmdmess["ctrl"]["thrust"] = 58.2
sender.send_json(cmdmess)
time.sleep(3)


#Dronen flyver fremad med en vinkel på (forhåbentlig) 3 grader i 1 sekundt.
print("lander!!!!!!!!")
cmdmess["ctrl"]["roll"] = 10
sender.send_json(cmdmess)
time.sleep(1)

#Dronen retter op og gør klar til stilstand
#Dronen flyver fremad med en vinkel på (forhåbentlig) 3 grader i 1 sekundt.
print("lander!!!!!!!!")
cmdmess["ctrl"]["roll"] = 0
sender.send_json(cmdmess)
time.sleep(0.1)


#Dronen holder stille i luften i 2 sek (ved fuld opladet batt og 3 viconkugler)
print("lander!!!!!!!!")
cmdmess["ctrl"]["thrust"] = 58.2
sender.send_json(cmdmess)
time.sleep(2)

#Dronen daler i 4 sekunder
print("Vinkler dronen")
cmdmess["ctrl"]["thrust"] = 56
sender.send_json(cmdmess)
time.sleep(4)

#Dronen slukker motorerne
print("slut for programmet ")
cmdmess["ctrl"]["thrust"] = 0
sender.send_json(cmdmess)
print("motorer slukket")
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

However, no matter what i change the "roll" parameter to, it will not move at an angle but merely stands still in the air.

Are we missing osmething :)?
Br,
Rasmus
arnaud
Bitcraze
Posts: 2538
Joined: Tue Feb 06, 2007 12:36 pm

Re: Individual control of the motors on the crazyflie

Post by arnaud »

In principle what you are doing should make the Crazyflie roll 20 degree. Though, I think I might be missing an important information because from what I understand there is no way you manage to get your Crazyflie to " stands still in the air" with open loop control like this code seems to to.

A couple of question so that I can understand a bit more what is happening:
- Are you using the headless ZMQ client or are you using the ZMQ endpoint in the graphical client?
- Are you using any assisted mode in the client and do you have anything attached to the Crazyflie like a flow deck?

Ps. The forum has a "code display" functionality that makes is much easier to read copy-pasted code.
Post Reply