Notifications
Clear all

Pi4 + Crickit Robotics controller board analogue input help

1 Posts
1 Users
0 Likes
569 Views
(@td4dotnet)
Member
Joined: 4 years ago
Posts: 1
Topic starter  

Dear All

I'm working a small robotics project where I 3D printed a chassis and want to use it as a base platform for experimenting with input devices (sensors / switches etc)

I am pretty new to coding for robotics so I thought it would be a good idea to use the Crickit board to handle the basic functions of my robot. Initially I got the Robot to avoid obstacles by setting up an Arduino Mini with an HC-SR04 to output the range value to the serial console which I then parsed and fed into the Pi4 serial monitor which in turn would set the values on the Crickit board to stop my motors and change the colour of a NeoPixel strip. This worked but it would go wrong on me quite a lot...

So being faced with this issue I thought I would use the Analogue inputs on the Crickit board to interpret the sensor data but I have reached a stumbling point. Due to my inexperience and my inability to find any examples online that used the hardware combination I have I am now unsure how to set up the HC-SR04 on this esoteric Crickit board.

This is the link to the Crickit board, I tried to wire it up but I'm getting out of my depth and I don't want to damage anything...

https://www.adafruit.com/product/3957

The documentation speaks of the analogue input and I have read all I can but I don't know how to set up my code so that it reads the input data from it's own onboard ports, of which I have 8 to work with.

Here is my shambling attempt at sensor input, I'd like to alter this to use the onboard analogue inputs on the Crickit to receive sensor data from the HC-SR04, I feel once I have this ability then I can use it to further my knowledge which is severely lacking in this area 🙁 

Below is how I had the thing running, there's a lot of serial stuff in here that won't be needed, unless I've somehow stumbled upon a method which other people use but I have incorrectly implemented...

#R0B0T C0D3!
#
from adafruit_crickit import crickit
import time
import serial
import asyncio
crickit.init_neopixel(8)

## --- Serial Stuff -- ##
ser = serial.Serial('/dev/ttyUSB0')
inputdata = [0,0] # data which is currently being parsed. we have this because it is not yet a full set of readings which will cause the program to cake its trousers.
outputdata = [0,0] # last set of completed readings
counter = 0
buffer = "" # temporary input buffer

## -- Motor stuff -- ##
motorleftspeed = 0 # Variable to keep track of how fast each motor is going
motorrightspeed = 0

leftmotor = crickit.dc_motor_1 #Just tidying up the names of the motors here
rightmotor = crickit.dc_motor_2

def runmotors(lspeed,rspeed): #function which we use to control the speed of the left and right motors
if (lspeed != motorleftspeed):
leftmotor.throttle = lspeed
motorlspeed = lspeed
if (rspeed != motorrightspeed):
rightmotor.throttle = rspeed
motorrightspeed = rspeed

if lspeed and rspeed == 0:
stopped = True
else:
stopped = False

#stuff that happens when we quit
def quitclean():
crickit.neopixel.fill((0, 0 , 0))
ser.close()
exit()

## -- Turn around bits --

stopped = False # how we know if the robot is moving or not
laststoptime = time.time() # Reference timestamp for turnaround function

def turnaround():
turnaroundtime = time.time() + 10000 # what time we should stop turning around (ms)
while time.time() <= turnaroundtime:
runmotors(-.7,.7) # rotate

## -- Main While Loop -- ##
try:
while True: # at some point need to make this asyncronous so it can do this in the background while it's thinking about other stuff.

tmpbuf = ser.read()
buffer += tmpbuf.decode('utf-8')

if ":" in buffer: #if we find a : in our buffer we know that it is the end of that reading

# print(buffer)
currentdata = buffer #set the current data we're working on to what we have in the buffer
currentdata = currentdata.replace(":", "") #remove the : in the buffer so we can convert it into an integer
currentdata = int(currentdata) #make it in to an integer
inputdata[counter] = currentdata #add what we have in the buffer into our "inputdata" array
counter += 1

buffer = "" # clear the buffer

elif ";" in buffer: # if we find a ; in the buffer, we know it is the end of the road!

currentdata = buffer #set the current data we're working on to what we have in the buffer
currentdata = currentdata.replace(";", "") #remove the : in the buffer so we can convert it into an integer
currentdata = int(currentdata) #make it in to an integer
inputdata[counter] = currentdata #add what we have in the buffer into our "inputdata" array

outputdata = inputdata # we can now set the "outputdata" array to be the same as the "inputdata" as we know it is a complete set
buffer = "" # clear the buffer so we are ready to start again!
counter = 0

# Begin Motor Function Control
# Distance is measured in MM

if outputdata[0] > 200:
runmotor(.5,.5) # set both motors to .5 speed
crickit.neopixel.fill((0, 255, 0))
print("CHOOCH FAST")
else:
if outputdata[0] < 100:
runmotor(0,0)
crickit.neopixel.fill((255, 0, 0))
print("NO CHOOCH")

## Turnaround logic starts here
now = time.time()
waittime = 5000 # How long we wait before giving up and turning around in ms
if (!stopped):
laststoptime = now # if this is the first time we've received command yet we can let the program now we are stopped now

if (now - laststoptime) > waittime:
turnaround() ## if we have waited longer than the amount of patients the robot has we should start turning around

else:
runmotor(.3,.3)
crickit.neopixel.fill((50, 50, 50))
print("CHOOCH SLOW")

except KeyboardInterrupt:
quitclean()

#eof

If anyone has experience in this area or can point me at a reference guide I've so far failed to find I would be very grateful as I feel like I've fallen down a rabbit's hole where I've ended up stuck and unsure as to what to look at first.


   
Quote