Environment Building
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.
1 Download and Installation of 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.
1.1 Installing Python
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.
2 Preparations
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
3 Import of pymycobot
This part takes MyCobot 320 Pi as an example to introduce how to control a robot via python.
- Import pymycobot library for MyCobot320 :
from pymycobot.mycobot320 import MyCobot320
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.
4 Simple Demo
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.mycobot320 import MyCobot320
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 MyCobot320
# 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.