【Customer sharing】-- Serial port control Raspberry Pi DOFBOT Robotic arm from Mr.Wu - Yahboom

Because the Dofbot-pi is installed with Ubuntu mate instead of raspian system, it is impossible to open the serial port with raspi config to modify the serial port settings.

There are two serial ports inside the Raspberry Pi CPU, one is the hardware serial port (/dev/ttyama0), and the other is the mini serial port (/dev/ttys0).
The hardware serial port has a separate baud rate clock source, which has good performance and strong stability; The mini serial port has simple function and poor stability. The baud rate is provided by the CPU core clock and is affected by the core clock.

The serial port we use for serial communication needs high stability and performance, so the hardware serial port has become our first choice.
Since the default hardware serial port is allocated to the Bluetooth module, we need to allocate the hardware serial port to the GPIO serial port. The specific operations are as follows.

1. Open the terminal and enter the command sudo ls -l /dev

Input password   yahboom

 

We can see that the serial port configuration in the initial state, as shown below.

2. Set the hardware serial port to GPIO serial port

2.1 Modify file   /boot/firmware/config.txt   set  enable_uart=1  If enable_uart = 0, minicom serial port is enabled instead of this hardware serial port.

Input following command:

sudo nano /boot/firmware/config.txt

2.2 Add  dtoverlay=miniuart-bt  at the end, then click Ctrl + X and enter y, and finally press enter to save and exit.

3. Modify the   /boot/firmware/cmdline.txt  file and delete all the contents about the console. 

Input following command:

sudo nano /boot/firmware/cmdline.txt

Open as

 Delete content about console

Click Ctrl + X and enter y, and then press enter to save and exit

4. Turn off Bluetooth

Input following command:

sudo systemctl disable hciuart 

5. Turn off the power switch of the Dofbot-pi and restart it.

Input following command to check whether the modification is successful

sudo ls -l /dev

 

At this time, the following figure shows that the modification is successful.

6. Connect the Raspberry Pi to the computer with the micro USB cable, and open the serial port debugging assistant (the display ch340 indicates that the computer has been connected to the serial port of the robotic arm).

Click the open serial port button,Open the serial port, as shown below.

 7. Write serial port test program  serial_ test.py

Input following command: 

nano serial_test.py

 

8. About code

#Import the necessary libraries
import RPi.GPIO as GPIO
import time
import string
import serial

from Arm_Lib import Arm_Device
Arm = Arm_Device() #create object

#Set GPIO port as BCM coding mode
GPIO.setmode(GPIO.BCM)

#Ignore warning messages
GPIO.setwarnings(False)

uart = serial.Serial("/dev/ttyAMA0", 115200, timeout=0.001)
if uart.isOpen == False:
uart.open()
uart.write('Raspberry pi is ready'.encode())

try:
while True:
size = uart.inWaiting() # Get buffer characters
if size != 0:
response = uart.read(size) # Read content and display
#print(response)
m=str(response, encoding = "utf-8")
uart.flushInput() # Empty receive buffer
time.sleep(0.1) # Software delay
if m == '3':
Arm.Arm_serial_servo_write6(90, 120, 90, 90, 120, 90, 500) #Control the movement of the Dofbot
uart.write('Execute 9 complete '.encode())
print ('ok')
if m == '4':
Arm.Arm_serial_servo_write6(90, 40, 90, 60, 120, 80, 500)
uart.write('Execute 0 complete '.encode())
print ('ok')
if m == '1':
Arm.Arm_serial_servo_write6(90, 90, 90, 90, 90, 0, 500)
uart.write('Execute 1 complete '.encode())
print ('ok')
if m == '2':
Arm.Arm_serial_servo_write6(120, 60, 90, 90, 90, 90, 500)
uart.write('Execute 2 complete '.encode())
print ('ok')
if m == '0':
Arm.Arm_serial_servo_write6(180, 60, 90, 60, 90, 180, 500)
uart.write('Execute 0 complete '.encode())
print ('ok')
if m == '5':
Arm.Arm_serial_servo_write6(90, 90, 90, 90, 90, 180, 500)
uart.write('Execute 0 complete '.encode())
print ('ok')

except KeyboardInterrupt:
uart.close()

9. Input above content and save this file.

You can also put the file  serial_test.py  to the main directory

10. Close the large program and run  python3  serial _ test.py  file.

Input following command: 

sh ~/Dofbot/kill_YahboomArm.sh

Input following command: 

python3 serial_test.py

11. This prompt indicates that the port cannot be opened  /dev/ttyAMA0 Let's just modify the permissions.

Input following command: 

sudo chmod 777 /dev/ttyAMA0

Input following command: 

python3 serial_test.py 

12. Enter the number 0-5 and the Dofbotperforms some actions. For example, send the number 0. The terminal and serial port debugging assistant, as is shown below.

This code is only for testing and can be improved and modified by yourself.

 

 

 

 

 

Diy works

Leave a comment