UltraBorg and python3

Forums:

First of all - hello everyone. If my post is out of place I do apologise. Please let me explain the issue I am trying to deal with - I have read through the forum topics but it seems they are all a bit old (the most recent is months old) while I have a problem with the UltraBorg.py module and running it on Python3. The issue is with the print function which is used differently in Python3 compared to Python2. The version of UltraBorg.py file which I have managed to download seems to be using the old syntax for print. I can imagine rewriting every single line in the UltraBorg.py module that contains the print statement to make runnable in Python3 but this is not the dreamed start with UltraBord the least to say :) And I am not sure if, after making changes to print and making it work in Python3, something else will not pop up. So, if anyone could tell me if there is a newer version of the UltraBorg.py module that will for sure work in Python3 I would be more than grateful. And next thing - I have looked through the example code but I can't quite figure out how do I handle PWM using UltraBorg board? I have found some examples controlling DC motors but not quite so many examples showing how to control a motor using PWM.

Please advise.

piborg's picture

The first problem is easy to fix, we have a Python 3 version of the UltraBorg.py script that is ready to use :)

cd ~/ultraborg
wget -O UltraBorg3.py http://piborg.org/downloads/ultraborg/UltraBorg3.py.txt

To use this version you will need to change the import statement to

import UltraBorg3 as UltraBorg

Then you can use it normally.

I think there is a little confusion with the second part. UltraBorg is designed to drive servo motors. For example: MG996R - 180 degree metal gear servo or SG90 - 180 degree micro servo.

Servo motors work by having a fixed power connection (in UltraBorg's case 5V) and providing a PWM signal on another connection to tell the motor where to move to.

The PWM connection is labelled "S = Signal" in this diagram.

The PWM output level for each servo is set using the UB.SetServoPosition1 to SetServoPosition4 commands. It takes values from -1 to +1 for the position, with 0 being in the middle.

Since the exact PWM levels needed for each servo is slightly different you will need to set the range of movement using the Tuning GUI. Instructions can be found here: UltraBorg - Tuning.

UltraBorg is not designed to drive DC motors.

Thank you very much for your response, it clarifies a lot, especially I am so happy to see that the UltraBorg module is also available in Python3-compatible version :)

Now, to clarify the second part of my question which you referred to as "a bit confusing" :) The thing is that I know that "UltraBorg is not designed to drive DC motors". I am using it for my drone application where the UltraBorg is assumed to be able to control brushless motors through ESC. I saw a mention on the Raspberry PI forum that UltraBorg can be used for this purpose and I believe before I decided to buy the board, I also checked the PiBorg forum and found somewhere a discussion where the comment was saying that controlling brushless motors speed should be doable using UltraBorg. As you surely know, all that ESCs need to control motor speed is PWM signal. Battery is LiPO package so it is not to be connected to UltraBorg anywhere, UltraBorg must only generate the PWM signal. So, in the diagram you included (which I have found some time ago) where you put the servo in my application goes the ESC. All I need is to know which specific functions to use to generate PWM signal. I believe I can figure this out myself after some research but maybe you already know this so it would save me A LOT of time :)

Thank you in advance for any help.

piborg's picture

That makes more sense, thanks :)

There are four functions that allow you to directly alter the PWM signal outputs: UB.CalibrateServoPosition1 to UB.CalibrateServoPosition4.

These functions ignore any calibration settings and directly set the duty cycle for the PWM outputs. The values are from 0 to 0xFFFF (or 65535 in decimal) and must be given as integers (type int).

For example this snippet sets each output to a different duty cycle.

maxPwmLevel = 0xFFFF

# Output #1 to 50% duty
UB.CalibrateServoPosition1(int(0.50 * maxPwmLevel))

# Output #2 to 40% duty
UB.CalibrateServoPosition2(int(0.40 * maxPwmLevel))

# Output #3 to 100% duty (full on)
UB.CalibrateServoPosition3(int(1.00 * maxPwmLevel))

# Output #4 to 0% duty (off)
UB.CalibrateServoPosition4(int(0.00 * maxPwmLevel))

The PWM frequency is hard-coded into the UltraBorg and cannot be changed.

For your usage I would also recommend setting the limits so that the UltraBorg starts with all four outputs as low as possible on power up. This can be done with the following code:

# Import library
import UltraBorg3 as UltraBorg

# Setup the UltraBorg
UB = UltraBorg.UltraBorg()
UB.Init()

# Set the maximum limits to the largest allowed value
UB.SetWithRetry(UB.SetServoMaximum1, UB.GetServoMaximum1, 0xFFFE, 5)
UB.SetWithRetry(UB.SetServoMaximum2, UB.GetServoMaximum2, 0xFFFE, 5)
UB.SetWithRetry(UB.SetServoMaximum3, UB.GetServoMaximum3, 0xFFFE, 5)
UB.SetWithRetry(UB.SetServoMaximum4, UB.GetServoMaximum4, 0xFFFE, 5)

# Set the minimum limits to the smallest allowed value
UB.SetWithRetry(UB.SetServoMinimum1, UB.GetServoMinimum1, 0x0001, 5)
UB.SetWithRetry(UB.SetServoMinimum2, UB.GetServoMinimum2, 0x0001, 5)
UB.SetWithRetry(UB.SetServoMinimum3, UB.GetServoMinimum3, 0x0001, 5)
UB.SetWithRetry(UB.SetServoMinimum4, UB.GetServoMinimum4, 0x0001, 5)

# Set the startup value to be the minimum limit
UB.SetWithRetry(UB.SetServoStartup1, UB.GetServoStartup1, 0x0001, 5)
UB.SetWithRetry(UB.SetServoStartup2, UB.GetServoStartup2, 0x0001, 5)
UB.SetWithRetry(UB.SetServoStartup3, UB.GetServoStartup3, 0x0001, 5)
UB.SetWithRetry(UB.SetServoStartup4, UB.GetServoStartup4, 0x0001, 5)

The values are saved to the UltraBorg so they will take effect without needing to be set each time.

Hello. Just to update all potentially interested - indeed, the UltraBorg board very smoothly controlls brushless motors in my quadcopter project. Since Raspberry Pi has only 2 dedicated PWM outputs, for those wanting control more than 2 motors - this board will do the job.

Once again thank you very much for all help provided. If I have any other questions I will come back but for now this thread can be closed :)

Best
M.

Wow! You really made my day :) I don't know how to thank you for this but I am really grateful :) I will try to integrate your examples with my code and we'll see what happens ;) So far integration of the UltraBorg board with Raspberry PI was flawless, the module appears under 36 address along with my Altimu module. The UB.Init() worked without any problem, no errors returned so I believe the other functions will work, too :)

Once again, thank you very much ;)

Subscribe to Comments for "UltraBorg  and python3"