Description: The car was built with two motors controlling the direction and speed of the car. A ball bearing structure is attached to the back for stability. The video includes pictures of the car's mechanical components and the tests are shown at the end. Test one demonstrates a simple case  structure to stop the car at 20 cm. The other test demonstrates the use of a proportional controller.

Proportional Control Code:

#!/usr/bin/env python3 
ev3 = Device("this")
def manageRange(n):
    return max(min(n,1050),-1050)

def main():
    distance = None
    error = None
    kp = None
    power = None
    user_prop_const = None
    text = None

    while True:
        distance = .1 * ev3.UltrasonicSensor('in2').distance_centimeters
        distance = distance * 10
        print(distance)
        kp = 2
        error = distance - 20
        power = error * kp
        if error > 0.1:
            outA = ev3.LargeMotor('outA')
            outA.run_forever(speed_sp= manageRange(power* 10.50))
            outB = ev3.LargeMotor('outB')
            outB.run_forever(speed_sp= manageRange(power* 10.50))
        elif error < -0.1:
            outA = ev3.LargeMotor('outA')
            outA.run_forever(speed_sp= manageRange(power* 10.50))
            outB = ev3.LargeMotor('outB')
            outB.run_forever(speed_sp= manageRange(power* 10.50))
        elif error >= -0.1 or error <= 0.1:
            print('acceptable range has been reached')
            outA = ev3.LargeMotor('outA')
            outA.stop(stop_action="hold")
            outB = ev3.LargeMotor('outB')
            outB.stop(stop_action="hold")
            break
        else:
            break

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

Simple Case Structure Code:

#!/usr/bin/env python3 
ev3 = Device("this")
def manageRange(n):
    return max(min(n,1050),-1050)

def main():
    distance = None

    while True:
        distance = .1 * ev3.UltrasonicSensor('in2').distance_centimeters
        distance = distance * 10
        print(distance)
        if distance > 20.2:
            outA = ev3.LargeMotor('outA')
            outA.run_forever(speed_sp= manageRange(5* 10.50))
            outB = ev3.LargeMotor('outB')
            outB.run_forever(speed_sp= manageRange(5* 10.50))
        elif distance < 19.8:
            outA = ev3.LargeMotor('outA')
            outA.run_forever(speed_sp= manageRange((-5)* 10.50))
            outB = ev3.LargeMotor('outB')
            outB.run_forever(speed_sp= manageRange((-5)* 10.50))
        elif distance >= 19.8 or distance <= 20.2:
            print('acceptable range has been reached')
            outA = ev3.LargeMotor('outA')
            outA.stop(stop_action="hold")
            outB = ev3.LargeMotor('outB')
            outB.stop(stop_action="hold")
            break
        else:
            break

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