def main(): """ Calls the TEST functions in this module. """ # The simulator does NOT simulate IR. Use a REAL robot to test! port = 'sim' robot = new_create.Create(port) # Attach the RED wire of the "hat" device to either of the two # +5V holes. Attach the BLACK wire to the LD1 hole. Maybe turn # off the overhead lights. The little red light on the device # blinks when it is sending a signal. # To SEND an IR signal, use the startIR method (NOT sendIR): number = 57 # IR signals can be between 0 and 254 robot.startIR(number) # To RECEIVE an IR signal, use the usual getSensor method # with the ir_byte sensor type. A result of 255 means # "nothing received". # IMPORTANT: Be sure you understand why a LOOP is necessary below. # Discuss with your instructor! ir_sensor = new_create.Sensors.ir_byte while True: number_heard = robot.getSensor(ir_sensor) if number_heard != 255: break print(number_heard) # To STOP a startIR method from sending, use stopIR method. robot.stopIR()
def test_go_distance(function_to_test): """ Tests the given go_distance_until... function. """ # TODO: Do your testing in the simulator. # If time permits, ALSO test with a REAL robot, # replacing 'sim' by YOUR port number when doing so. port = 'sim' robot = new_create.Create(port) number_of_tests = 5 for _ in range(number_of_tests): # Go a random distance that ranges from 10 to 100 cm. # Use a random speed that ranges from slow (5) to fast (50). centimeters = random.randrange(10, 101) speed = random.randrange(5, 51) message1 = 'Going {} cm at {} cm per second.' print(message1.format(centimeters, speed)) reported_distance = function_to_test(robot, centimeters, speed) message2 = ' The robot thinks it went {} cm.' print(message2.format(reported_distance)) # Pause between each movement to be able to see them separately. time.sleep(2) robot.shutdown() # Pause briefly in case we are about to make another connection to # this robot. There needs to be a short pause between connections. time.sleep(1)
def simulator(dc): """ Connects the robot using the simulator """ try: dc.port = 'sim' dc.robot = new_create.Create(dc.port) except: print("Error: Cannot connect to simulator.")
def test_go_until_black(): """ Tests the go_until_black function. """ # TODO: Implement this WITH YOUR INSTRUCTOR. port = 'sim' robot = new_create.Create(port) reading_when_stop = go_until_black(robot, 100) print(reading_when_stop) time.sleep(2) robot.shutdown()
def input_port(dc): """ Connects the robot using a port specified by the user. Prints an error if the user entered a wrong port. """ port = dc.port.get() print(port) try: dc.robot = new_create.Create(int(port)) except: print("Error: Wrong port entered.")
def go_by_time(robot, centimeters, centimeters_per_second): """ Makes the given robot go straight the given number of centimeters at the given speed (in centimeters per second), then stop. Positive centimeters means go forward; negative centimeters means go backwards. Returns the number of centimeters that the robot reports that it went (positive if it went forward, negative otherwise). IMPLEMENTATION REQUIREMENT: Use this algorithm (which we'll call the "time" algorithm): 0. Compute the TIME the robot must move to achieve the requested DISTANCE at the requested SPEED. 1. Start the robot moving at the requested speed. 2. Sleep the computed time. 3. Stop the robot. Preconditions: The arguments satisfy: :type robot: new_create.Create :type centimeters: float :type centimeters_per_second: float with centimeters_per_second being positive. """ # TODO: 2b. Implement and test this function. # # NOTE: Use the REQUIRED ALGORITHM TO IMPLEMENT, # per the specification, with the required restrictions on # the arguments (in particular, centimeters_per_second > 0). # # HINT: ** First solve this problem BY HAND on an example! ** assert (isinstance(robot, new_create.Create)) assert (isinstance(centimeters, (int, float))) assert (isinstance(centimeters_per_second, (int, float))) assert (centimeters_per_second > 0) port = 3 robot = new_create.Create(port) speed = centimeters_per_second cm = centimeters robot.go(speed, 0) robot.toFullMode() time.sleep(float(cm / speed)) sensor1 = new_create.Sensors.distance v1 = robot.getSensor(sensor1) print('the distance I have traveled is,', v1) robot.stop() robot.shutdown()
def main(): """ 1. Constructs (and hence connects to) a Create robot. Puts the robot in full mode. 2. Moves the robot forward (in a straight line) at a reasonable speed for 3 seconds (and then stops the robot). 3. Shuts down the robot. """ # Done: 2. Implement and test this function. port = 3 robot = new_create.Create(port) robot.driveDirect(30, 30) time.sleep(3) robot.stop() robot.shutdown()
def run(): port = 3 robot = new_create.Create(port) leftspeed = float(input('left speed:')) rightspeed = float(input('right speed:')) time1 = float(input('time:')) robot.toFullMode() robot.driveDirect(leftspeed, rightspeed) time.sleep(time1) sensor1 = new_create.Sensors.distance sensor2 = new_create.Sensors.angle v1 = robot.getSensor(sensor1) v2 = robot.getSensor(sensor2) input('Press the Enter key') print("distance:", v1) print('angle:', v2) robot.stop() robot.shutdown()
def main(): """ Tests functions in this module. Intended to be used internally by the primary author of this module. """ print('-------------------------------') print('Testing functions in module m1:') print('-------------------------------') data = DataContainer1 port = 'sim' robot = new_create.Create(port) data.port = port data.robot = robot root = tkinter.Tk() a = play_song(root, data) b = move_until(root, data) c = follow_line(root, data) a.mainloop() b.mainloop() c.mainloop()
def main(): # ------------------------------------------------------------------ # Set the port to whatever COM number YOUR laptop is using # for its connection to the BAM on the Create robot. # Then connect to a Create via that port. # Put the robot in "full mode" so that sensors don't stop the robot. # ------------------------------------------------------------------ port = 3 # Use YOUR laptop's COM number robot = new_create.Create(port) robot.toFullMode() # ------------------------------------------------------------------ # Start moving: # -- left wheel at 30 centimeters per second forwards, # -- right wheel at 25 centimeters per second forwards. # (so forward but veering to the right). # Sleep for 4 seconds (while doing the above motion), # then stop. # ------------------------------------------------------------------ robot.driveDirect(30, 25) time.sleep(4.0) robot.stop() # ------------------------------------------------------------------ # All sensor data is obtained by the getSensor method. # The Sensors class has constants for all sensor names available. # ------------------------------------------------------------------ sensor = new_create.Sensors.cliff_front_left_signal reflectance = robot.getSensor(sensor) print('Current value of the front left cliff sensor:', reflectance) # ------------------------------------------------------------------ # ALWAYS execute the following before your program ends. Otherwise, # your robot may be left in a funky state and on the NEXT run, # it might fail to connect or it might run incorrectly. # ------------------------------------------------------------------ robot.shutdown()
def __init__(self): """ Initializes instance variables (fields). """ # Add self.FOO = BLAH here as needed. self.robot = new_create.Create('sim') self.port = None self.remote_speed_entry = None
def main(): """ 1. Prompts for and inputs a speed and number of seconds to move at that speed. (The user should enter speeds that are between 0 and 50 and seconds between 0.5 and 5.0 or so). For example, where the user's input is shown to the right of the colons: Enter the speed: 25 Enter the number of seconds to move at that speed: 3.2 2. Constructs (and hence connects to) a Create robot. Puts the robot in full mode. 3. Moves the robot FORWARD (in a straight line) at the requested speed for the requested time (and then stops the robot). 4. Moves the robot BACKWARDS (in a straight line) at the requested speed for the requested time (and then stops the robot). 5. Makes the robo SPIN (turns in place) CLOCKWISE for the requested time (and then stops the robot). To spin, make both wheels go at the requested speed, but with one wheel going forwards and the other backwards. 6. Makes the robo SPIN (turns in place) COUNTER-CLOCKWISE for the requested time (and then stops the robot). To spin, make both wheels go at the requested speed, but with one wheel going forwards and the other backwards. 7. Shuts down the robot. """ # TODO: 2. Implement and test this function. # # IMPORTANT: Do your work using what is called # an ITERATIVE ENHANCEMENT PLAN, here: # Step 1: Implement and test just the FORWARD motion. # Get that to work before continuing. # Step 2: THEN implement and test the backward motion. # Step 3: THEN implement and test clockwise spinning. # Step 4: THEN implement and test counter-clockwise spinning. # # CAUTION: Do NOT use 'time' as a VARIABLE since it is # the name of a MODULE that you need. Instead, consider # using something like 'seconds' for the seconds to move. port = 3 robot = new_create.Create(port) speed = float(input('give me the speed')) time1 = float(input('give me a time')) robot.toFullMode() robot.go(speed, 0) time.sleep(time1) robot.stop() robot.go(-speed, 0) time.sleep(time1) robot.stop() robot.driveDirect(speed, -speed) time.sleep(time1) robot.stop() robot.driveDirect(-speed, speed) time.sleep(time1) robot.stop() robot.shutdown()