S.A.R.T Development Blog

Day 135 – Let’s break it down! “movement.py” part 1

In this post, I’m going to break down the “movement1v.py” script which, if you didn’t read the last post I wrote, can be accessed on our GitHub repository here. So, let’s break it down!

The modules imported at the top can be attained from pip3 with these commands:

Note: asyncio and websockets only work in python 3.3 or above

These imports are important because the first one allows us to connect to AX18 servos, the second one for delays and sleeping, the third one allows us to create websockets in python and asyncio is a necessary dependency for the websockets module.

The first line here is to create a variable which stores our connection to our servos. They are connected to our serial port “/dev/ttyACM0” and the baudrate has to be “1000000”.

speedSettingToByteArray(reverse, arc, speed) function

The function “speedSettingToByteArray” is a very important function. This function essentially turns a speed percentage sent from the client or the “SART Control Panel” and turn it into a speed readable by the servos. It takes 3 parameters: “reverse” which is a boolean, True means the servo is going to reverse and False means it is going to go forward. “arc” is another boolean. This is not necessary anymore. The last parameter is the “speed” setting. This is an integer value between between and including 1 – 10, indicating 1 = 10%, 2 = 20% and so forth until 10 = 100%.

The variable ‘a’ in the function is the speed as it gets converted to a percentage of 1023, depending on what the ‘speed’ value was. Next, the function checks if it should be reversing. If so, we add 1024 to the speed var of ‘a’. This is because values 0 – 1023 make the servos run forward and values 1024 – 2048 make it reverse.

Next, it checks if it should be arching. Arching isn’t used in this script anymore, but if it was, the servo would go at half the speed while the others would go full speed and run as if it was turning. This way the robot would still go forward but slightly turn. The function then returns a byte array of the variable ‘a’, which is the correct format for the servos.

runMotor(m_id, speed) function

This function is used to run a specific motor using its ID given by the first parameter, m_id. The other parameter is a speed, in byte array format.

The only line in this function uses the servo connection variable at the start of the script to write data to a specified servo with a specified ID, the address of 32 which is for all AX18A servos and the speed in byte array form which can be converted from integer using the previously explained function “speedSettingToByteArray”.

runMotorGroup(m_ids, spd) function

This function does what it names implies: runs more than 1 servo with the same speed. It’s first parameter “m_ids” is an array of integers, aka the IDs. The second parameter is again, a speed in byte array format. The function then loops through all the IDs in the array and runs the “runMotor” function mentioned above to run the motors. Simple!

That’s it for this post. Stay tuned for the next post, part 2 of breaking down the “movement1v.py” script!





Leave a Reply


© Copyright SFXRescue 2018

Website created by Aaron Maggs and Jack Williams with support from St Francis Xavier College


Support Email: support@sfxrescue.com

Website Change Log