def run_robot(): robot = CamJamKitRobot() controller = Controller('/dev/input/js0', debug=True) pos_l = 0.0 pos_r = 0.0 while True: event_info = controller.process_event() if event_info: event_name, event_value = event_info updated = False if event_name == 'y': pos_l = -event_value if event_value > THRESHOLD or event_value < -THRESHOLD else 0.0 updated = True if event_name == 'ry': pos_r = -event_value if event_value > THRESHOLD or event_value < -THRESHOLD else 0.0 updated = True if updated: robot.value = (pos_l, pos_r)
# Increase the seek count seekcount += 1 # Change direction direction = not direction # The line wasn't found, so return False robot.stop() print("The line has been lost - relocate your robot") linelost = True return False # Tell the program what to do with a line is seen linesensor.when_line = lineseen # And when no line is seen linesensor.when_no_line = linenotseen try: # repeat the next indented block forever robot.value = motorforward while True: if not isoverblack and not linelost: seekline() # If you press CTRL+C, cleanup and stop except KeyboardInterrupt: exit()
# connect wiimote wiimote = connectToWiimote() # tell wiimote to report data back wiimote.rpt_mode = RPT_STATUS | RPT_BTN | RPT_ACC | RPT_IR | RPT_NUNCHUK | RPT_CLASSIC | RPT_BALANCE | RPT_MOTIONPLUS | RPT_EXT | MESG_STATUS | MESG_BTN | MESG_ACC | MESG_IR | MESG_NUNCHUK wiimote.rpt_mode = cwiid.RPT_BTN | cwiid.RPT_ACC | cwiid.RPT_EXT # wait for wiimote to detect nunchuk print('waiting for nunchuk to be detected') while not 'nunchuk' in wiimote.state: pass print('nunchuk detected') print('starting main loop') # main loop while True: # get joystick position joystickpos = getjoystickposition(wiimote) # scale joystick position to standard coordinates joystickpos = list(scale(joystickpos[0], joystickpos[1], 23, 36, 227, 227, 0, 0, 200, 200)) joystickpos[0] -= 100 joystickpos[1] -= 100 # get motor speeds based on joystick position motorspeeds = list(steering(joystickpos[0], joystickpos[1])) motorspeeds[0] /= 100 motorspeeds[1] /= 100 #print('motor speeds {0}'.format(motorspeeds)) # send motor speeds to motor controller robot.value = motorspeeds[::-1]
# CamJam EduKit 3 - Robotics # Worksheet 7 - Controlling the motors with PWM import time # Import the Time library from gpiozero import CamJamKitRobot # Import the CamJam GPIO Zero Library robot = CamJamKitRobot() # Set the relative speeds of the two motors, between 0.0 and 1.0 leftmotorspeed = 0.5 rightmotorspeed = 0.5 motorforward = (leftmotorspeed, rightmotorspeed) motorbackward = (-leftmotorspeed, -rightmotorspeed) motorleft = (leftmotorspeed, 0) motorright = (0, rightmotorspeed) robot.value = motorforward time.sleep(1) robot.value = motorbackward time.sleep(1) # Pause for 1 second robot.value = motorleft time.sleep(1) # Pause for 1 second robot.value = motorright time.sleep(1) # Pause for 1 second robot.stop()