Skip to main content

Develop and use based on Python

XGO-lite V2 has built-in motion control library file xgolib.py, education library xgoedu.py and advanced library xgoadvance.py, developers can directly call related interface functions to control the robot dog.

Sports library download xgolib.py(26 KB)

Initialization

The following is the initialization code:

#import xgolib
from xgolib import XGO
#instantiate dog
dog = XGO(port='/dev/ttyAMA0',version="xgolite")

Determine the type of robot dog

Since xgolite and xgomini have different motion performance and parameters, in order to accurately control the motion of the robot dog, you can use:

dog.read_firmware() function detects the type of robot dog

Sample code:

from xgolib import XGO
dog = XGO(port='/dev/ttyAMA0',version="xgolite")
version=dog.read_firmware()
if version[0]=='M':
print('XGO-MINI')
dog = XGO(port='/dev/ttyAMA0',version="xgomini")
dog_type='M'
else:
print('XGO-LITE')
dog_type='L'

The type of the dog can be judged through this piece of code. If the variable dog_type is 'M', the type of the dog is XGOMINI, and if it is 'L', the type of the dog is XGOLITE.

Motion Control Library Introduction

Pan back and forth

Move(direction, step)

parameter nameFormatinput rangeillustrate
directioncharacter'x'、'X'、'y'、'Y''x' or 'X' makes the dog move forward or backward, 'y' or 'Y' makes the dog move left or right
stepnumberx:[-25,25],y:[-18,18]This parameter represents the translation step size. According to the direction, a positive value represents forward or left movement, and a negative value represents backward or right movement. When the input value exceeds the range, it moves according to the limit value.

To rotate

Turn(step)

Parameter nameFormatInput rangeIllustrate
stepnumber[-150,150]This parameter represents the rotation speed, the unit is °/s, the positive value is left rotation, and the negative value is right rotation.

No progress

mark_time(data)

Parameter NameFormatInput rangeIllustrate
datanumber[10,35]This parameter represents the height of leg raising when standing still, the unit is mm, when the input is 0, stop standing still

Change the frequency of strides

pace(mode) speed = stride frequency x stride length

Parameter NameFormatInput rangeIllustrate
modestring['normal','slow','high']This parameter represents the stride frequency, normal is the default stride frequency, low is the slow stride frequency, and high is the high-speed stride frequency

Stop moving

Stop()

Examples of mobile related methods

#The robot dog advances in 18mm steps
dog.move('x',18)

#The robot dog moves to the right with a step size of 5mm
dog.move('y',-5)

#The code in the next sentence can be executed normally, and it will move to the left by 18mm
dog.move('Y',30)

#The robot dog turns left at a speed of 60°/s
dog.turn(60)

#The robot dog moves at a high pace
dog.pace('high')

#The robot dog walks on the spot with a leg height of 25mm
dog.mark_time(25)

#The robot dog stops walking in place
dog.mark_time(0)

#robot dog stops moving
dog.stop()

Based on these methods, a series of methods are encapsulated in the library for easy use.

Method nameIllustrate
move_x(step)Move back and forth, equivalent to move ('x ', step)
move_y(step)Moving left and right, equivalent to move ('y ', step)
forward(step)Forward, equivalent to move ('x ', abs (step))
back(step)Step back, equivalent to move ('x ', - abs (step))
left(step)Left shift, equivalent to move ('y ', abs (step))
right(step)Right shift, equivalent to move ('y ', - abs (step))
turnleft(step)Turn left, equivalent to turn (abs (step))
turnright(step)Turn right, equivalent to turn (- abs (step))

When adjusting the posture, the position of the four legs of the robot dog will not change, but the position or angle of the body will change.

Body position translation

Translation(direction, data)

Parameter NameFormatInput rangeIllustrate
directionSingle character or character list'x', 'y', 'z' or a list containing the above values'x' represents forward and backward translation, 'y' represents left and right translation, and 'z' represents height
dataNumberx:[-35,35],y:[-18,18],z:[75,115]This parameter represents the translation distance of the fuselage position, in millimeters

