Raspberry Pi cases, Raspberry Pi, Arduino, micro:bit, robots

Lesson 4 - Controlling PiArm without GUI

Lesson 4 - Controlling PiArm without GUI

Controlling using GUI is very easy but what if there is no GUI, so in this case, you need to program the PiArm.

Note: The piarm.py and serial_comm.py are the required files for operating PiArm. Keep the two files in the same directory where you are going to write your code.

PiArm Functions

PiArm is a class which contain functions to control the PiArm. The PiArm consists of 6 serial servo motors. Each motor is identified by its ID which is unique numbering from 1-6 in PiArm. Reading and writing of data are done by sending serial packets for particular servo differentiated on the basis of there IDs. The servo also reverts with response packets which contain either acknowledgement or data for position, temperature, voltage, torque, etc. 

The data format for serial servos is:

[Start Byte, Start Byte, Servo ID, Number of Bytes, Register Address, Data, Checksum]

Start Byte: 0x55

Servo ID: ID of the servo to write or read from.

Number of Bytes: Number of bytes to send except start bytes and checksum.

Register Address: Each instruction has an address which is used for reading/writing operation in various commands. The Addresses are mentioned in the PiArm class.

Checksum: For a checksum, we need to add all the elements in the data packet except start byte. Then complement the sum and anding with 0xFF gives us our checksum.

~(Servo ID + Number of Bytes + Register Address + Data) & 0xFF

Note: Data is only needed to write instructions. It is an optional parameter.

In the PiArm class, the first imported module is SerialComm which is used for serial communication with PiArm. This module uses threads for serial read and writes operations. 

Importing

Make sure the piarm.py and serial_comm.py file are in the working directory. The PiArm module is imported as:

from piarm import PiArm

Initialisation

The PiArm module needs no parameters, hence it can be easily initialised as-

robot = PiArm()

Serial Communication

There are various methods in PiArm class which are used to control the PiArm. To start the communication we need to initialize a serial communication between PiArm and Raspberry Pi. The serial instance is created using connect function which takes port and baud rate as input arguments. It automatically adds “/dev/” so we only need to type the port name. The default baudrate for servos is 115200. 

robot.connect(port=”ttyS0”, baudrate=115200)

Read

PiArm class has lots of reads and write functions. The read functions return the data packets received from the robot. 

The read functions return a data packet in the byte format which contains the data for the current state of PiArm.

tempRead, voltageRead, positionRead, readAngleOffset, readAngleLimit, readVolLimit, readTempLimit, readID, ledRead are the functions which accepts servo ID as argument and returns the desired data.

Example: positionRead takes one argument that is the ID of servo you want to read the position of. It returns a data packet in the byte format which contains the position of servos.

response = robot.positionRead(ID=1)

pos = int.from_bytes(response[5]+response[6], byteorder='little')

Write Parameters

The instructions such as changing the position of PiArm, changing the temperature, voltage, position range, etc to the servos are performed with write functions. These functions don’t return anything. 

The write functions are adjustAngleOffset, writeAngleLimit, writeVolLimit, writeTempLimit, torqueServo, writeID, servoWrite, ledWrite. All these functions accept servo ID and data arguments. 

The writeID accepts only 1 argument which is a new ID, it changes the ID of all the connected servos to the argument passed.

Example: servoWrite function takes three arguments that is ID, position, and time. Position and time are for writing a position to the servo in the given time period. Positions value ranges from 0 to 999 and time value from 0 to 3000 millisecond.

robot.servoWrite(ID=2, position=500, time=1000)

Program Explanation

By manually entering the positions of the motors:

The whole program is shown below with the explanation. 

Import the required modules. PiArm module is needed for controlling PiArm functionalities. Sleep module for providing a proper delay between commands. The regex re module is used for text search from the file.

from piarm import PiArm         // importing piarm library
from time import sleep            // importing time module

Initialise the PiArm module and connect to the serial communication port. Connect is the same as pressing the ‘connect’ button on Control or Config software.
robo = PiArm()         // creating instance of “PiArm” class
robo.connect('/dev/ttyS0')      // connecting to PiArm serially to the ttyS0 port


Position values for each servos for every command in the group. Below is the position data of a group.
pos = [[472, 497, 306, 478, 484, 570],
[471, 499,303 ,739, 486, 571],
[472 ,499,206 ,739 ,421, 576],
[579, 499 ,207, 739 ,408, 576],
[579, 499 ,206, 739 ,533, 576],
[579, 499 ,206 ,748 ,555, 944],
[579, 499 ,207 ,748 ,413, 944],
[498, 499 ,207 ,748 ,420, 944],
[498, 499 ,208 ,748 ,575, 944],
[496, 499 ,206 ,749 ,614 ,575]]


Iterate over the positions of PiArm.
for command in pos:

Iterate over each servo of PiArm and write the position value to PiArm. Here the time is constant i.e. 500 and IDs are incremented via the loop.
for ID in range(6):
robo.servoWrite(ID + 1, command[ID], 500)
sleep(1)

You can also write your own piece of codes according to your requirements. Go to the following link for python file-

https://github.com/sbcshop/PiArm/blob/master/PiArm_without_GUI.py


By using the example codes directly from the exported file:

The whole program is shown below with the explanation.

Import the required modules. PiArm module is needed for controlling PiArm functionalities. Sleep module for providing a proper delay between commands. The regex re module is used for text search from the file.

from piarm import PiArm
from time import sleep
import re


Initialise the PiArm module and connect to the serial communication port. Connect is the same as pressing the ‘connect’ button on Control or Config software.
robo = PiArm()
robo.connect('/dev/ttyS0')


Create a file handle using the open function, pass the name of the file you want to run.
file = open('/home/pi/PiArm/Export Files/pick and place.txt', 'r')   // opening the file

Creating an empty list for cammand data to store the commands. Iterate over each line of file and append to command_data.
command_data = [ ]
for line in file:
command_data.append(line.strip())

Store total commands in the file in a variable size.
size = len(command_data)

Iterate over each line of the file and process the command. Split values for all servos in the command.
for command in range(0, size):
raw_Data = command_data[command]
raw_Data = raw_Data.split(' ')

Fetch and remove the delay time from raw_Data, extract the time from the string and convert to integer.
delay = raw_Data.pop()
delay = delay.split(':')
delay = int(int(delay[1])/1000


Iterate for all the servos in the command and write the position for each servo motor.
for value in range(6):

Find the integers in the raw_Data
cmd_Data = re.findall('\d+',raw_Data[value])
print('Cmd Data- ', cmd_Data)

Write the position, time for a servo ID. cmd_Data has data arranged like [ID, Position, Time]
robo.servoWrite(int(cmd_Data[0]), int(cmd_Data[1]), int(cmd_Data[2]))

Sleep for the delay time.
sleep(delay)

Go to the following link for python file-
https://github.com/sbcshop/PiArm/blob/master/PiArm_without_GUI_1.py

 

Previous article Lesson 3 - Controlling using Joystick
Next article Lesson 5 - Integrating the ultrasonic sensor

Leave a comment

Comments must be approved before appearing

* Required fields



Net Orders Checkout

Item Price Qty Total
Subtotal £0.00 GBP
Shipping
Total

Shipping Address

Shipping Methods

x