Using ZMQ as input device

Firmware/software/electronics/mechanics
Post Reply
rshum19
Beginner
Posts: 4
Joined: Thu Jul 30, 2015 10:39 pm

Using ZMQ as input device

Post by rshum19 »

Hello,

I'm trying to control my crazyflie using ZMQ as the input device. I have enabled the Crazyflie python client to use ZMQ as input device by editing the config.json file. I have also created a python script that will send a constant thrust signal but I can't figure out why its not working. When I run the python script nothing happens.

This is what I'm doing:
1) open CF python client w/ ZMQ enabled
2) connect the crazyflie
3) run the python script in the terminal

Any help or comments will be highly appreciated.

thanks,

Code: Select all

import time

try:
    import zmq
except Exception 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)

#sender.send_json(zmess)
thrust_step = 100
thrust_max = 30000
thrust_min = 20000
thrust = thrust_min

cmdmess = {
    "version": 1,
    "ctrl": {
        "roll": 0.0,
        "pitch": 0.0,
        "yaw": 0.0,
        "thrust": 20000
        }
}
print "starting to send control commands!"
while 1:
    time.sleep(0.01)
    sender.send_json(cmdmess)
arnaud
Bitcraze
Posts: 2538
Joined: Tue Feb 06, 2007 12:36 pm

Re: Using ZMQ as input device

Post by arnaud »

Hi,

I could not run zmq on the last "develop" branch because there was a bug in the client. I pushed a fix so you can pull the latest commits from the client develop branch.

There was a bug in your example as well, the thrust is in percent so this would work:

Code: Select all

import time

try:
    import zmq
except Exception 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)

#sender.send_json(zmess)
thrust_step = 100
thrust_max = 30000
thrust_min = 20000
thrust = thrust_min

cmdmess = {
    "version": 1,
    "ctrl": {
        "roll": 0.0,
        "pitch": 0.0,
        "yaw": 0.0,
        "thrust": 30
        }
}
print "starting to send control commands!"

# Unlocking thrust protection
cmdmess["ctrl"]["thrust"] = 0
sender.send_json(cmdmess)

while 1:
    cmdmess["ctrl"]["thrust"] = 30
    time.sleep(0.01)
    sender.send_json(cmdmess)
A last things is that the new Crazyflie firmwares have a thrust protection: you need to send one time 0% thrust to unlock it. This is designed to avoid flying away when connecting with a bugged joystick. I added to the code a line to send 0 first and then send the right thrust.

Can I use your code as an example and push it in the repos? I see that we have no easy example for that and your code is nice and simple.
rshum19
Beginner
Posts: 4
Joined: Thu Jul 30, 2015 10:39 pm

Re: Using ZMQ as input device

Post by rshum19 »

Hi,

Great thanks for fixing the bugs it works nicely now. Yes, by all means go ahead and push my code.
arnaud
Bitcraze
Posts: 2538
Joined: Tue Feb 06, 2007 12:36 pm

Re: Using ZMQ as input device

Post by arnaud »

Hi,

Glad that it is working. I pushed an example and credited you (I assumed you have the same name on GitHub).

Thanks.
Post Reply