Body attitude adjustment

attitude(direction, data)

Parameter NameFormatInput rangeIllustrate
directionSingle character or character list'r', 'p', 'y' or a list containing the above values'r' represents roll angle, 'p' represents pitch angle, and 'y' represents yaw angle
dataNumberr:[-20,20],p:[-15,15],y:[11,11]This parameter represents the amplitude of aircraft attitude adjustment, in degrees

Periodic translation of the fuselage

Periodic_tran(direction, period)

The machine dog body will undergo reciprocating translation in the specified period and direction, with an amplitude of half the limit value of position translation, and can perform periodic motion in multiple directions simultaneously. The periodic movement of the fuselage and the movement of the entire machine cannot be carried out simultaneously.

Parameter NameFormatInput rangeIllustrate
directionSingle character or character list'x', 'y', 'z' or a list containing the above values'x' represents forward and backward translation, 'y' represents left and right translation, and 'z' represents height
periodNumber[1.5,8]This parameter represents the motion period, in seconds; Entering 0 represents stopping movement

Periodic rotation of the fuselage

Periodic_rot(direction, period)

Parameter NameFormatinput rangeillustrate
directionSingle character or character listr', 'p', 'y' or a list containing the above values'r' represents roll angle, 'p' represents pitch angle, and 'y' represents yaw angle
periodNumber[1.5,8]This parameter represents the motion period, in seconds; Entering 0 represents stopping movement

Examples of pose related methods

#Forward translation of the fuselage by 18mm
dog.translation('x',18)

#Translate the body to the right by 10mm, and set the height to 100mm
#You can enter a single value or a list to control movement in multiple directions
dog.translation(['y','z'],[-10,100])

#The pitch angle of the fuselage is set to 10 °, the yaw angle is set to -4 °, and the roll angle is set to 8 °
dog.attitude(['p','y','r'],[10,-4,8])

#The body moves back and forth in a cycle of 3 seconds
dog.periodic_tran('x',3)

#The fuselage undergoes rolling angle reciprocating rotation at a period of 3 seconds, and yaw angle reciprocating rotation at a period of 7.4 seconds
dog.periodic_rot(['r','y'],[3,7.4])

#Stop the reciprocating rotation of yaw angle
dog.periodic_rot('y',0)

Set the end position of the robotic arm

Arm( arm_x, arm_z)

Parameter NameFormatInput rangeIllustrate
arm_xfloat[-80, 155]Unit: mm
arm_zfloat[-95, 155]Unit: mm

The coordinates x and z here are relative to the base of the robotic arm, in millimeters.

When the value exceeds the workspace of the robotic arm, the robotic arm will maintain the posture corresponding to the last valid value. For example, the posture corresponding to (155,0) is extended forward to the maximum, (0155) is extended upward to the maximum, and (155155) is inclined upward to the maximum. However, if the robotic arm cannot reach this position, it will maintain the valid position sent last time.

Set the mechanical arm gripper to open and close

Claw(pos)

Parameter NameFormatinput rangeillustrate
posuint_80-2550 corresponds to fully open, 255 corresponds to fully closed

Set whether the robotic arm is in stable mode

Arm_mode(mode)

Parameter NameFormatinput rangeillustrate
modebool0\10 not turned on, 1 turned on

After opening, the end of the robotic arm will not translate with the body's translation (translation refers to the movement of the trunk with four feet standing, rather than walking forward, backward, left, or right).

Other methods

Restore initial state

Reset()

Stop all movements and restore all states to their initial state

Set self stable state

imu(mode)

In a self stable state, the robotic dog will automatically adjust its posture angle to maintain its back in a horizontal position, and cannot be manually set when turned on.

Parameter NameFormatInput rangeIllustrate
modeinteger0, 10 represents off, 1 represents on

Perform(mode)

In performance mode, the robot dog will cycle through the preset actions.

