# The robot has not found the black line yet, so stop robot.stop() # 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 = linenotseen # And when no line is seen linesensor.when_no_line = lineseen 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()
def joystick_loop(j, ): robot = Robot((27, 4), (6, 5)) fork = Motor(15, 23) lift = Motor(20, 21) print('Pins OK') try: axes = {} d_pad = '(0, 0) ' buttons = [] acceleration_mode = False stick_mode = False forward = False d_pad_x, d_pad_y = 0, 0 nom_left, nom_right = 0, 0 while True: try: events = pygame.event.get() for event in events: if event.type == pygame.JOYBUTTONDOWN: buttons.append(str(event.button)) if event.button == 1: # switch soft acceleration mode acceleration_mode = not acceleration_mode elif event.button == 2: try: # blocking scan function scan() try: buttons.remove(str(event.button)) except ValueError: pass except Exception as e: print(f'\r{e}') elif event.button == 3: # switch control map stick_mode = not stick_mode elif event.type == pygame.JOYBUTTONUP: try: buttons.remove(str(event.button)) except ValueError: pass elif event.type == pygame.JOYAXISMOTION: if abs(event.value) > 0.1: axes[event.axis] = f'{event.value:+.4f}' else: axes[event.axis] = f'{0.0:+.4f}' elif event.type == pygame.MOUSEMOTION: pass # print(event.rel, event.buttons) elif event.type == pygame.MOUSEBUTTONDOWN: pass elif event.type == pygame.MOUSEBUTTONUP: pass elif event.type == pygame.JOYHATMOTION: d_pad = f'{event.value} ' d_pad_x, d_pad_y = event.value else: print(event) # logging and writing to motors buttons.sort(key=lambda x: int(x)) ax = list(axes.items()) ax.sort(key=lambda x: x[0]) left, right = -float(axes.get(1, '+0.0')), -float( axes.get(4, '+0.0')) if stick_mode: left, right = joy_to_motor_best( -float(axes.get(0, '+0.0')), -float(axes.get(1, '+0.0'))) if acceleration_mode: left, right, nom_left, nom_right = calculate_diff( left, right, nom_left, nom_right, diff=0.005) if forward: left, right = 0.1, 0.1 print('\rButtons: ' + d_pad + ' '.join(buttons) + ' Axes: ' + '; '.join([f'{k}: {v}' for k, v in ax]), end=f' ({left}, {right})') robot.value = (left, right) lift.value = -d_pad_y fork.value = -d_pad_x except Exception as e: print(f'\r{e}') except KeyboardInterrupt: print("\rEXITING NOW") j.quit()
self.encoder.when_activated = self._increment self.encoder.when_deactivated = self._increment def reset(self): self._value = 0 def _increment(self): self._value += 1 @property def value(self): return self._value SAMPLETIME = 0.1 r = Robot((3, 2), (15, 14)) e1 = Encoder(17) e2 = Encoder(27) #start the robot m1_speed = 1.0 m2_speed = 1.0 r.value = (m1_speed, m2_speed) #find a sample rate while True: print("e1 {} e2 {}".format(e1.value, e2.value)) e1.reset() e2.reset() sleep(SAMPLETIME)
from gpiozero import Robot from time import sleep left_pins = (24, 25) right_pins = (18, 23) robot = Robot(left_pins, right_pins) while True: x = input("left 1, right 2, forward 3, back 4") if x == 1: print("left") robot.value = (1, 0) elif x == 2: print("right") robot.value = (0, 1) elif x == 3: print("forward") robot.value = (1, 1) elif x == 4: print("back") robot.value = (-1, -1)
diminui_velocidade = True while not (iscenteroverblack() and isrightoverblack() and isleftoverblack()) and not robotlost: if (sensor.distance > 20): # print(sensor.distance) # if (sensor.distance < 30):# and diminui_velocidade: # print("diminuindo velocidade "+str(sensor.distance)) #motorforward = (leftmotorspeed-0.15, rightmotorspeed-0.15) #motorbackward = (-leftmotorspeed+0.15, -rightmotorspeed+0.15) #motorright = (-leftmotorspeed+0.15, rightmotorspeed-0.15) #motorleft = (leftmotorspeed-0.15, -rightmotorspeed+0.15) #robot.stop() #time.sleep(1) #diminui_velocidade = False if iscenteroverblack(): print("para frente") robot.value = motorforward # time.sleep(tempo) if isrightoverblack(): print("para a direita") robot.value = motorright # time.sleep(tempo) if isleftoverblack(): print("para a esquerda") robot.value = motorleft # time.sleep(tempo) else: print("achou objeto") servo_position = 0 servo.value = servo_position time.sleep(1) robot.value = motorright
gate_wid = 50 gate_dis = 50 gate_L = width / 2 - gate_dis gate_R = width / 2 + gate_dis # threshold variables #hsv greenLower = (29, 86, 35) greenUpper = (70, 255, 255) white = (255, 255, 255) # motor setup r = Robot(mL, mR) # m_speed = (mL_speed, mR_speed) m_speed = (base_spd, base_spd) r.value = m_speed # initialize camera and grab reference camera = PiCamera() camera.resolution = (width, height) camera.framerate = 32 rawCapture = PiRGBArray(camera, size=(width, height)) # allow camera to warm up time.sleep(0.1) # grab an image # capture frames from the camera for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):
from gpiozero import Robot from time import sleep left_pins = (24, 25) right_pins = (18, 23) robot = Robot(left_pins, right_pins) while True: print("right") robot.value = (0, 1) sleep(2) print("left") robot.value = (1, 0) sleep(2) print("both") robot.value = (1, 1) sleep(2)
mR = (5, 6) satadj = 1.25 SLEEPTIME = 0.1 # threshold variables #hsv greenLower = (29, 86, 50) greenUpper = (64, 255, 255) white = (255, 255, 255) # motor setup r = Robot(mL, mR) mL_speed = base_spd mR_speed = base_spd r.value = (mL_speed, mR_speed) # initialize camera and grab reference camera = PiCamera() camera.resolution = (width, height) camera.framerate = 32 rawCapture = PiRGBArray(camera, size=(width, height)) # allow camera to warm up time.sleep(0.1) # grab an image # capture frames from the camera for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True): # grab the raw NumPy array representing the image, then initialize the timestamp
from gpiozero import Robot from time import sleep robot = Robot(left=(4, 14), right=(17, 18)) robot.forward() # full speed forwards robot.forward(0.5) # half speed forwards robot.backward() # full speed backwards robot.backward(0.5) # half speed backwards robot.stop() # stop the motor robot.value = 0.5 # half speed forwards robot.value = -0.5 # half speed backwards robot.value = 0 # stop robot.reverse() # reverse direction at same speed, e.g... robot.forward(0.5) # going forward at half speed robot.reverse() # now going backwards at half speed robot.right() robot.left()
from gpiozero import Robot from gpiozero.tools import zip_values from time import sleep blackWinter = Robot(left=(7, 8), right=(9, 10)) blackWinter.stop() print("setting full speed ahead straight 3 secs") blackWinter.value = (1.0, 1.0) blackWinter.forward() sleep(1) # print("pausing 3 secs") # blackWinter.stop() # sleep(3) print("3 sec turning left") blackWinter.value = (0.1, 1.0) #blackWinter.forward() sleep(11.5) blackWinter.stop() print("setting full speed ahead straight 3 secs") blackWinter.value = (1.0, 1.0) blackWinter.forward() sleep(4) # print("pausing 3 secs") # sleep(3) # blackWinter.stop()