DIYGO Python Library
The DIYGO Python Library is a library that helps you control various circuit components of DIYGO in a Python environment.
You can install and use a library named RobokitRS in a Python environment.
https://pypi.org/project/RobokitRS/
▶Installation and deletion
Installation
The command below will install RobokitRS.
pip3 install RobokitRS
A pyserial library is required for Serial communication, and you can run the following commands to proceed with the library installation.
pip3 install pyserial
Upgrade
When you upgrade your library to the latest version, run the following commands.
pip3 install --upgrade RobokitRS
Deletion
The following command deletes the RobokitRS library.
pip3 uninstall RobokitRS
▶System
Modes
The types of pin modes on the DIYGO CPU board are as follows.
It supports digital and analog, respectively, and analog modes are subdivided according to the type of sensor.
class Modes(enum.enum):
INPUT = 0x00
OUTPUT = 0x01
ANALOG = 0x02
PWM = 0x03
SERVO = 0x04
I2C = 0x05
TONE = 0x0A
SONAR = 0x0B
RGBLED = 0x0E
Sensor Type
The type of analog sensor that connects to the DIYGO CPU board.
Use to read the value of an analog sensor that is not distinguished by pin mode.
class SensorType(enum.Enum):
TEMPERATURE = 0
JOYSTICK_X = 1
JOYSTICK_Y = 2
LIGHT = 3
DIAL = 4
A_KEYPAD = 5
ROTARYPOSITION = 6
MAGNETIC = 7
SONAR = 8
Gyro Data Type
The data type of the gyro sensor.
It is used to read seven types of data values that can be recognized by gyro sensors.
class GyroDataType(enum.Enum):
ANGLE_X = 0
ANGLE_Y = 1
ANGLE_Z = 2
GYRO_X = 3
GYRO_Y = 4
GYRO_Z = 5
SHAKE = 6
Rotary Position Data Type
This is the data type of the rotation position sensor.
It is used to read three types of data values that can be recognized by the rotation position sensor.
class RotaryPositionDataType(enum.Enum):
ROTATION = 0
POSITION = 1
ANGLE = 2
Two Wheels Direction Type
This is the type of driving direction of a two-wheeled robot using two DC motors.
Used to drive in that direction.
When wired in accordance with the rules set in the motor drive board mounted on the CPU board, it operates correctly.
class TwoWheelsDirectionType(enum.Enum):
FORWARD = 0
BACKWARD = 1
LEFTWARD = 2
RIGHTWARD = 3
STOP = 4
▶Robokit RS Class
The creators of the RobokitRS class are as follows.
def __init__(self):
It does not have a separate factor, and when you create an object, the default pin mode setting and communication thread are automatically turned on.
In addition, the public function of the RobokitRS class is organized as follows.
Setting Function
1. port_open( )
Name
: port_open(portname:str)
Explanation
Open the serial port named portname, which has a communication speed of 115200, and additional communication threads will be turned on.
Port name
Port Name that is composed of 'COM' and numbers
example
port_open("COM3")
2. set_pin_mode( )
Name
: set_pin_mode(pin:int, mode:RobokitRS.Modes)
Explanation
Set pin usage to mode. Sensor and module must be set up before use.
pin
min : 2, max : 19
mode
Refer to: Modes
example
set_pin_mode(3,RobokitRS.Modes.OUTPUT)
Digital&Analog Port Control Function
1. analog_read( )
Name
: analog_read(pin:int)->int
Explanation
Receives the analog sensor value associated with the pin number.
pin
min : 0, max : 5
example
value = analog_read(3)
2. analog_reads( )
Name
: analog_reads(pins:list)->list
Explanation
Receives the analog sensor value associated with the pin number.
pins
min : 0, max : 5 list
example
value = []
value = analog_reads([3,4,5])
3. analog_write( )
Name
: analog_write(pin:int, value:int)
Explanation
Input value in the Pin
pin
min : 0, max : 5
value
min : 0, max : 255
example
analog_write(3,200)
4.digital_read( )
Name
: digital_read(pin:int)->int
Explanation
Receives the value of the digital sensor connected to the pin number.
pin
min : 2, max : 13
example
value = digital_read(2)
5. digital_reads( )
Name
: digital_reads(pins:list)->list
Explanation
Receives the value of the digital sensor connected to the pin number.
pins
min : 2, max : 13 List
example
value = []
value = digital_reads([3,4,5])
6. digital_write( )
Name
: digital_write(pin:int, value:int)
Explanation
Input value in the Pin
pin
min : 2, max : 13
value
0 or 1
example
digital_write(2,0)
7. sensor_read( )
Name
: sensor_read(pin:int, type:RobokitRS.SensorType)
Explanation
Calculate and receive the value of the analog sensor connected to the pin number according to the type.
pin
min : 0, max : 5
type
Standard: : SensorType.TEMPERATURE
Refer to: SensorType
example
value = sensor_read(3,RobokitRS.SensorType.TEMPERATURE)
Motor Control Function
1. motor_write( )
Name
: motor_write(motorid:int, direction:int, speed:int)
Explanation
A function that controls the DC motor. Rotates the motor connected to the motor at speed in the direction.
You can control the motor wired with the correct rules on the motor driveboard of the CPU board.
motorid
Motor1 : 0 (pin 4,5)
Motor2 : 1 (pin 6,7)
Motor3 : 2 (pin 8,9)
Motor4 : 3 (pin 10,11)
direction
CW : 0 , CCW : 1
Default: CW
speed
min : 0, max : 15
Default: 15
example
motor_write(0, 1, 15)
# Turn motor1 counterclockwise at a speed of 15
2. motor_stop( )
Name
: motor_stop(motorid:int)
Explanation
Function that controls the DC motor. Stop the motor connected to the motor.
You can control the motor wired with the correct rules on the motor drive board of the CPU board.
motorid
Motor1 : 0 (pin 4,5)
Motor2 : 1 (pin 6,7)
Motor3 : 2 (pin 8,9)
Motor4 : 3 (pin 10,11)
example
motor_stop(0)
# Stops motor1
3. set_twowheels_direction( )
Name
: set_twowheels_direction(first_id:int,second_id:int,first_speed:int=15,second_speed:int=15,direction:RobokitRS.TwoWheelsDirectionType)
Explanation
A function that drives a two-wheel drive robot using two DC motors toward direction at speed.
You can control the motor wired with the correct rules on the motor drive board of the CPU board.
first_id, second_id
Motor1 : 0 (pin 4,5)
Motor2 : 1 (pin 6,7)
Motor3 : 2 (pin 8,9)
Motor4 : 3 (pin 10,11)
first_speed, second_speed
min : 0, max : 15
Default: 15
direction
Indicates the direction of the 2-wheel robot. Refer to: TwoWheelsDirectionType
example
set_twowheels_direction(0, 1, 15, 15, RobokitRS.TwoWheelsDirectionType.FORWARD)
sleep(1)
set_twowheels_direction(0, 1, 15, 15, RobokitRS.TwoWheelsDirectionType.RIGHTWARD)
sleep(1)
set_twowheels_direction(0, 1, 0, 0, RobokitRS.TwoWheelsDirectionType.STOP)
# Turn the two-wheel drive robot using motor1 and motor2 straight for 1 second at a speed of 15 and then to the right for 1 second.
Mecanum Wheel Control Function
A function to control a four-wheel drive robot equipped with a mechanical wheel.
Four DC motors must be wired to the two motor drive boards on the CPU board with the correct rules.
1. set_mecanumwheels_drive_front( )
Name
: set_mecanumwheels_drive_front(speed:int, motortype:int)
Explanation
It is a function that distinguishes a four-wheel drive robot using a mechanical wheel by motor type and drives forward by speed.
speed
min : 0, max : 15
Default : 15
motortype
DC motor : 1, Smart motor : 2
Default : 1
example
set_mecanumwheels_drive_front(15,1)
# Advance a four-wheel drive robot equipped with a mechanical wheel at a speed of 15.
2. set_mecanumwheels_drive_back( )
Name
: set_mecanumwheels_drive_back(speed:int, motortype:int)
Explanation
It is a function of distinguishing a four-wheel drive robot using a mechanical wheel by motor type and driving backward by speed.
speed
min : 0, max : 15
Default : 15
motortype
DC motor : 1, Smart motor : 2
Default: 1
example
set_mecanumwheels_drive_back(15,1)
# Reverse the four-wheel drive robot equipped with a mechanical wheel at a speed of 15.
3. set_mecanumwheels_drive_right( )
Name
: set_mecanumwheels_drive_right(speed:int, motortype:int)
Explanation
It is a function that distinguishes a four-wheel drive robot using a mechanical wheel by motor type and drives it in parallel in the right direction (→) by speed.
Speed
min : 0, max : 15
Default: 15
motortype
DC motor : 1, Smart motor: 2
Default : 1
example
set_mecanumwheels_drive_right(15,1)
# Drive a four-wheel drive robot equipped with a mechanical wheel to the right (parallel drive) at a speed of 15.
4. set_mecanumwheels_drive_left( )
Name
: set_mecanumwheels_drive_left(speed:int, motortype:int)
Explanation
It is a function that distinguishes a four-wheel drive robot using a mechanical wheel by motor type and runs parallel to the left (←) by speed.
speed
min : 0, max : 15
Default : 15
motortype
DC motor : 1, Smart motor : 2
Default : 1
example
set_mecanumwheels_drive_left(15,1)
# Drive a four-wheel drive robot equipped with a mechanical wheel to the left (parallel drive) at a speed of 15.
5. set_mecanumwheels_drive_frontright( )
Name
: set_mecanumwheels_drive_frontright(speed:int, motortype:int)
Explanation
A function that distinguishes a four-wheel drive robot using a mechanical wheel by motor type and drives it in the forward right diagonal direction (↗) by speed.
speed
min : 0, max : 15
Default : 15
motortype
DC motor : 1, Smart motor : 2
Default : 1
example
set_mecanumwheels_drive_frontright(15,1)
# A four-wheel drive robot equipped with a mechanical wheel is driven diagonally to the front right at a speed of 15.
6. set_mecanumwheels_drive_frontleft( )
Name
: set_mecanumwheels_drive_frontleft(speed:int, motortype:int)
Explanation
A function that distinguishes a four-wheel drive robot using a mechanical wheel from a motor type and drives it in the front left diagonal direction (↖) as much as the speed.
speed
min : 0, max : 15
Default : 15
motortype
DC motor : 1, Smart motor : 2
Default : 1
example
set_mecanumwheels_drive_frontleft(15,1)
# A four-wheel drive robot equipped with a mechanical wheel is driven diagonally to the front left at a speed of 15.
7. set_mecanumwheels_drive_backright( )
Name
: set_mecanumwheels_drive_backright(speed:int, motortype:int)
Explanation
A function that distinguishes a four-wheel drive robot using a mechanical wheel by motor type and drives it in the right diagonal direction (↘) rear by speed.
speed
min : 0, max : 15
Default : 15
motortype
DC motor : 1, Smart motor : 2
Default : 1
example
set_mecanumwheels_drive_backright(15,1)
# A four-wheel drive robot equipped with a mechanical wheel is driven diagonally to the right of the rear at a speed of 15.
8. set_mecanumwheels_drive_backleft( )
Name
: set_mecanumwheels_drive_backleft(speed:int, motortype:int)
Explanation
A function that distinguishes a four-wheel drive robot using a mechanical wheel by motor type and drives it in the rear left diagonal direction (↙) by speed.
speed
min : 0, max : 15
Default : 15
motortype
DC motor : 1, Smart motor : 2
Default : 1
example
set_mecanumwheels_drive_backright(15,1)
# A four-wheel drive robot equipped with a mechanical wheel is driven diagonally to the rear left at a speed of 15.
9. set_mecanumwheels_drive_stop( )
Name
: set_mecanumwheels_drive_stop(motortype:int)
Explanation
It is a function that distinguishes and stops a four-wheel drive robot using a mechanical wheel by motor type.
motortype
DC motor : 1, Smart motor : 2
Default : 1
example
set_mecanumwheels_drive_stop(1)
# Stop the four-wheel drive robot equipped with a mechanical wheel.
10. set_mecanumwheels_rotate_right( )
Name
: set_mecanumwheels_rotate_right(speed:int, motortype:int)
Explanation
It is a function that distinguishes a four-wheel drive robot using a mechanical wheel by motor type and rotates clockwise by speed.
speed
min : 0, max : 15
Default: 15
motortype
DC motor : 1, Smart motor : 2
Default: 1
example
set_mecanumwheels_rotate_right(15,1)
# Rotates a four-wheel drive robot equipped with a mechanical wheel clockwise at a speed of 15.
11. set_mecanumwheels_rotate_left( )
Name
: set_mecanumwheels_rotate_left(speed:int, motortype:int)
Explanation
It is a function that distinguishes a four-wheel drive robot using a mechanical wheel into a motor type and rotates counterclockwise by speed.
speed
min : 0, max : 15
Default : 15
motortype
DC motor : 1, Smart motor : 2
Default : 1
example
set_mecanumwheels_rotate_left(15,1)
# Turn a four-wheel drive robot equipped with a mechanical wheel counterclockwise at a speed of 15.
Servo Motor Control Function
servo_write( )
Name
: servo_write(pin:int, position:int)
Explanation
A function that controls the servomotor. Move the servomotor connected to the pim to the position position.
id
min : 2, max : 13
position
min : 2, max : 13
example
servo_write(4,20)
# Move the servomotor connected to port 4 to the 20 degree position
RGB LED Control Function
Functions to control the color and brightness of RGB LEDs.
1. set_rgb_led_color( )
Name
: set_rgb_led_color(pin:int, red:int, green:int, blue:int)
Explanation
A function that determines the color of the RGB LED connected to the pin number.
It has a method of determining the color by mixing red, green, and blue lighting values.
pin
min : 2, max : 13
red, green, blue
min : 0, max : 100
example
set_rgb_led_color(2, 100, 100, 100)
#Set the RGB LED connected to port 2 to white.
2. set_rgb_led_red( )
Name
: set_rgb_led_red(pin:int)
Explanation
: A function that determines the color of the RGB LED associated with the pin number to be red.
3. set_rgb_led_orange( )
Name
: set_rgb_led_orange(pin:int)
Explanation
: A function that determines the color of the RGB LED connected to the pin number to be orange.
4. set_rgb_led_yellow( )
Name
: set_rgb_led_yellow(pin:int)
Explanation
: A function that determines the color of the RGB LED connected to the pin number to be Yellow.
5. set_rgb_led_green( )
Name
: set_rgb_led_green(pin:int)
Explanation
: A function that determines the color of the RGB LED connected to the pin number to be Green.
6. set_rgb_led_sky( )
Name
: set_rgb_led_sky(pin:int)
Explanation
: A function that determines the color of the RGB LED connected to the pin number to be Sky Blue.
7. set_rgb_led_navy( )
Name
: set_rgb_led_navy(pin:int)
Explanation
: A function that determines the color of the RGB LED connected to the pin number to be Navy.
8. set_rgb_led_purple( )
Name
: set_rgb_led_purple(pin:int)
Explanation
: A function that determines the color of the RGB LED connected to the pin number to be Purple
9. set_rgb_led_pink( )
Name
: set_rgb_led_pink(pin:int)
Explanation
: A function that determines the color of the RGB LED connected to the pin number to be Pink.
10. set_rgb_led_white( )
Name
: set_rgb_led_white(pin:int)
Explanation
: A function that determines the color of the RGB LED connected to the pin number to be White.
9. set_rgb_led_random( )
Name
: set_rgb_led_random(pin:int)
Explanation:
A function that determines the color of the RGB LED connected to the pin number to be random.
11. set_rgb_led_brightness( )
Name
: set_rgb_led_brightness(pin:int, brightness:int)
Explanation
A function that sets the brightness of the RGB LED connected to the pin number to brightness.
pin
min : 2, max : 13
brightness
Indicates the degree of brightness.
Unit : Percentage (%)
min : 0, max : 100
Default : 100
example
set_rgb_led_brightness(2, 50)
#Set the brightness of the RGB LED connected to port 2 to 50%.
12. change_rgb_led_brightness( )
Name
: change_rgb_led_brightness(pin:int, brightness:int)
Explanation
A function that changes the brightness of the RGB LED connected to the pin number by brightness.
pin
min : 2, max : 13
brightness
The brightness level increases or decreases with the value entered.
Unit : Percentage (%)
min : 0, max : 100
Default: 10
example
change_rgb_led_brightness(2, -20)
#Indicates the brightness of the RGB LED connected to port 2 to be 20 darker than the current state.
change_rgb_led_brightness(4, 50)
#Express the brightness of the RGB LED connected to port 4 by 50 brighter than the current state.
13. set_rgb_led_on( )
Name
: set_rgb_led_on(pin:int)
Explanation
Function to turn on RGB LED associated with pin number.
Color data settings must be preceded before RGB LED is turned on.
pin
min : 2, max : 13
example
set_rgb_led_color(2, 0, 100, 0)
#Set the color of the RGB LED connected to port 2 to green.
set_rgb_led_on(2)
#Turn on the RGB LED connected to port 2 (green)
Piezo Buzzer control Function
A control function for setting the scale from the piezo and turning it on and off.
1. tone( )
Name
: tone(pin:int, note:int, duration:int)
Description
A function that causes the piezoelectric buzzer connected to the pin number to sound during duration on the note scale.
pin
min : 2, max : 13
note
min : 0, max : 95
A parameter that represents the scale.
duration
Unit: milliseconds (ms), Default: 500
Example
tone(2,75,170)
#Sound the Piezo-booster connected to port 2 for 170 ms on the 75th scale.
2. tone_with_delay( )
Name
: tone_with_delay(pin:int, note:int, duration:int)
Description
A function that causes the piezoelectric buzzer connected to the pin number to sound and wait for the duration on the note scale.
Run all of these actions, then run the following command.
pin
min : 2, max : 13
note
min : 0, max : 95
A parameter that represents the scale.
duration
Unit: milliseconds (ms), Default: 500
example
tone_with_delay(2,75,170)
tone_with_delay(2,78,204)
tone_with_delay(2,82,255)
tone_with_delay(2,85,291)
#Play in order for 170ms on 75th scale, 204ms on 78th scale, 255ms on 82nd scale, and 291ms on 85th scale with a piezo-buzzer connected to port 2
3. tone_do( )
name
: tone_do(pin:int, duration:int)
Explanation
: A function that allows the piezoelectric buzzer connected to the pin number to be heard during duration on a 'do' scale.
4. tone_re( )
name
: tone_re(pin:int, duration:int)
Explanation
: A function that allows the piezoelectric buzzer connected to the pin number to be heard during duration on a 'Re' scale.
5. tone_mi( )
name
: tone_mi(pin:int, duration:int)
Explanation
: A function that allows the piezoelectric buzzer connected to the pin number to be heard during duration on a 'Mi' scale.
6. tone_fa( )
name
: tone_fa(pin:int, duration:int)
Explanation
: A function that allows the piezoelectric buzzer connected to the pin number to be heard during duration on a 'fa' scale.
7. tone_sol( )
name
: tone_sol(pin:int, duration:int)
: A function that allows the piezoelectric buzzer connected to the pin number to be heard during duration on a 'Sol' scale.
8. tone_la( )
name
: tone_la(pin:int, duration:int)
Explanation
: A function that allows the piezoelectric buzzer connected to the pin number to be heard during duration on a 'La' scale.
9. tone_si( )
Name
: tone_si(pin:int, duration:int)
Explanation
: A function that allows the piezoelectric buzzer connected to the pin number to be heard during duration on a 'si' scale.
Gyro Sensor Related Functions
A function that can be used to obtain tilt-related data of robot/structure to which the sensor is attached using gyro sensors.
1. gyro_begin( )
Name
: gyro_begin()
Explanation
A function that activates a gyro sensor to collect data.
The gyro sensor must have both pins properly connected to the DIYGO CPU's I2C port.
2. set_gyro_default_position( )
Name
: set_gyro_default_position(position:int)
Explanation
A function that sets the default position of the gyro sensor to position.
The gyro sensor must have both pins properly connected to the DIYGO CPU's I2C port.
position
Up : 0 (Default)
Front : 1
Right : 2
Back : 3
Left : 4
3. gyro_read( )
Name
: gyro_read(type:RobokitRS.GyroDataType)
Explanation
A function that returns data that meets the type of stored gyro sensor data.
The gyro sensor must have both pins properly connected to the DIYGO CPU's I2C port.
type
It consists of Angle (X, Y, Z), Gyro (X, Y, Z), and Shake. See GyroDataType
Rotation Location Sensor Related Function
It is a function that can secure external environment data input value data with a rotation position sensor.
1. rotary_position_read( )
Name
: rotary_position_read(pin:int, type:RobokitRS.RotaryPositionDataType)
Explanation
A function that returns data corresponding to the type of rotation position sensor connected to the pin number.
pin
min :0, max : 5
type
Indicates the type of data the rotation position sensor has, see RotaryPositionDataType
2. reset_rotary_position( )
Name
: reset_rotary_position(pin:int, type:RobokitRS.RotatyPositionDataTypevalue:int)
Explanation
A function that determines the data corresponding to the type of rotation position sensor connected to the pin number as value.
pin
min :0, max : 5
type
Indicates the type of data the rotation position sensor has, see RotaryPositionDataType.
value
The value to change the selected data for the rotation position sensor.
Ultrasonic Sensor Related Functions
This function can be used when detecting external environment data with an ultrasonic sensor.
1. sonar_begin( )
Name
: sonar_begin(pin:int)
Explanation
It is a function that operates an ultrasonic sensor connected to the pin number to collect data.
pin
min : 2, max : 13
example
sonar_begin(2)
#Turn on the ultrasonic sensor connected to digital port 2.
2. sonar_read( )
Name
: sonar_read(pin:int)
Explanation
A function that returns the data detected by the ultrasonic sensor connected to the pin number.
The data returned is the distance (cm) data between the ultrasonic sensor and the detected object.
pin
min : 2, max : 13
example
value = sonar_read(2)
#Store the detected value of the ultrasonic sensor connected to the digital port 2 in the value.