Parameter NameFormatInput rangeIllustrate
modeinteger0, 10 represents off, 1 represents on

Single leg control

leg(leg_id, data)

Control the position feet end of the designated leg(s)

Parameter NameFormatinput rangeillustrate
leg_idinteger1, 2, 3, 4Representing left front leg, right front leg, right rear leg, and left rear leg respectively
dataList of numbers with a length of 3x:[-35,35],y:[-18,18],z:[75,115]Representing left front leg, right front leg, right rear leg, and left rear leg respectively

Examples of other methods

#Set the coordinates of the foot end of the right front leg to(10mm,-12mm,90mm)
dog.leg(2,[10,-12,90])

Servo control

motor(motor_id, data)

Control the rotation angle of the servo

Parameter NameFormatInput rangeIllustrate
motor_idInteger or integer list[11,12,13,21,22,23,31,32,33,41,42,43]The first digit represents the leg where the steering gear is located, and the second digit represents the position on that leg, which is 1, 2, and 3 from bottom to top
dataNumber or number listLower: [-65, 73], Middle: [-66, 93], Upper: [-31, 31]This parameter represents the foot position in degrees

Example of servo

#Set the steering gear rotation angle in the right front leg to 30 °
dog.motor(22,30)

#Set the rotation angle of the steering gear on the right front leg to 10 °, and the rotation angle of the steering gear on the left rear leg to -20 °
dog.motor([23,41],[10,-20])

Single leg servo unloading

Unload_motor(leg_id)

Unload the three servo motors on one leg without outputting torque, and then freely rotate them by hand, usually used for writing actions

Parameter NameFormatInput rangeIllustrate
leg_idinteger1,2,3,4Representing left front leg, right front leg, right rear leg, and left rear leg respectively

All servo unloading

Unload_allmotor()

Unload all servo motors without outputting torque, and rotate them freely by hand

Single leg servo loading

Load_motor(leg_id)

Keep the three servo motors on one leg loaded in their current positions, output torque, and do not rotate them by hand afterwards. Usually used for writing actions

Parameter NameFormatInput rangeIllustrate
leg_idinteger1,2,3,4Representing left front leg, right front leg, right rear leg, and left rear leg respectively

All servo loads

load_allmotor()

Keep all servo motors loaded in their current positions, output torque, and do not rotate them by hand afterwards

Set the rotation speed of the steering gear

motor_speed(speed)

Adjusting the rotation speed of the steering gear is suitable for individual control of the servo

Parameter NameFormatinput rangeillustrate
speedinteger[0,255]0 is the lowest speed, 255 is the highest speed

Modify Bluetooth name

bt_rename(name)

Modify the Bluetooth name again, and Bluetooth will disconnect after calling this function

Parameter NameFormatinput rangeillustrate
namecharacter stringLength not greater than 10The Bluetooth name format of the robot dog is XGO_ Xxx, xxx is a modifiable part that only supports characters in the ascii code.

Execute preset actions

action(action_id)

Parameter NameFormatInput rangeIllustrate
action_idinteger[1,255]The corresponding relationship between ID and action is shown in the table below
IDActionIDActionIDAction
1get down2Stand up3Crawling forward
4circle5marking time6Squat
7Rotate Roll8Rotate Pitch9Rotate Yaw
10Three axis rotation11Pee12Sit down
13Wave14Stretch oneself15Wave
16Swing left and right17Seeking food18Find food
19Handshake20New Year's greetings

Calibrate the position of the steering gear

calibration(state)

If there is a significant position deviation of some joints after startup, this function can be called for calibration. Please use with caution in other situations

Parameter NameFormatInput rangeIllustrate
stateinteger[0,1]1. Enter the calibration state, at which point the servo is unloaded, and then place the robotic dog in the calibration state, with the lower legs parallel to the ground, the thighs at 90 ° to the torso, and the torso parallel to the ground; 0 complete calibration

Reading the servo angle

read_motor()

