Image

Project 3: Trailer Control

Annalisa DeBari & Raven Fournier

Description: We created a car and trailer using the provided acrylic cut piece and attaching it to the EV3 brick using a hinge that allows low friction rotation. For Challenge 1, the our goal is to drive straight (angle = 0) so the error is equal to the angle minus zero. To adjust the direction of the main part of the car, we added or subtracted values from the left and right motors labeled powerA and powerB. For challenge 2, the goal was to follow a set curved line. In this case, the error is the difference in the two gyro sensor readings minus the desired angle. In one test, we set the angle to 20. In test two, the angle was set to 40. These two constants control the radius of the arc.

Reset Gyro Code: (From Website) We used this separately for both gyro sensors before running our code.

#!/usr/bin/python3
from time import sleep

def main():
    print('Program: Reset Gyro')

    # create EV3 device
    ev3 = Device('this')

    print('Step 1\. Setup and Configure Gyro')
    # define gyro sensor
    gyro_sensor = ev3.GyroSensor('in4')
    # set gyro mode to angle
    gyro_sensor.mode = 'GYRO-ANG'

    # read two base values (second apart)
    print('Step 2\. Read Base Values')
    sleep(1)
    print('- value 1: ' + str(gyro_sensor.value()))
    sleep(1)
    print('- value 2: ' + str(gyro_sensor.value()))
    sleep(1)

    # calibrate/reset gyro (by changing modes)
    print('Step 3\. Calibrate/Reset Gyro')
    gyro_sensor.mode = 'GYRO-RATE'
    gyro_sensor.mode = 'GYRO-ANG'

    # read new values (second apart)
    print('Step 4\. Read New Values')
    sleep(1)
    print('- value 3: ' + str(gyro_sensor.value()))
    sleep(1)
    print('- value 4: ' + str(gyro_sensor.value()))

    print('Step 5\. Done')

if __name__ == '__main__':
    main():

Challenge 1: Drive Straight

#!/usr/bin/python3
from time import sleep
def manageRange(n):
    return max(min(n,1050),-1050)

def main():
        # define variables 
    distance = None
    kp = None
    error = None
    power = None
    powerA = None
    powerB = None
    # base is negative to make the motors drive in the correct direction
    base = -80
    print('Program: Read Gyro')

    # create EV3 device
    ev3 = Device('this')

    print('Step 1\. Setup and Configure Gyro')
    # define gyro sensor
    gyro_sensor = ev3.GyroSensor('in4')
    # set gyro mode to angle
    gyro_sensor.mode = 'GYRO-ANG'

    print('Step 2\. Read 10 Gyro Sensor Values')
    while True:
        # read the gyro value (angle)
        angle = gyro_sensor.value()
        # print the gyro value (with some formatting)
        print(str(angle))
                # set prop control value and define error
        kp = 1.5
        error = angle - 0
        print('error is')
        print(str(error))
                # if angle is zero, drive both motors at the same time to stay straight
        if error == 0:
            print('error is zero')
            outA = ev3.LargeMotor('outA')
            outA.run_forever(speed_sp= manageRange(base* 1))
            outB = ev3.LargeMotor('outB')
            outB.run_forever(speed_sp= manageRange(base* 1))
                # if turning, adjust motors to realign 
        elif error > 0:
            print('error is')
            print(str(error))
            # using same addition and subtraction for both positive and negative error
            # for motor A and B because when error is negative, 
            # the double negative removes the need to change the sign
            powerA = base+ angle* kp
            powerB = base- angle* kp
            outA = ev3.LargeMotor('outA')
            outA.run_forever(speed_sp= manageRange(powerA* 1))
            outB = ev3.LargeMotor('outB')
            outB.run_forever(speed_sp= manageRange(powerB* 1))
        elif error < 0:
            print('error is')
            print(str(error))
            powerA = base+ angle* kp
            powerB = base- angle* kp
            outA = ev3.LargeMotor('outA')
            outA.run_forever(speed_sp= manageRange(powerA* 1))
            outB = ev3.LargeMotor('outB')
            outB.run_forever(speed_sp= manageRange(powerB* 1))
        else:
            break

    print('Step 3\. Done')

if __name__ == '__main__':
    main()

Challenge 2: Drive in a curved path set by the different in angle readings

#!/usr/bin/python3
from time import sleep
def manageRange(n):
    return max(min(n,1050),-1050)

def main():
    distance = None
    kp = None
    error = None
    power = None
    powerA = None
    powerB = None
    base = -80
    print('Program: Read Gyro One and Two')

    # create EV3 device
    ev3 = Device('this')

    print('Step 1\. Setup and Configure Gyro')
    # define gyro sensors - same code as before but new sensor added
    gyro_sensor1 = ev3.GyroSensor('in4')
    gyro_sensor2 = ev3.GyroSensor('in2')
    # set gyro mode to angle
    gyro_sensor1.mode = 'GYRO-ANG'
    gyro_sensor2.mode = 'GYRO-ANG'

    print('Step 2\. Read Gyro Sensor Values')
    while True:
        # read the gyro value (angle)
        angle1 = gyro_sensor1.value()
        angle2 = gyro_sensor2.value()
        # print the gyro value (with some formatting)
        print('angle1:' + str(angle1))
        print('angle2:' + str(angle2))
        kp = 1.2
        # for smaller curve, the fixed difference in angle readings is 40
        # for the larger curve, we made the constant = 20
        # changing the constant changes the radius of the arc 
        error = (angle1 - angle2) - 40
        print('error is')
        print(str(error))
        if error == 0:
            print('error is zero')
            outA = ev3.LargeMotor('outA')
            outA.run_forever(speed_sp= manageRange(base* 1))
            outB = ev3.LargeMotor('outB')
            outB.run_forever(speed_sp= manageRange(base* 1))
        elif error > 0:
            print('error is')
            print(str(error))
            powerA = base+ error* kp
            powerB = base- error* kp
            outA = ev3.LargeMotor('outA')
            outA.run_forever(speed_sp= manageRange(powerA* 1))
            outB = ev3.LargeMotor('outB')
            outB.run_forever(speed_sp= manageRange(powerB* 1))
        elif error < 0:
            print('error is')
            print(str(error))
            powerA = base+ error* kp
            powerB = base- error* kp
            outA = ev3.LargeMotor('outA')
            outA.run_forever(speed_sp= manageRange(powerA* 1))
            outB = ev3.LargeMotor('outB')
            outB.run_forever(speed_sp= manageRange(powerB* 1))
        else:
            break

    print('Step 3\. Done')

if __name__ == '__main__':
    main()

:

:

:Challenge 1

Challenge 2a.

Challenge 2b.

Image Image Image Image