Here is the Python3 code from the Pi.
It seems the comms are averaging about 6 tenths of a second to parse completely, when unregulated- it tends to push in batches of 3 comms at a time. My math says that at 4800 bps, it should be taking less than 3 tenths of a second. As a result, I have set the program to pause for half a second simply to make the speed of comms more predictable.
Should I bump the rate up to 9600? The plan is to have the xBee’s up to 1000 feet apart, and they are 63mw series 2 Pro’s.
Video of the transmission behavior:
[https://www.youtube.com/watch?v=JFiJ6nF65QI
import serial
from time import sleep
import time
import board
import busio
import adafruit_lsm9ds1
import Adafruit_ADS1x15
import math
#port = serial.Serial("/dev/ttyAMA0", baudrate=9600, timeout=3.0)
#ser = serial.Serial("/dev/ttyACM0", baudrate=4800, timeout=3.0) #USB Serial to Arduino
ser = serial.Serial("/dev/ttyUSB0", baudrate=4800, timeout=3.0) #USB xBee
# I2C connection:
i2c = busio.I2C(board.SCL, board.SDA)
sensor = adafruit_lsm9ds1.LSM9DS1_I2C(i2c)
adc48 = Adafruit_ADS1x15.ADS1115()
adc49 = Adafruit_ADS1x15.ADS1015(address=0x49, busnum=1)
GAIN = 1
incomingCommand = ""
prevPosInString = 0
nextPosInString = 0
joy1X = 0
joy1Y = 0
joy2x = 0
joy2Y = 0
joy3X = 0
joy3Y = 0
sw1 = 0
sw2 = 0
sw3 = 0
sw4 = 0
waitingForContact = 1
sentCommand=""
rangeLF = 0
rangeRF = 0
rangeLR = 0
rangeRR = 0
accelX = 0
accelY = 0
accelZ = 0
speedFactor = 1.0
crabSteer = 0
def driveMotors():
if (sw1 == "1"):
speedFactor = 0.25
# set forward ESC to 25% of received Joy1Y
else:
speedFactor = 1.0
# set forward ESC to received Joy1Y
if (sw2 == "1"):
crabSteer = 1
# set front and rear steer to Joy1X
# this makes all 4 tires point the same direction
else:
crabSteer = 0
# set front steer servo to Joy1X
# set rear steer servo to -1*Joy1X
# this makes tires turn about a single point
def readRangeSensors():
global rangeLF, rangeRF, rangeLR, rangeRR
values48 = [0]*4
values49 = [0]*4
for i in range(4):
values48[i] = adc48.read_adc(i, gain=GAIN)
values49[i] = adc49.read_adc(i, gain=GAIN)
rangeLF = values48[0]
rangeRF = values48[1]
rangeLR = values48[2]
rangeRR = values48[3]
def readAccelerometer():
global accelX, accelY, accelZ
accelX, accelY, accelZ = sensor.acceleration
accelX = round((accelX / 9.8) * 1000)
accelY = round((accelY / 9.8) * 1000)
accelZ = round((accelZ / 9.8) * 1000)
magX, magY, magZ = sensor.magnetic
gyroX, gyroY, gyroZ = sensor.gyro
temp = sensor.temperature
while waitingForContact:
x = ser.read()
print(x)
if ( x.decode() == 'A' ):
ser.write(b'B')
print('B')
waitingForContact = 0
else:
ser.write(b'C')
print('C')
sleep(0.500)
while True:
driveMotors()
readRangeSensors()
readAccelerometer()
if(ser.inWaiting() == 0):
sleep(0.500) #4800 bps = 600 bytes per second @ 42 bytes per comm = 0.071 per comm
while(ser.inWaiting() > 0):
sleep(0.002) #4800 bps = 600 bytes per second @ 42 bytes per comm = 0.071 per comm
incomingCommand = str(ser.readline())
print("in: " + incomingCommand[0:-2])
# print incomingCommand.find("A")
if(incomingCommand.find("A") > -1):
ser.write(b'B')
print('B')
ser.flush()
sleep(0.250)
# incomingCommand = str(ser.readline())
# incomingCommand = incomingCommand[1:-3]
# print("revised1: " + incomingCommand)
if(len(incomingCommand) > 15):
prevPosInString = 0
nextPosInString = incomingCommand.find("{")
incomingCommand = incomingCommand[nextPosInString+1:-6]
prevPosInString = nextPosInString
print("revised: " + incomingCommand)
nextPosInString = incomingCommand.find(",")
# print "index1: %s" % (nextPosInString,)
joy1X = incomingCommand[prevPosInString:nextPosInString]
prevPosInString = nextPosInString
nextPosInString = incomingCommand.find(",",prevPosInString+1)
# print "index2: %s" % (nextPosInString,)
joy1Y = incomingCommand[prevPosInString+1:nextPosInString]
prevPosInString = nextPosInString
nextPosInString = incomingCommand.find(",",prevPosInString+1)
# print "index3: %s" % (nextPosInString,)
joy2X = incomingCommand[prevPosInString+1:nextPosInString]
prevPosInString = nextPosInString
nextPosInString = incomingCommand.find(",",prevPosInString+1)
# print "index4: %s" % (nextPosInString,)
joy2Y = incomingCommand[prevPosInString+1:nextPosInString]
prevPosInString = nextPosInString
nextPosInString = incomingCommand.find(",",prevPosInString+1)
# print "index5: %s" % (nextPosInString,)
joy3X = incomingCommand[prevPosInString+1:nextPosInString]
prevPosInString = nextPosInString
nextPosInString = incomingCommand.find(",",prevPosInString+1)
# print "index6: %s" % (nextPosInString,)
joy3Y = incomingCommand[prevPosInString+1:nextPosInString]
prevPosInString = nextPosInString
nextPosInString = incomingCommand.find(",",prevPosInString+1)
# print "index7: %s" % (nextPosInString,)
sw1 = incomingCommand[prevPosInString+1:nextPosInString]
prevPosInString = nextPosInString
nextPosInString = incomingCommand.find(",",prevPosInString+1)
# print "index8: %s" % (nextPosInString,)
sw2= incomingCommand[prevPosInString+1:nextPosInString]
prevPosInString = nextPosInString
nextPosInString = incomingCommand.find(",",prevPosInString+1)
# print "index9: %s" % (nextPosInString,)
sw3 = incomingCommand[prevPosInString+1:nextPosInString]
prevPosInString = nextPosInString
nextPosInString = incomingCommand.find(",",prevPosInString+1)
# print "index10: %s" % (nextPosInString,)
sw4 = incomingCommand[prevPosInString+1:]
prevPosInString = nextPosInString
# print(joy1X)
# print(joy1Y)
# print(joy2X)
# print(joy2Y)
# print(joy3X)
# print(joy3Y)
# print(sw1)
# print(sw2)
# print(sw3)
# print(sw4)
if (ser.inWaiting() == 0):
sentCommand = "KF5RVR {" + str(rangeLF) + "," + str(rangeRF) + "," + str(rangeLR) + "," + str(rangeRR) + "," + str(accelX) + "," + str(accelY) + "," + str(accelZ) + "}"
sentCommand = sentCommand
ser.write(sentCommand.encode())
print("out: " + sentCommand)
print("")
# End Serial Comms
# End While True
# Start Rest Of Code Here
ser.close()
](https://www.youtube.com/watch?v=JFiJ6nF65QI)