def __init__(self): self.hub = PrimeHub() self.colorsensor = ColorSensor('B') self.force = ForceSensor('A') self.boundary = None# Initial Decision boundary self.ATraining = []# Initial A examples self.BTraining = []# Initial B examples self.averageA = None# Initial average for the A training self.averageB = None# Initial average for the B training
def go_until_black(hub, motor_pair): right_sensor = ColorSensor('F') left_sensor = ColorSensor('B') motor_pair.start(0, 30) while True: right_color = right_sensor.get_color() left_color = left_sensor.get_color() if right_color == 'black': motor_pair.stop() motor_pair.start_tank(left_speed=30, right_speed=-10) if left_color == 'black': motor_pair.stop() if left_color == 'black': motor_pair.stop() motor_pair.start_tank(left_speed=-10, right_speed=30) if right_color == 'black': motor_pair.stop() if right_color == 'black' and left_color == 'black': motor_pair.stop() break
from spike import PrimeHub, LightMatrix, Button, StatusLight, ForceSensor, MotionSensor, Speaker, ColorSensor, App, DistanceSensor, Motor, MotorPair from spike.control import wait_for_seconds, wait_until, Timer from math import * hub = PrimeHub() color_sensor_E = ColorSensor('E') color_sensor_F = ColorSensor('F') motor_A = Motor('A') # Set the motor port to the motor. motor_B = Motor('B') # Set the motor port to the motor. motor_A.set_default_speed(-30) # Set the default speed of the motor. motor_B.set_default_speed(30) # Set the default speed of the motor. motor_A.set_stop_action('brake') # Activate the brakes when the motor stops. The other conditions are 'hold' and 'coast'. motor_B.set_stop_action('brake') # Activate the brakes when the motor stops. motor_A_flag = 0 # Create a flag for motor A and set it to OFF. motor_B_flag = 0 # Create a flag for motor B and set it to OFF. def stop_at(color): motor_A_flag = 0 # Reset the flag for motor A to OFF. motor_B_flag = 0 # Reset the flag for motor B to OFF. # Move forward. motor_A.start() motor_B.start() while (motor_A_flag == 0) or (motor_B_flag == 0): # Repeat while both sensors ever detect black color. if color_sensor_E.get_color() == color: # If the color sensor on port E detect the desired color motor_A.stop() # stop the motor an port A and
from spike import PrimeHub, LightMatrix, Button, StatusLight, ForceSensor, MotionSensor, Speaker, ColorSensor, App, \ DistanceSensor, Motor, MotorPair from spike.control import wait_for_seconds, wait_until, Timer import math g_hub = PrimeHub() g_right_sensor = ColorSensor('D') g_motor_pair = MotorPair('A', 'E') g_wheel_distance_apart = 14.5 g_wheel_radius = 4.25 g_wheel_circumference = 2 * math.pi * g_wheel_radius g_motor_pair.set_motor_rotation(g_wheel_circumference, 'cm') g_motor_pair.set_default_speed(-50) g_motor_pair.set_stop_action('break') def line_follow(hub, motor_pair, right_sensor, num): count = 0 while count < num: if right_sensor.get_reflected_light() > 50: motor_pair.start_tank(-25, 15) else: motor_pair.start_tank(20, 15) count += 1 line_follow(g_hub, g_motor_pair, g_right_sensor, 200)
from spike import ColorSensor # Initialize the Color Sensor. paper_scanner = ColorSensor('E') # Measure the color. color = paper_scanner.get_color() # Print the color name to the console print('Detected:', color) # Check if it is a specific color # Values: 'black','violet','blue','cyan','green','yellow','red','white', None if color == 'red': print('It is red!') elif color == 'white': print('It is white!')
from spike import ColorSensor from spike.control import wait_for_seconds # Initialize the Color Sensor. paper_scanner = ColorSensor('E') while True: # Measure the intensity. red = paper_scanner.get_red() green = paper_scanner.get_green() blue = paper_scanner.get_blue() # Print the color name to the console print('Red intensity', red) print('Green intensity', green) print('Blue intensity', blue) wait_for_seconds(2)
from spike import ColorSensor from spike.control import wait_until from spike.operator import equal_to color_sensor = ColorSensor('E') print("Waiting") # wait for the Color Sensor to detect red wait_until(color_sensor.get_color, equal_to, 'red') print("Yay! Found Red")
from util.print_override import spikeprint print = spikeprint from spike import PrimeHub, LightMatrix, Button, StatusLight, ForceSensor, MotionSensor, Speaker, ColorSensor, App, DistanceSensor, Motor, MotorPair from spike.control import wait_for_seconds, wait_until, Timer hub = PrimeHub() from util.print_override import spikeprint print = spikeprint from spike import PrimeHub, LightMatrix, Button, StatusLight, ForceSensor, MotionSensor, Speaker, ColorSensor, App, DistanceSensor, Motor, MotorPair from spike.control import wait_for_seconds, wait_until, Timer hub = PrimeHub() color = ColorSensor('D') del print # removes print to console functionality; restores standard implementation x = 0 def printSP(input): #use to filter pi serial print("TXTSPTXT" + str(input) + "TXTSPTXT") while True: # Check for win condition if (color.get_color() == 'red'): # tell pi that mario has won" printSP("9;red:(1,2)") # cheer to show Mario wins hub.light_matrix.write('M')
from spike import ColorSensor from spike.control import wait_for_seconds # Initialize the Color Sensor. color_sensor = ColorSensor('E') while True: color_sensor.wait_until_color('red') print("Red object found")
from spike import PrimeHub, LightMatrix, Button, StatusLight, ForceSensor, MotionSensor, Speaker, ColorSensor, App, \ DistanceSensor, Motor, MotorPair from spike.control import wait_for_seconds, wait_until, Timer import math hub = PrimeHub() motor_pair = MotorPair('A', 'E') right_motor = Motor('E') left_color = ColorSensor('B') right_color = ColorSensor('D') wheel_distance_apart = 14.5 wheel_radius = 4.25 wheel_circumference = 2 * math.pi * wheel_radius motor_pair.set_motor_rotation(wheel_circumference, 'cm') right_motor.set_default_speed(15) i = 0 motor_pair.start_tank(25, 25) while i < 200: if left_color.get_color() == 'black': print('l') motor_pair.start_tank(5, 25) elif right_color.get_color() == 'black': print('r') motor_pair.start_tank(25, 5) i += 1 motor_pair.stop() motor_pair.move_tank(165, 'cm', left_speed=15, right_speed=15) right_motor.run_for_rotations(20) motor_pair.start_tank(-25, -25)
def closeGrip(): motor_e.set_default_speed(50) motor_e.run_for_rotations(+1.50) def openGrip(): motor_e.set_default_speed(50) motor_e.run_for_rotations(-1.50) # set up hub, motors and sensors hub = PrimeHub() motor_pair = MotorPair('A', 'B') motor_pair.set_motor_rotation(17.5, 'cm') motor_pair.set_default_speed(20) colour_c = ColorSensor('C') distance = DistanceSensor('D') motor_e = Motor('E') # create a peripheral object remote = BLEPeripheral() # scan for the target device and connect if found utime.sleep(1) remote.on_connect(callback=on_connect) remote.on_disconnect(callback=on_disconnect) remote.connect() grabberOpen = True while remote.is_connected() is not None: