pymycobot is a Python package used for serial communication with myCobot. It supports Python2, Python3.5 and later versions.
Before using pymycobot, make sure to build a Python environment. Follow the steps below to install Python.
At present, Python has two versions: 2.x
and 3.x
. These two versions are incompatible with each other. This section takes the version 3.x
as an example due to its increasing popularity.
The Raspberry Pi version comes with an Ubuntu (V-20.04) system and a built-in Python development environment, so you don't need to build and manage it.
-
Firmware burning. Firmware serves as a driver for systems to control robots.
- Pi \ jetsonnano version
AtomMain
for Atom at the top is factory burnt.
- Pi \ jetsonnano version
-
Update pymycobot
pip install pymycobot --upgrade
This part takes MyCobot 320 Pi as an example to introduce how to control a robot via python.
- Import pymycobot library for MyCobot :
from pymycobot.mycobot import MyCobot
Notice:
- If no red wavy line appears below the codes, pymycobot is successfully installed.
- if a red wavy line appears, got to the address https://github.com/elephantrobotics/pymycobot to download pymycobot manually and put it into python library.
Create a new Python file, and type the following codes to set the color of RGB light panel.
Notice: The baud rates are different according to types of devices. Refer to calculator device manager to check the corresponding number.
- Codes for MyCobot:
# demo.py
from pymycobot.mycobot import MyCobot
import time
#The above codes are required to be written, which means importing the project package
# MyCobot class initialization requires two parameters:
# The first is the serial port string: '/dev/ttyAMA0'
# The second is the baud rate: 115200
#
#
# Example:
# mycobot-Pi: mc = MyCobot('/dev/ttyAMA0', 115200)
# Initiate MyCobot
# Create object code here.
mc = MyCobot("/dev/ttyAMA0", 115200)
i = 7
#loop 7 times
while i > 0:
mc.set_color(0,0,255) #blue light on
time.sleep(2) #wait for 2 seconds
mc.set_color(255,0,0) #red light on
time.sleep(2) #wait for 2 seconds
mc.set_color(0,255,0) #green light on
time.sleep(2) #wait for 2 seconds
i -= 1
Run the example file:
python3 demo.py
The blue, red, and green lights on the top of the robot flash 7 times continuously at an interval of 2 seconds.