Education Kit
Last updated
Last updated
The Education Kit has all SMD Add-on Modules on three combined 11x19 Plate. It allows the users to interact with all the sensors that is available on the add-on modules and understand their working principle through experience. This kit is ideally created for educational purposes.
Here is the project code of the Education Kit:
from smd.red import *
import os
import sys
from serial.tools.list_ports import comports
from platform import system
from colorama import Fore, Style
from tabulate import tabulate
from random import randint
id = 0
module_id = 1
# Serial Port Define
def USB_Port():
if system() == "Windows":
ports = list(comports())
if ports:
for port, desc, hwid in sorted(ports):
if 'USB Serial Port' in desc:
SMD_Port = port
return SMD_Port
else:
SMD_Port = None
return SMD_Port
elif system() == "Linux":
ports = list(serial.tools.list_ports.comports())
if ports:
for port, desc, hwid in sorted(ports):
if '/dev/ttyUSB' in port:
SMD_Port = port
return SMD_Port
elif system() == "Darwin": # macOS
ports = list(serial.tools.list_ports.comports())
if ports:
for port, desc, hwid in sorted(ports):
if '/dev/tty.usbserial' in port or '/dev/tty.usbmodem' in port:
SMD_Port = port
return SMD_Port
else:
SMD_Port = None
return SMD_Port
port = USB_Port()
m = Master(port)
m.attach(Red(id))
try:
print("Scanning process is starting...")
connected_modules = m.scan_modules(id)
except serial.serialutil.PortNotOpenError:
print("Please check the USB Gateway. If it's plugged, unplug and plug again.")
input("\nPress enter to exit.")
if system() == "Windows":
os.system('cls')
else:
os.system('clear')
print(connected_modules)
def colorize_str(str, boolean):
return f"{Fore.GREEN}{str} found!{Style.RESET_ALL}" if boolean else f"{Fore.RED}{str} not found.{Style.RESET_ALL}"
############### Module Test Functions ###############
########################################################################################################################
## Global variables ##
motor_speed = 0
servo_position = 0
old_pot = 0
new_pot = 0
latency = 0
######################
data = m.get_variables(id, [Index.Button_1, Index.Buzzer_1, Index.Distance_1, Index.IMU_1, Index.Joystick_1, Index.Light_1, Index.Pot_1, Index.RGB_1, Index.Servo_1, Index.QTR_1])
######################
def button_test(check:bool, button_state):
if check:
button_state = m.get_button(id, module_id)
if button_state:
R = randint(0,255)
G = randint(0,255)
B = randint(0,255)
m.set_rgb(id, module_id, R, G, B)
return button_state
else:
return f"{Fore.RED}Not Connected{Style.RESET_ALL}"
def buzzer_test(check:bool, button_state):
if check:
if button_state:
freq = randint(100,4000)
m.set_buzzer(id, module_id, freq)
return f"Buzzer Frequency: {freq} Hz"
else:
return "Button is not pressed"
else:
return f"{Fore.RED}Not Connected{Style.RESET_ALL}"
def distance_test(check:bool, distance):
if check:
if distance > 30:
distance = 0
elif 20 <= distance <= 30 and distance != 0:
R = 0
G = 255
B = 0
m.set_rgb(id, module_id, int(R), int(G), int(B))
m.set_buzzer(id, module_id, 400)
elif 10 <= distance < 20 and distance != 0:
R = 255
G = 55
B = 0
m.set_rgb(id, module_id, int(R), int(G), int(B))
m.set_buzzer(id, module_id, 800)
elif 0 <= distance < 10 and distance != 0:
R = 255
G = 0
B = 0
m.set_rgb(id, module_id, int(R), int(G), int(B))
m.set_buzzer(id, module_id, 1200)
return distance
else:
return f"{Fore.RED}Not Connected{Style.RESET_ALL}"
def imu_test(check:bool, imu):
if check:
return imu
else:
return f"{Fore.RED}Not Connected{Style.RESET_ALL}"
def joystick_test(check:bool, joystick_data):
if check:
global motor_speed
if joystick_data[0] > 10 or joystick_data[0] < -10:
m.set_duty_cycle(id, joystick_data[0])
motor_speed = joystick_data[0]
elif 0 <= joystick_data[0] < 10 or 0 >= joystick_data[0] > -10:
m.set_duty_cycle(id, 0)
motor_speed = joystick_data[0]
return f"X: {joystick_data[0]} Y: {joystick_data[1]} Button: {joystick_data[2]} Motor Speed: {motor_speed}"
else:
return f"{Fore.RED}Not Connected{Style.RESET_ALL}"
def light_test(check:bool, ambient_light):
if check:
ambient_light = m.get_light(id, module_id)
if ambient_light < 150:
m.set_rgb(id, module_id, 255, 255, 255)
elif ambient_light > 300:
m.set_rgb(id, module_id, 0, 0, 0)
return ambient_light
else:
return f"{Fore.RED}Not Connected{Style.RESET_ALL}"
def pot_test(check:bool, pot):
if check:
global old_pot
global new_pot
old_pot = new_pot
potentiometer_data = pot
new_pot = potentiometer_data
delta_pot = abs(new_pot - old_pot)
if potentiometer_data <= 180 and delta_pot > 5:
m.set_servo(id, module_id, potentiometer_data)
elif potentiometer_data > 180 and delta_pot > 5:
m.set_servo(id, module_id, 180)
return potentiometer_data
else:
return f"{Fore.RED}Not Connected{Style.RESET_ALL}"
def rgb_test(check:bool, button_data):
if check:
button_data = m.get_button(id, module_id)
if button_data:
R = randint(0,255)
G = randint(0,255)
B = randint(0,255)
m.set_rgb(id, module_id, R, G, B)
time.sleep(0.02)
return f"R: {R} G: {G} B: {B}"
else:
return "Button is not pressed"
else:
return f"{Fore.RED}Not Connected{Style.RESET_ALL}"
def servo_test(check:bool, imu_data):
if check:
global servo_position
try:
imu_data = imu_data[0]
servo_position = (((imu_data + 90) * 180) / 180)
if imu_data > 5 or imu_data < -5:
m.set_servo(id, module_id, int(servo_position))
except:
pass
return servo_position
else:
return f"{Fore.RED}Not Connected{Style.RESET_ALL}"
def qtr_test(check:bool, qtr_data):
if check:
if qtr_data[0] < 50:
m.set_buzzer(id, module_id, 262) # C4
elif qtr_data[1] < 50:
m.set_buzzer(id, module_id, 294) # D4
elif qtr_data[2] < 50:
m.set_buzzer(id, module_id, 330) # E4
else:
m.set_buzzer(id, module_id, 0) # Empty note
return qtr_data
else:
return f"{Fore.RED}Not Connected{Style.RESET_ALL}"
########################################################################################################################
############### Module Connection Check ###############
########################################################################################################################
BUTTON_check = False
BUZZER_check = False
DISTANCE_check = False
IMU_check = False
JOYSTICK_check = False
LIGHT_check = False
POT_check = False
RGB_check = False
SERVO_check = False
QTR_check = False
try:
if ("Button_" + str(module_id)) in connected_modules:
BUTTON_check = True
if ("Buzzer_" + str(module_id)) in connected_modules:
BUZZER_check = True
if ("Distance_" + str(module_id)) in connected_modules:
DISTANCE_check = True
if ("IMU_" + str(module_id)) in connected_modules:
IMU_check = True
if ("Joystick_" + str(module_id)) in connected_modules:
JOYSTICK_check = True
if ("Light_" + str(module_id)) in connected_modules:
LIGHT_check = True
if ("Pot_" + str(module_id)) in connected_modules:
POT_check = True
if ("RGB_" + str(module_id)) in connected_modules:
RGB_check = True
if ("Servo_" + str(module_id)) in connected_modules:
SERVO_check = True
if ("QTR_" + str(module_id)) in connected_modules:
QTR_check = True
except:
print("\nNO MODULES CONNECTED!\n")
module_check_data = [["Button module", colorize_str("Button module", BUTTON_check)],
["Buzzer module", colorize_str("Buzzer module", BUZZER_check)],
["Distance module", colorize_str("Distance module", DISTANCE_check)],
["IMU module", colorize_str("IMU module", IMU_check)],
["Joystick module", colorize_str("Joystick module", JOYSTICK_check)],
["Light module", colorize_str("Light module", LIGHT_check)],
["Potentiometer module", colorize_str("Pot module", POT_check)],
["RGB module", colorize_str("RGB module", RGB_check)],
["Servo module", colorize_str("Servo module", SERVO_check)],
["QTR module", colorize_str("QTR module", QTR_check)]]
module_check_table = tabulate(module_check_data, headers=['Modules', 'States'], tablefmt="rounded_grid")
print(module_check_table)
########################################################################################################################
test_input = str(input("Press enter to start the test, write 'exit' to close the program: "))
if test_input == "exit":
sys.exit()
else:
m.set_operation_mode(id, 0)
m.enable_torque(id, 1)
pass
while True:
data = m.get_variables(id, [Index.Button_1, Index.Distance_1, Index.IMU_1, Index.Joystick_1, Index.Light_1, Index.Pot_1, Index.QTR_1])
test_result = [ ["Button module", button_test(BUTTON_check, data[0])],
["Buzzer module", buzzer_test(BUZZER_check, data[0])],
["Distance module", distance_test(DISTANCE_check, data[1])],
["IMU module", imu_test(IMU_check, data[2])],
["Joystick module", joystick_test(JOYSTICK_check, data[3])],
["Light module", light_test(LIGHT_check, data[4])],
["Potentiometer module", pot_test(POT_check, data[5])],
["RGB module", rgb_test(RGB_check, data[0])],
["Servo module", servo_test(SERVO_check, data[2])],
["QTR module", qtr_test(QTR_check, data[6])]]
test_table = tabulate(test_result, headers=["Modules", "Results"], tablefmt="rounded_grid")
if system() == "Windows":
os.system('cls')
else:
os.system('clear')
print(test_table)