FRC4564 / Maestro

Python class to support Pololu's Maestro servo controller over USB serial. Great with Raspberry Pi.
MIT License
92 stars 59 forks source link

Issues controlling multiple servo at same time #6

Closed Razbotics closed 7 years ago

Razbotics commented 7 years ago

hey, I tested your library and its a good job done by you but your code has some issues it works for only one servo at a time let me explain first it gives error saying it needs to be encoded to bytes, unicode not supported so we should simply edit it to cmd.encode() this solves the problem in python shell

but if you try to send command for 2 servos like this

maestro.setTarget(0,6000)
maestro.setTarget(1,6000)

maestro will always understand the second command and the servo1 will reach to position to 6000 and hold the position and servo 0 will not go anywhere also you can freely rotate servo 0 with your hands this concludes that there is some error in sending bytes to maestro which will always light up red led

i have also posted this issue on pololu forum https://forum.pololu.com/t/controlling-two-servos-at-same-time-problem/12282

Today i figured out the problem using .encode() solves the problem from python side but it also adds up an extra byte '/xc2' this creates error in maestro servo controller suppose you want to give command like this

maestro.setTarget(0,6000)
maestro.setTarget(1,6000)

this sends bytes like '\xc2\xaa\0xc\0x04\channelbyte 0\byte lsb0\byte msb0 \xc2 \xaa\0xc\0x04\channelbyte1\byte lsb1\byte msb1' if i am not wrong this means maestro detects wrong command \xc2\xaa\0xc\0x04\channelbyte 0\byte lsb0\byte msb0 \xc2' upto here and next command '\xaa\0xc\0x04\channelbyte1\byte lsb1\byte msb1' gets valid this ends up controlling only servo 1 also lights up red led on maestro indicating unknown command error

so i have to again modify the code like this

import serial
ser=serial.Serial('COM10',115200,timeout=1)

def setTarget(chan, target):

        lsb = target & 0x7f #7 bits for least significant byte
        msb = (target >> 7) & 0x7f #shift 7 and take next 7 bits for msb
        # Send Pololu intro, device number, command, channel, and target lsb/msb
        cmd = (chr(0xaa) + chr(0xc) + chr(0x04) + chr(chan) + chr(lsb) + chr(msb)).encode()
        command = cmd.split(b'\xc2',1)
        ser.write(command[1])
i=9000
setTarget(0, i)
setTarget(1, i)

which will split the bytes by \xc2 and will send the exact required command this finally solves all the problem and all the servo reaches correct position also no red error led

hope You will understand this long issue and modify the library so that our python guys gets no issues maybe the issue was because i am coding on my pc using python and will not cause any problem on RPi

byronmejia commented 7 years ago

I haven't run into this issue with the raspberry pi's before. We use them at work as an educational tool. I'll personally try to replicate the problem on my computer soon.

Razbotics commented 7 years ago

Hey, As i said it works fine on python 2 but its not working on python 3 as unicode is not supported we have convert it in bytes. Thanks for replying

byronmejia commented 7 years ago

Ohhh. I'm so sorry, @Shubho97 . I totally missed that.

FRC4564 commented 7 years ago

I've added support for Python 3. Give it a try. I've haven't run into any issues updating multiple targets at once. My use of this module is to control some larger scale robots with a combination of electronic motor controllers as well as some servos for animatronics. That code updates multiple servo channels 30 times per second without a hitch. Let me know if you continue to have trouble.

Razbotics commented 7 years ago

Sure , will let you know soon after testing it...well i was using the library for a hexapod project. Thanks for the support and making changes.