def main(): motor_pair = MotorPair('B', 'A') # Establishing Gains Kp = 8 Kd = 0.1 Ki = 0 while True: target = 0 pid(Kp, Kd, Ki, target, motor_pair) motor_pair.stop() return
from spike import MotorPair from spike import DistanceSensor import utime # small wheel to spike wheel ratio wheel_gain = 55 / 30 # other init target_dist = 10 last_dist = 10 kp = 5 wall_detector = DistanceSensor('E') motor_pair = MotorPair('B', 'A') # move panda forward while True: try: dist = wall_detector.get_distance_percentage() if dist == None: dist = target_dist else: last_dist = dist except: dist = last_dist motor_power = (int(dist) - target_dist) * kp if motor_power > 50: motor_power = 50 elif motor_power < -50: motor_power = -50 print('speed:{} dist:{}'.format(dist, motor_power))
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 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 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_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.move_tank(10, 'cm', left_speed=25, right_speed=25)
if button == PoweredUPButtons.RIGHT_PLUS: motor_pair.start(speed=75) elif button == PoweredUPButtons.RIGHT_MINUS: motor_pair.start(speed=-75) elif button == PoweredUPButtons.LEFT_PLUS_RIGHT_PLUS: motor_pair.start(steering=-45, speed=75) elif button == PoweredUPButtons.LEFT_MINUS_RIGHT_PLUS: motor_pair.start(steering=45, speed=75) elif button == PoweredUPButtons.LEFT_MINUS_RIGHT_MINUS: motor_pair.start(steering=45, speed=-75) elif button == PoweredUPButtons.LEFT_PLUS_RIGHT_MINUS: motor_pair.start(steering=-45, speed=-75) elif button == PoweredUPButtons.RELEASED: motor_pair.stop() else: motor_pair.stop() # set up hub hub = PrimeHub() # set up motors motor_pair = MotorPair('A', 'B') motor_pair.set_stop_action('coast') # create remote and connect remote = PoweredUPRemote() remote.on_connect(callback=on_connect) remote.on_disconnect(callback=on_disconnect) remote.on_button(callback=on_button) remote.connect()
from spike import MotorPair import math # If the left motor is connected to Port B # And the right motor is connected to Port A. motor_pair = MotorPair('B', 'A') # amount, unit, steering (-100,100)), speed (-100 to 100) motor_pair.move(10, 'cm', speed=50) motor_pair.move(10, 'cm', speed=50, steering=50) # turn a Driving Base 180 degrees in place (if wheels are 8.1 cm apart) #motor_pair.move(8.1 * math.pi / 2, 'cm', steering=100)
from spike import MotorPair motors = MotorPair ('A', 'E') #To write a moving line, you do: motors.move(How many cenitmeters, 'cm', the angle, and then the power) motors.move(42, 'cm', 0, 50) motors.move(25, 'cm', -45, 50) motors.move(10, 'cm', 0, 50) motors.move(15, 'cm', -45, 50) motors.move(14, 'cm', 0, 50) motors.move(2, 'cm', 0, 50) motors.move(2, 'cm', 0, -50) motors.move(2, 'cm', 0, 50) motors.move(2, 'cm', 0, -50) motors.move(2, 'cm', 0, 50) motors.move(2, 'cm', 0, -50)
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() motor_pair = MotorPair('A', 'B') # Set the motor ports in the motor_pair. motor_pair.set_default_speed(30) # Set the default speed of the motor_pair. # Set the distance that the robot travels for one rotation its wheels. The value comes from# the diameter of the wheel multiplied by "π" (3.14). motor_pair.set_motor_rotation(22, 'cm') # Activate the brakes when the robot stops. The other conditions are 'hold' and 'coast'. motor_pair.set_stop_action('brake') wait_for_seconds(1) # Wait for one second. #Reset the Gyro sensor. The current yaw angle value is equal to 0. hub.motion_sensor.reset_yaw_angle() motor_pair.start( steering=100 ) # Turn left around the center of the wheelbase. Leftward because of steering=-100 parameter. # To program the robot to wait until the robot has turned, we need to define a fuction that checks if the robot has turned. def left_turn_end(): # Define the function return hub.motion_sensor.get_yaw_angle( ) > 90 # Return true or false depending on the yaw angle value.
value = urequests.get(urlValue, headers=headers).text data = ujson.loads(value) print(data) result = data.get("value").get("value") except Exception as e: print(e) result = 'failed' return result def Create_SL(Tag, Type): urlBase, headers = SL_setup() urlTag = urlBase + Tag propName = {"type": Type, "path": Tag} try: urequests.put(urlTag, headers=headers, json=propName).text except Exception as e: print(e) # Get_SL('Eric') # Create_SL('Eric','STRING') # Put_SL('Eric','STRING','done') Get_SL('Eric') # small wheel to spike wheel ratio wheel_gain = 55 / 30 # move panda forward motor_pair = MotorPair('B', 'A') motor_pair.move(math.round(10 * wheel_gain), 'cm', speed=30)
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)
from spike import MotorPair motor_pair = MotorPair('C', 'D') motor_pair.move(50, 'rotations', steering=50)
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(15) 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 turn(hub, motor_pair, angle_to_turn): if angle_to_turn != 0: print('AngleToTurn', angle_to_turn) fudge = 0.7 rotate_const = 586.0 / 360.0 hub.motion_sensor.reset_yaw_angle() turn_angle = rotate_const * angle_to_turn motor_pair.move(turn_angle, 'degrees', steering=100, speed=10) while True: yaw_angle = hub.motion_sensor.get_yaw_angle() if yaw_angle < 0 and angle_to_turn > 0:
hub.status_light.on("white") 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()