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_front_motor = Motor('F') g_front_motor.set_default_speed(30) g_back_motor = Motor('C') g_back_motor.set_default_speed(30) 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') def to_mission(hub, motor_pair, front_motor, back_motor): motor_pair.move(83, 'cm', steering=0, speed=25) def do_mission(hub, motor_pair, front_motor, back_motor): i = 0 while i < 41: motor_pair.move(0.5, 'cm', steering=0, speed=35) print(i) i += 1 def go_home(hub, motor_pair, front_motor, back_motor): motor_pair.move_tank(100, 'cm', -65, -65)
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)