Read the angles of 12 servo motors. If the reading is successful, a list with a length of 12 will be returned, corresponding to the servo motor angles numbered [11,12,13,21,23,23,31,33,41,42,43]. If the reading fails, an empty list will be returned

Read battery level

read_battery()

Reads the current battery level, returns an integer from 1 to 100 for successful reading, representing the percentage of remaining battery level, and returns 0 for failed reading.

Read attitude angle

read_roll()

read_pitch()

read_yaw()

Read the current attitude angle. If the reading succeeds, it will be a floating point number. If the reading fails, it will return 0

Introduction to Education Library

XGO lite V2 has an education library file xgoedu.py built-in, which can be directly imported and used.

You can also download it here:https://github.com/Xgorobot/XGO-PythonLib

initialization

The following is the initialization code:

#import xgoedu
from xgoedu import XGOEDU
#instantiate edu
edu = XGOEDU()

Screen drawing

You need to kill the self starting main.py process first, otherwise there will be a screen refresh conflict

draw a straight line

lcd_line(x1,y1,x2,y2,color=(r,g,b),width=width)

Parameter NameFormatInput rangeIllustrate
x1,y1,x2,y2digitx1 x2:[0,320] y1 y2:[0,240]X1, y1 is the initial point label x2, and y2 is the endpoint coordinates
Color (can be defaulted)
Default to white
Rgb tupler g b:[0,255]Color is the line color
Width (can be defaulted)
defaults to 2
digitWidth is the line width

Draw an arc with width for the line width

lcd_circle(x1,y1,x2,y2,angle0,angle1,color=(255,255,255),width=2)

Parameter NameFormatInput rangeIllustrate
x1,y1,x2,y2digitx1 x2:[0,320] y1 y2:[0,240]x1,y1,x2,y2 define two points for a given border
angle0,angle1digitangle0 angle1:[0,360]Angle0 is the initial degree, angle1 is the ending angle
Color (can be defaulted)
Default to white
Rgb tupler g b:[0,255]Color is the color of the arc
Width (can be defaulted)
defaults to 2
digitWidth is the width of the arc

Draw a rectangle

lcd_rectangle(x1,y1,x2,y2,fill=None,outline=(255,255,255),width=2)

Parameter NameFormatinput rangeillustrate
x1,y1,x2,y2digitx1 x2:[0,320] y1 y2:[0,240]X1, y1 is the initial point label x2, and y2 is the endpoint coordinates
Fill (can be defaulted)
The default is None
Rgb tupler g b:[0,255]Fill is the fill color, and None is not filled
Outline (can be defaulted)
Default to white
Rgb tupler g b:[0,255]Width is the line color
Width (can be defaulted)
defaults to 2
digitWidth is the line width

Display text

Can display Chinese and English using Microsoft Yahei font, with adjustable font sizelcd_text(x,y,content,color=(255,255,255),,fontsize=15)

Parameter NameFormatInput rangeIllustrate
x,ydigitx y:[0,320]x. y is the initial point marker
contentcharacter stringContent is the displayed content
Color (can be defaulted)
Default to white
Rgb tupler g b:[0,255]Color is the line color
Fontsize (can be defaulted)
The default is 15
digitFontsize is the font size

Display picture

lcd_picture(filename,x=0,y=0)

Parameter NameFormatInput rangeIlustrate
filenamecharacter stringImage file path, please use jpg images
x. Y (can be defaulted)
Default x, y is 0
digitx y:[0,320]x. Y is the initial point marker

Clear screen

lcd_clear()

Key detection

xgoButton(button)

Parameter NameFormatInput rangeReturn value
buttonSpecify string["a","b","c","d"]False not pressed True pressed

Audio function

PLAY AUDIO

xgoSpeaker(filename)

Parameter NameFormatInput rangeIllustrate
filenamecharacter stringAudio file path

Recording audio

xgoAudioRecord(filename="record.wav",seconds=5)

