Thunderborg Autonomous

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's picture

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()

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's picture

You need to store the values returned from the UltrasonicSensor1 and UltrasonicSensor2 functions into variables inside the Movement 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 :)

#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's picture

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:

  1. Check you have the pair of resistors on the echo line connected as shown on the ModMyPi page. Without both of them attached you may have accidentally put 5V into the GPIO pin and damaged it :(
  2. Double check the pin numbers. The code is set to use BCM numbers, for example BCM 20 is here.
  3. Turn off any other scripts which use the GPIO before testing in case they try to control the pins as well.
  4. Make sure the Vcc connection on the sensor is 5V not 3.3V, otherwise the sensor will not work.
  5. Check if there are any errors or warnings printed in the program output.
Subscribe to Comments for &quot;Thunderborg Autonomous&quot;