Code Verison: 0.7

This code version introduces alot of extra commands that can be used to control MIPR. The idea behind this was to allow people to program MIPR using what ever tools they wanted. With this SDK all you need to do is to connect to MIPR via the HC-05, once connected you can use the commands detailed in the SDK documentation to make MIPR do things and to get readings from MIPR’s sensors.

The GitHub repo contains the SDK documentation that details all commands that can be used in this mode. I have also included some example programs written in Python to help get you started. Although I have written the example programs in Python you can use what ever language you want such as Java, C++, C#, etc.

Some of the examples will use the Python Turtle class to draw how MIPR thinks it is moving. You will notice when running these scripts that the robot will think its moving in a certain direction however the actual direction could be totally different. This is because we’re not using Odometry/Telemetry data in these examples, to have more accurate representation of the world we need to use both Odometry data and data from an IMU or compass. It’s interesting to see how the robot views the space that it is in. In future updates we will improve the Odometry code and add a compass to the robot so we can get accurate direction information.

You can see one of the Python scripts below to see how easy it is to get up and running with this SDK;

# run "pip install pyserial" in the command line to install the serial library
# run "python -m serial.tools.list_ports" to get a list of your serial ports
# run "pip3 install keyboard" to install the keyboard library

#This example implements the basic path finder algorithm found in V0.4 of
#MIPR's base code. Instead of writing the code and uploading it to the
#Arduino we use the SDK and python. All of the timer.sleep() commands gives us our
#system delay which is 52mS. We use this delay because we can get a new reading
#from the VL53L1X every 50mS. You need a delay between sending commands over serial
#If you don't commands will get lost. I use a 1mS delay between sending commands in the main loop.

import serial
import sys
import time
import keyboard

from turtle import *
color('red', 'yellow')
begin_fill()

try:
#SETUP THE SERIAL PORT
ser = serial.Serial('COM11', 57600, timeout=0, parity=serial.PARITY_EVEN, rtscts=0) #connect to the serial port, you might need to change the comm port
time.sleep(3) #wait for MIPR to boot up
msg = ser.read(500) #Read whatever is in the buffer
time.sleep(0.05) #allow the program time to read the buffer

#GET THE OP MODE AND CHANGE TO 9 IF IT IS NOT 9
ser.write(b'GOM') #send the GOM command to get the Op Mode
time.sleep(0.05)
msg = ser.read(500)
msg = msg.decode('utf-8')
intMsg = int(msg)
if intMsg != 9: #If the Op Mode is not 9 the change to 9
print('Changing to SDK; Op Mode 9')
time.sleep(0.05)
ser.write(b'O') #Change MIPR's mode
time.sleep(1)
ser.write(b'9') #Put into Op Mode 9 SDK
time.sleep(0.05)

dirCounter = 0 #Used to decide which direction to turn

#SETUP VARIOUS VALUES ON MIPR
ser.write(b'SPD:96') #Set the speed to 200
time.sleep(0.05)
ser.write(b'SETSEN') #Start the VL53L1X
time.sleep(0.05)
ser.write(b'MAXD:4000') #Set the VL53L1X max range to 4 meters
time.sleep(0.05)

print('Basic Path Finding Algorithm Executing type S to stop')

#GET A DISTANCE MEASUREMENT AND ACT ACCORDINGLY
while True: #Loop forever
time.sleep(0.025)
ser.write(b'DIST') #Get a measurement
msg = ser.read(500) ##read the message from the serial buffer
time.sleep(0.025)
msg = msg.decode('utf-8') ##decode the message
if keyboard.is_pressed('S') or keyboard.is_pressed('s'):
ser.write(b'S') #Stop MIPR
sys.exit()
if msg != "": #Only print the message if it contains something
#print(msg, end ='') #uncomment to print measurements; use this for debugging if you have issues
try:
intMsg = int(msg)

if intMsg > 500:
ser.write(b'SPD:96') #Set the speed to 200
time.sleep(0.01)
ser.write(b'F')
forward(3)

if intMsg < 500:

if dirCounter < 21:
dirCounter = dirCounter + 1
ser.write(b'SPD:64') #Set the speed to 64, we must corner slowly due to the vl53l1x refresh rate
time.sleep(0.01)
ser.write(b'R')
right(16)

if dirCounter > 20:
dirCounter = dirCounter + 1
ser.write(b'SPD:64') #Set the speed to 64, we must corner slowly due to the vl53l1x refresh rate
time.sleep(0.01)
ser.write(b'L')
left(16)

if dirCounter > 40:
dirCounter = 0
except:
print('Basic Path Finding Algorithm Executing type S to stop')
except:
print('Can not connect please check the COMM port and ensure that MIPR is switched on')

When writing code for real time systems I would usually say not to use delays such as time.sleep() as the program will be non-responsive for that amount of time however, when writing these Python scripts it is fine to use these as we’re requesting data from the robot at a relatively slow rate and in between updates we’re just waiting for the next bit of data to act upon anyway. The delays are very important in this context because if we send or request data too quickly we will overload the buffer and data will be lost, remember that we’re operating at 57600 baud which is 57600 bit per second or 7,200 KBytes per second which is very slow.

Resources