Parameter NameFormatInput rangeIllustrate
Filename (can be defaulted)
The default is "record. wav"
character stringFile name for recording audio
Seconds (can be defaulted)
The default is 5
digitThe length of the recorded file in seconds

Camera function

Turn on the camera

cameraOn(filename="camera")

After calling the API, it will enter camera mode. Press a key to take photos, b key to start recording, b key to stop again, and c key to exit

Parameter NameFormatInput rangeIllustrate
Filename (can be defaulted)
Default "camera"
character stringFile name for taking photos and videos. Save photos in jpg format and videos in mp4 format

AI function

The core function of this series of APIs is to retrieve a frame of image for analysis and return the results. It can be used to detect a single image by passing parameters into the path of the image. To analyze the camera image in real-time, please use it in conjunction with while. The following is the example code:

Gesture recognition single picture:

#input xgoedu
from xgoedu import XGOEDU
#Instantiating edu
edu = XGOEDU()

#Gesture recognition for camera.jpg in the same level directory
result=edu.gestureRecognition("camera.jpg")
#Print recognition results
print(result)

Real time gesture recognition via camera:

#input xgoedu
from xgoedu import XGOEDU
#Instantiating edu
edu = XGOEDU()

#Cycle through camera recognition, press the c key to exit
while True:
result=edu.gestureRecognition() #Default parameter, using camera recognition by default
print(result)
if dog.xgoButton("c"): #Press the c key to exit the cycle
break

Gesture recognition

gesture Recognition(target="camera")

Parameter NameFormatIllustrateReturn value
Target (can be defaulted)
The default "camera"
is to use the camera to capture images
character stringTarget is the path to the image file(ges,(x,y))
GES is the result of gesture recognition
The gestures currently included include:
["1","2","3","4","5","Good","Ok","Rock","Stone"]
Coordinate value xy

YOLO identification

yoloFast(target="camera")

Parameter NameFormatIllustrateReturn value
Target (can be defaulted)
The default "camera"
is to use the camera to capture images
character stringTarget is the path to the image file(object,(x,y))
Object is YOLO recognition result
The objects currently included are:
['person','bicycle','car','motorbike','aeroplane','bus','train','truck','boat','trafficlight','firehydrant','stopsign','parkingmeter','bench','bird','cat','dog','horse','sheep','cow','elephant','bear','zebra','giraffe','backpack','umbrella','handbag','tie','suitcase','frisbee','skis','snowboard','sportsball','kite','baseballbat','baseballglove','skateboard','surfboard','tennisracket','bottle','wineglass','cup','fork','knife','spoon','bowl','banana','apple','sandwich','orange','broccoli','carrot','hotdog','pizza','donut','cake','chair','sofa','pottedplant','bed','diningtable','toilet','tvmonitor','laptop','mouse','remote','keyboard','cell phone','microwave','oven','toaster','sink','refrigerator','book','clock','vase','scissors','teddy bear','hair drier','toothbrush'] 坐标值xy

Face recognition

face_detect(target="camera")

Parameter NameFormatIllustrateReturn value
Target (can be defaulted)
The default "camera"
is to use the camera to capture images
character stringTarget is the path to the image file[x, y, w, h] The
x-coordinate, y-coordinate, width, height of the facial recognition frame

Emotional recognition

emotion(target="camera")

Parameter NameFormatIllustrateReturn value
Target (can be defaulted)
The default "camera"
is to use the camera to capture images
character stringTarget is the path to the image file(emotion,(x,y))
emotion contain:
['Angry','Happy','Neutral','Sad','Surprise']
Coordinate value xy

Age and gender recognition

agesex(target="camera")

Parameter NameFormatIllustrateReturn value
Target (can be defaulted)
The default "camera"
is to use the camera to capture images
character stringTarget is the path to the image file(gender,age,(x,y))
gender contain['Male', 'Female']
age contain['(0-2)', '(4-6)', '(8-12)', '(15-20)', '(25-32)', '(38-43)', '(48-53)', '(60-100)']
Coordinate value x,y