# # %% print("Setting arm motors to position 0...") motor_b = Motor('B') # Left arm motor_f = Motor('F') # Right arm motor_b.run_to_position(0) motor_f.run_to_position(0) print("DONE!") # %% [markdown] # # Configure motors # %% print("Configuring motors...") motors_movement = MotorPair('A', 'E') motors_movement.set_default_speed(80) print("DONE!") # %% [markdown] # # Program color reactions # %% color_sensor = ColorSensor('C') # %% [markdown] # We will use a counter to control printing on the console. # First, we need to initialize it. # %% ii = 0
from mindstorms import MSHub, Motor, MotorPair, ColorSensor, DistanceSensor, App from mindstorms.control import wait_for_seconds, wait_until, Timer from mindstorms.operator import greater_than, greater_than_or_equal_to, less_than, less_than_or_equal_to, equal_to, not_equal_to import math # Create your objects here. hub = MSHub() movement_motors = MotorPair('E', 'A') roll_target = 89.95 power = 3 integral = 0 #Not actual game values (k) kp = 0 ki = 0 kd = 0 # Write your program here. hub.light_matrix.show_image('HAPPY') wait_for_seconds(3) while True: error = roll_target - hub.motion_sensor.get_roll_angle() integral = integral + error * 0.25 derivative = error - prev_error prev_error = error result = error * kp + integral * ki + derivative * kd print( "error:", error, "integral:", integral, "derivative:",
# %% hub = MSHub() app = App() # %% hub.status_light.on('black') # %% [markdown] # # Configure motors # %% print("Configuring motors...") motor_left_arm = Motor('B') # Left arm motor_right_arm = Motor('F') # Right arm motors_arms = MotorPair('B', 'F') motor_left_arm.set_default_speed(-100) motor_right_arm.set_default_speed(100) print("DONE!") # %% [markdown] # # Set arm motors to starting position # %% print("Setting arm motors to position 0...") motor_left_arm.run_to_position(0) motor_right_arm.run_to_position(0) print("DONE!") # %% [markdown]
# Write your program here. hub.speaker.beep() motor = Motor('C') motor.run_to_position(165) def pen_up(): motor.run_for_degrees(-90) def pen_down(): motor.run_for_degrees(90) motor_pair = MotorPair('B', 'A') motor_pair.set_default_speed(20) motor_pair.set_motor_rotation(wheel_radius * 2 * math.pi, 'cm') for i in range(4): if i > 0: motor_pair.move(pen_radius, 'cm') motor_pair.move_tank(axis_radius * math.pi / 2, 'cm', left_speed=-20, right_speed=20) motor_pair.move(-pen_radius, 'cm') pen_down()
# Write your program here. hub.speaker.beep() motor = Motor('C') motor.run_to_position(165) def pen_up(): motor.run_for_degrees(-90) def pen_down(): motor.run_for_degrees(90) motor_pair = MotorPair('B', 'A') motor_pair.set_default_speed(50) motor_pair.set_motor_rotation(wheel_radius * 2 * math.pi, 'cm') for i in range(30): pen_down() motor_pair.move(-random.randrange(1,3), 'cm', speed = 20) pen_up() r = random.randrange(2) if r==0: motor_pair.move(pen_radius, 'cm') motor_pair.move_tank(axis_radius*math.pi/2, 'cm', left_speed=-50, right_speed=50) motor_pair.move(-pen_radius, 'cm') if r==1: motor_pair.move(pen_radius, 'cm')
# %% hub = MSHub() app = App() # %% hub.status_light.on('black') # %% [markdown] # # Configure motors # %% print("Configuring motors...") motor_left_arm = Motor('B') # Left arm motor_right_arm = Motor('F') # Right arm motors_arms = MotorPair('B', 'F') motors_wheels = MotorPair('A', 'E') print("DONE!") # %% [markdown] # # Set arm motors to starting position # %% print("Setting arm motors to position 0...") motor_left_arm.run_to_position(0) motor_right_arm.run_to_position(0) print("DONE!") # %% [markdown] # # Overwrite `Timer`
# All through the execution, we will put pauses on different points to be able to see better what Charlie is doing. # %% print("Waiting to be tapped...") if hub.motion_sensor.wait_for_new_gesture() == 'tapped': print("TAPPED!") print("Displaying surprised face...") hub.light_matrix.show_image('SURPRISED') print("DONE!") wait_for_seconds(1) print("Raise hands...") motor_pair = MotorPair('B', 'F') motor_pair.set_default_speed(50) motor_pair.move(-90, unit='degrees') # Negative angle raises the hands. print("DONE!") print("Saying 'Hi!'...") app.play_sound('Robot 2', 75) print("DONE!") wait_for_seconds(1) print("Lower hands...") motor_pair.move(90, unit='degrees') # Positive angle lowers the hands. print("DONE!") wait_for_seconds(1)
async def on_start(vm, stack): ms_hub.status_light.on("black") rotate_hub_display_to_value("3") blinking_frames = [hub.Image(frame) for frame in blinking_animation] vm.system.display.show(blinking_frames, clear=False, delay=round(1000 / 8), loop=True, fade=1) left_hand = Motor("B") right_hand = Motor("F") # Calibrate left_hand.run_to_position(0) right_hand.run_to_position(0) vm.system.sound.play("/extra_files/Humming") for i in range(2): right_hand.run_for_degrees(-100, 50) right_hand.run_for_degrees(100, 50) left_hand.run_for_degrees(100, 50) left_hand.run_for_degrees(-100, 50) left_turn = MotorPair("B", "E") right_turn = MotorPair("A", "F") vm.system.sound.play("/extra_files/Humming") for i in range(2): right_turn.move(100, "degrees", steering=100, speed=50) right_turn.move(-100, "degrees", steering=100, speed=50) left_turn.move(100, "degrees", steering=-100, speed=50) left_turn.move(-100, "degrees", steering=-100, speed=50) wheels = MotorPair("A", "E") wheels.move(14 * math.pi, "cm", steering=100, speed=80) wheels.move(14 * math.pi, "cm", steering=-100, speed=80) right_hand.run_to_position(270, direction="counterclockwise") wait_for_seconds(1) right_hand.run_for_seconds(1, 50) await vm.system.sound.play_async("/extra_files/Tadaa") wait_for_seconds(1) right_hand.run_to_position(0, direction="counterclockwise")
# # %% print("Setting arm motors to position 0...") motor_b = Motor('B') # Left arm motor_f = Motor('F') # Right arm motor_b.run_to_position(0) motor_f.run_to_position(0) print("DONE!") # %% [markdown] # # Configure motors # %% print("Configuring motors...") motors_wheels = MotorPair('A', 'E') print("DONE!") # %% [markdown] # # Rise arms # %% print("Rising arms...") motor_f.set_default_speed(-75) motors_arms = MotorPair('B', 'F') motors_arms.move(-90, unit='degrees') # Negative angle is clockwise movement motors_arms.move(90, unit='degrees') # Positive angle is counterclockwise movement print("DONE!") # %% [markdown]
# Radar car - this car drives only north<->south or east<->west, when hitting a wall rotating to where is the most space. from mindstorms import MSHub, Motor, MotorPair, DistanceSensor import math import sys # Setup hardware hub = MSHub() mradar = Motor("C") sradar = DistanceSensor("F") mdrive = MotorPair("A", "B") # Lights up 5x5 matrix. # Parameter `bits` is a 25 bits vector: a 1 switches on that led. # Bit 24 is upper left, bit 0 is lower right. def set_image(bits): hub.light_matrix.off() cur = 1 << 24 for y in range(5): for x in range(5): if bits & cur: hub.light_matrix.set_pixel(x, y, 100) cur >>= 1 # Emits 3 tone fancy beep def beep(): hub.speaker.beep(60, 0.1) hub.speaker.beep(70, 0.1) hub.speaker.beep(62, 0.1)
for ii in range(0, 2): motor_f.run_for_degrees(-100) motor_f.run_for_degrees(100) motor_b.run_for_degrees(100) motor_b.run_for_degrees(-100) print("DONE!") # %% print("Dancing steps 2...") app.start_sound('Party Blower') motors_af = MotorPair('A', 'F') motors_be = MotorPair('B', 'E') for ii in range(0, 2): motors_af.move(-100, unit='degrees', steering=-100) motors_af.move(100, unit='degrees', steering=-100) motors_be.move(100, unit='degrees', steering=-100) motors_be.move(-100, unit='degrees', steering=-100) print("DONE!") # %% print("Dancing steps 3...")
# %% hub = MSHub() app = App() # %% hub.status_light.on('black') hub.light_matrix.show_image('SILLY') # %% [markdown] # # Configure motors # %% print("Configuring motors...") motors_wheels = MotorPair('A', 'E') motors_wheels.set_default_speed(80) motor_left_arm = Motor('B') # Left arm motor_right_arm = Motor('F') # Right arm motor_left_arm.set_default_speed(75) motor_right_arm.set_default_speed(-75) motors_arms = MotorPair('B', 'F') print("DONE!") # %% [markdown] # # Set arm motors to starting position # %%
"00000:99900:99900:99909:00000", "00000:99900:99900:99909:00000", "00000:00000:99099:99099:00000", "00000:00999:00999:90999:00000", "00000:00999:00999:90999:00000", ] happy_animation = [ "77077:00000:97097:79079:00000", "77077:00000:79079:97097:00000", ] ms_hub = MSHub() left_hand = Motor("B") right_hand = Motor("F") wheels = MotorPair("A", "E") color_sensor = ColorSensor("C") async def on_start(vm, stack): ms_hub.status_light.on("black") rotate_hub_display_to_value("3") blinking_frames = [hub.Image(frame) for frame in looking_animation] vm.system.display.show(blinking_frames, clear=False, delay=1000, loop=True, fade=1) # Calibrate
# %% print("Displaying first image...") hub.light_matrix.show_image('HAPPY') print("DONE!") # %% [markdown] # # # Control motors # We could initialize and control the motors individually. # However, it can be very practical to pair them, which can be done as follows. # %% # Initialize motors. print("Initializing motors...") motor_pair = MotorPair('A', 'E') print("DONE!") # Set default speed. speed = 80 print("Setting default speed to " + str(speed) + "...") motor_pair.set_default_speed(speed) print("DONE!") # Move the motors. amount = 40 # With steering, we can make the robot turn right (100), # turn left (-100) or go straight (0) steering = 100
# %% hub = MSHub() app = App() # %% hub.status_light.on('black') # %% [markdown] # # Configure motors # %% print("Configuring motors...") motor_left_arm = Motor('B') # Left arm motor_right_arm = Motor('F') # Right arm motors_arms = MotorPair('B', 'F') motors_wheels = MotorPair('A', 'E') print("DONE!") # %% [markdown] # # Set arm motors to starting position # %% print("Setting arm motors to position 0...") motor_left_arm.run_to_position(0) motor_right_arm.run_to_position(0) print("DONE!") # %% [markdown] # # Configure sensor
# %% hub = MSHub() app = App() # %% hub.status_light.on('black') # %% [markdown] # # Configure motors # %% print("Configuring motors...") motor_left_arm = Motor('B') # Left arm motor_right_arm = Motor('F') # Right arm motors_arms = MotorPair('B', 'F') print("DONE!") # %% [markdown] # # Set arm motors to starting position # %% print("Setting arm motors to position 0...") motor_left_arm.run_to_position(0) motor_right_arm.run_to_position(0) print("DONE!") # %% [markdown] # # Overwrite `Timer` # Unfortunately, `mindstorms.control.Timer` doesn't support floats. # Thus, we need to define our own timer (using the functionality of `utime`).
# %% print("-"*15 + " Execution started " + "-"*15 + "\n") # %% hub = MSHub() app = App() # %% hub.status_light.on('black') # %% [markdown] # # Configure motors # %% print("Configuring motors...") motors_wheels = MotorPair('A', 'E') motors_wheels.set_default_speed(70) motor_left_arm = Motor('B') # Left arm motor_right_arm = Motor('F') # Right arm motor_left_arm.set_default_speed(40) motor_right_arm.set_default_speed(-40) motors_arms = MotorPair('B', 'F') print("DONE!") # %% [markdown] # # Set arm motors to starting position # This isn't part of the original program, but I think it is good practice to make sure motors always start in the same state. #
from mindstorms import MSHub, Motor, MotorPair, ColorSensor, DistanceSensor, App from mindstorms.control import wait_for_seconds, wait_until, Timer from mindstorms.operator import greater_than, greater_than_or_equal_to, less_than, less_than_or_equal_to, equal_to, not_equal_to import math import time # First experiments with a balancing robot... no yet finished. # See https://medium.com/@janislavjankov/self-balancing-robot-with-lego-spike-prime-ac156af5c2b2 for a more # complete example. wheel_radius = 2.8 # Create your objects here. hub = MSHub() motor_pair = MotorPair('B', 'F') motor_pair.set_default_speed(50) motor_pair.set_motor_rotation(wheel_radius * 2 * math.pi, 'cm') while True: r = hub.motion_sensor.get_roll_angle() + 90 motor_pair.start(speed=-r * 40)
# Write your program here. hub.speaker.beep() motor = Motor('C') motor.run_to_position(165) def pen_up(): motor.run_for_degrees(-90) def pen_down(): motor.run_for_degrees(90) motor_pair = MotorPair('B', 'A') motor_pair.set_default_speed(20) motor_pair.set_motor_rotation(wheel_radius * 2 * math.pi, 'cm') pen_down() motor_pair.move_tank(axis_radius * math.pi * 2.05, 'cm', left_speed=-25, right_speed=25) #pen_up() #pen_down() motor_pair.move(-pen_radius * 2, 'cm') pen_up()