Thunderborg Autonomous
Forums:
I am trying to set up autonomous driving while using a Thunderborg. How would I call the motors to the code while using thunderborg. I figured I would have to import thunderborg and then call SetMotors. Just confused and was wondering if anyone could help me out. Thanks!!

piborg
Sat, 02/17/2018 - 18:15
Permalink
Simple ThunderBorg code example
This is a stripped down example which should do the minimum needed to setup the ThunderBorg and then command the motors to move for a set period of time:
# Import the time library so we can get a delay
import
time
# Setup the ThunderBorg library ready for use
import
ThunderBorg
# Load the library
TB
=
ThunderBorg.ThunderBorg()
# Create a board object
TB.Init()
# Setup the board
# Now the motors should be controllable by using the TB object
# Run both motors at 50% output
TB.SetMotors(
0.5
)
# Wait for two seconds
time.sleep(
2.0
)
# Stop both motors
TB.MotorsOff()
gmar7
Sat, 02/17/2018 - 19:46
Permalink
So I am using ultrasonic
So I am using ultrasonic sensors to detect if there is an object in a certain amount of distance. I keep getting an error for my code because I am trying to pass the two distance variables to the Movement function to see if it should move forward.
import
time
import
RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
import
ThunderBorg
TB
=
ThunderBorg.ThunderBorg()
TB.Init()
TRIG
=
23
ECHO
=
24
def
UltrasonicSensor2():
TRIG
=
20
ECHO
=
21
GPIO.setup(TRIG,GPIO.OUT)
GPIO.setup(ECHO,GPIO.IN)
GPIO.output(TRIG,
False
)
print
(
'Waiting For Sensor 2 To Settle'
)
time.sleep(
2
)
GPIO.output(TRIG,
True
)
time.sleep(
0.00001
)
GPIO.output(TRIG,
False
)
while
GPIO.
input
(ECHO)
=
=
0
:
pulse_start
=
time.time()
while
GPIO.
input
(ECHO)
=
=
1
:
pulse_end
=
time.time()
pulse_duration
=
pulse_end
-
pulse_start
distance2
=
pulse_duration
*
17150
distance2
=
round
(distance,
2
)
print
(
'Distance for sensor 2:'
), distance, (
'cm'
)
return
distance2
def
UltrasonicSensor1():
GPIO.setup(TRIG,GPIO.OUT)
GPIO.setup(ECHO,GPIO.IN)
GPIO.output(TRIG,
False
)
print
(
'Waiting For Sensor To Settle'
)
time.sleep(
2
)
GPIO.output(TRIG,
True
)
time.sleep(
0.00001
)
GPIO.output(TRIG,
False
)
while
GPIO.
input
(ECHO)
=
=
0
:
pulse_start
=
time.time()
while
GPIO.
input
(ECHO)
=
=
1
:
pulse_end
=
time.time()
pulse_duration
=
pulse_end
-
pulse_start
distance
=
pulse_duration
*
17150
distance
=
round
(distance,
2
)
print
(
'Distance For Sensor 1:'
), distance, (
'cm'
)
return
distance
UltrasonicSensor2()
def
Movement():
UltrasonicSensor1()
if
(distance >
10
and
distance2 >
10
):
while
(distance <
5
and
distance2 <
5
):
TB.SetMotors(
1
)
time.sleep(
2.0
)
TB.MotorsOff()
print
(Distance ,
" cm"
)
elif
(distance <
7
and
distance2 <
7
):
TB.SetMotors(
-
1
)
time.sleep(
2.0
)
TB.MotorsOff()
print
(Distance ,
" cm"
)
piborg
Sun, 02/18/2018 - 11:29
Permalink
Reading values from a different function
You need to store the values returned from the
UltrasonicSensor1
andUltrasonicSensor2
functions into variables inside theMovement
function itself.For example:
def
Movement():
distance
=
UltrasonicSensor1()
distance2
=
UltrasonicSensor2()
if
(distance >
10
and
distance2 >
10
):
while
(distance <
5
and
distance2 <
5
):
TB.SetMotors(
1
)
time.sleep(
2.0
)
TB.MotorsOff()
print
(Distance ,
" cm"
)
elif
(distance <
7
and
distance2 <
7
):
TB.SetMotors(
-
1
)
time.sleep(
2.0
)
TB.MotorsOff()
print
(Distance ,
" cm"
)
On a second point your
while
loop will need to re-run the distance functions each time if you need the values to be updated after each move.Hopefully that helps you get going :)
gmar7
Sat, 02/24/2018 - 17:07
Permalink
Keeps reading sensors but won't go into autonomous
#Here is what I have so far it seems to just stay in the sensor function and doesn't compare the distance
#If anyone has any idea on how to fix this please let me know
import
time
import
RPi.GPIO as GPIO
import
ThunderBorg
GPIO.setmode(GPIO.BCM)
TB
=
ThunderBorg.ThunderBorg()
TB.Init()
def
UltrasonicSensor(TRIG, ECHO, sensorName):
GPIO.setup(TRIG,GPIO.OUT)
GPIO.setup(ECHO,GPIO.IN)
GPIO.output(TRIG,
False
)
print
(
'Waiting For Sensor '
+
sensorName
+
' To Settle'
)
time.sleep(
2
)
GPIO.output(TRIG,
True
)
time.sleep(
0.00001
)
GPIO.output(TRIG,
False
)
while
GPIO.
input
(ECHO)
=
=
0
:
pulse_start
=
time.time()
while
GPIO.
input
(ECHO)
=
=
1
:
pulse_end
=
time.time()
pulse_duration
=
pulse_end
-
pulse_start
distance
=
pulse_duration
*
17150
distance
=
round
(distance,
2
)
print
(
'Distance for sensor '
+
sensorName
+
':'
), distance, (
'cm'
)
return
distance
TRIG_sensor1
=
23
ECHO_sensor1
=
24
TRIG_sensor2
=
20
ECHO_sensor2
=
21
maxDistance
=
16
while
True
:
travel_distance1
=
UltrasonicSensor(TRIG_sensor1, ECHO_sensor1,
'1'
)
travel_distance2
=
UltrasonicSensor(TRIG_sensor2, ECHO_sensor2,
'2'
)
if
travel_distance1 < maxDistance
and
travel_distance2 < maxDistance:
TB.SetMotors(
1
)
## move forward
else
:
TB.MotorsOff()
## stop the car
## go in reverse
piborg
Mon, 02/26/2018 - 13:10
Permalink
Stuck reading sensors
Looking at your code I presume you are following the guide on the ModMyPi Ultrasonic module page.
If you are not seeing any readings printed then it is likely you are not getting a response from sensor #1. If you see one reading then the problem is related to sensor #2.
Here is a quick list of things to check which may be preventing the sensors from working correctly: