Project 1 - Julio Salazar and Khalil Payton

Video

Description

Image

Figure 1 - Plan view of car

Our car has a very simple design, using two motors (ports B and C) and wheels, along with a steel ball caster on the backside of the car to allow for movement. The ultrasonic sensor (input 1) is mounted on the front of the car pointing horizontally outward.

Image Image

Figures 2 and 3 - Rear view and Front view

The basic premise behind our car’s code is that when running, the ultrasonic sensor will continually take readings of the distance of the car to the nearest object in front of it. If this distance is greater than 20 cm, the car will move forward. After it detects that the distance is less than 20 cm, the car breaks and begins to back up. The backing up of the car is controlled by an i counter, starting at i = 0 and adding 1 after every reverse phase until i = 4. We chose a relatively small number as the car only needs to back up a few times to adjust and stop at approximately 20 cm from the wall.

When the car is placed near the wall (less than 20 cm away), the car automatically detects this and begins to back up and adjust to stay approximately 20 cm from the wall.

Code

Image

Code Text

!/usr/bin/env python3

ev3 = Device("this")

from time import sleep

def main():

outC = ev3.LargeMotor('outC')

outB = ev3.LargeMotor('outB')

i = 0

while i < 4:

sensor = ev3.UltrasonicSensor('in1')

distanceCM = 0

distanceCM = sensor.distance_centimeters

if (distanceCM >= 20):

print("The distance in centimeters is:" + str(distanceCM))

outB = ev3.LargeMotor('outB')

outB.run_forever(speed_sp=90)

outC = ev3.LargeMotor('outC')

outC.run_forever(speed_sp=90)

sleep(0.1)

else:

print("The distance in centimeters is:" + str(distanceCM))

print("Reverse Thrusters")

outC = ev3.LargeMotor('outC')

outB = ev3.LargeMotor('outB')

outC.stop(stop_action='brake'); outB.stop(stop_action='brake')

sleep(0.5)

outC.run_forever(speed_sp=-90); outB.run_forever(speed_sp=-90)

sleep(1)

i = i+1

print (i)

outB.stop(stop_action='brake'), outC.stop(stop_action='brake')

if name == 'main':

main()