[-48.05303, -5.74169], [20.55724, -9.699068], [-26.58092, 20.82998]] Input = [0, 0, 0, 0] Hidden = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] Output = [0, 0] # END OF COMPUTER GENERATED CODE # --------------------------------------------------------------- color_left = ColorSensor('D') color_right = ColorSensor('C') color_front = ColorSensor('F') color_back = ColorSensor('B') motor_left = Motor('E') motor_right = Motor('A') def drive(left_motor, right_motor, seconds): motor_left.start_at_power(check_range(-left_motor)) motor_right.start_at_power(check_range(right_motor)) wait_for_seconds(seconds) def check_range(speed): if speed > 100: return 100 if speed < -100:
# %% print("-" * 15 + " Execution started " + "-" * 15 + "\n") # %% 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!")
# By setting its color to black # %% print("Turning center button off...") hub.status_light.on('black') print("DONE!") # %% [markdown] # # Set arm motors to starting position # In the original Scratch program, there's a `Charlie - Calibrate` block. I don't know exactly how the calibration is done, but in the end I think that it just sets the motor to position 0. # Notice that moving motors to a specific position needs to be done individually (and sequentially). In other words, we cannot run a `MotorPair` to a position, only one motor at a time. # # %% 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]
# %% print("-" * 15 + " Execution started " + "-" * 15 + "\n") # %% 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]
print("DONE!") # %% print("Displaying asleep face...") hub.light_matrix.show_image('ASLEEP') print("DONE!") # %% [markdown] # # Set motors to starting position # In the original Scratch program, there's a `Charlie - Calibrate` block. I don't know exactly how the calibration is done, but in the end I think that it just sets the motor to position 0. # Notice that moving motors to a specific position needs to be done individually (and sequentially). In other words, we cannot run a `MotorPair` to a position, only a single motor. # # %% print("Setting motors to position 0...") motor_b = Motor('B') motor_f = Motor('F') motor_b.run_to_position(0) motor_f.run_to_position(0) print("DONE!") # %% [markdown] # # Raise hands when tapped # 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!")
def initiate_objects(self): self.hub = MSHub() self.hub.status_light.on('orange') self.motor_front = Motor(self.settings['motor_front_port']) self.motor_rear = Motor(self.settings['motor_rear_port']) self.dist_sens = DistanceSensor(self.settings['dist_sens_port'])
def code(): rightArm = Motor('A') rightArm.start(50) wait_for_seconds(2) rightArm.stop()
# %% print("-" * 15 + " Execution started " + "-" * 15 + "\n") # %% hub = MSHub() app = App() # %% hub.status_light.on('black') # %% [markdown] # # Configure motors # %% print("Configuring motors...") motor_wheel = Motor('E') # Wheel (for turning around) motor_left_arm = Motor('B') # Left arm motor_left_arm.set_default_speed(-80) print("DONE!") # %% print("Setting arm motor to initial position...") motor_left_arm.run_for_seconds(1) print("DONE!") # %% [markdown] # # Configure sensor # %% print("Configuring color sensor...") 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 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")
# 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)
# # Display (happy) image # %% print("Displaying happy face...") hub.light_matrix.show_image('HAPPY') print("DONE!") # %% [markdown] # # Set arm motors to starting position # In the original Scratch program, there's a `Charlie - Calibrate` block. I don't know exactly how the calibration is done, but in the end I think that it just sets the motor to position 0. # Notice that moving motors to a specific position needs to be done individually (and sequentially). In other words, we cannot run a `MotorPair` to a position, only one motor at a time. # # %% 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] # # Dance time! # %% print("Dancing steps 1...") app.start_sound('Party Blower') for ii in range(0, 2): motor_f.run_for_degrees(-100)
# https://antonsmindstorms.com # This code is meant to run on a robot with four rotocaster wheels # The wheels are connected to ports A,B,E and F. C has a color sensor # Connect to it using a steering wheel or app: # https://play.google.com/store/apps/details?id=com.antonsmindstorms.mindstormsrc&hl=nl&gl=US from projects.mpy_robot_tools.rc import RCReceiver, R_STICK_VER, L_STICK_HOR, R_STICK_HOR from projects.mpy_robot_tools.helpers import clamp_int from mindstorms import ColorSensor, Motor, DistanceSensor from mindstorms.control import wait_for_seconds import math ls = ColorSensor('C') rcv = RCReceiver(name="roto") flw = Motor("A") # Front Right Wheel frw = Motor("B") blw = Motor("E") brw = Motor("F") light = 100 while 1: if rcv.is_connected(): speed, turn, strafe = rcv.controller_state(R_STICK_VER, R_STICK_HOR, L_STICK_HOR) if rcv.button_pressed(1): light = 100 if rcv.button_pressed(2): light = 50 if rcv.button_pressed(3): light = 0 if rcv.button_pressed(8):
] scared_animation = [ "00000:00000:99099:99099:00000", "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,
from mindstorms import MSHub, Motor, MotorPair, ColorSensor, DistanceSensor, App from mindstorms.control import wait_for_seconds, wait_until, Timer from mindstorms.operator import equal_to #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 math hub = MSHub() rightArm = Motor('F') leftArm = Motor('B') color = ColorSensor('C') def calibrate_charlie(): rightArm.set_stall_detection(True) leftArm.set_stall_detection(True) leftArm.start(20) rightArm.start(-20) wait_for_seconds(1) rightArm.run_to_position(0, 'clockwise', 50) leftArm.run_to_position(0, 'counterclockwise', 50) leftArm.set_degrees_counted(0) rightArm.set_degrees_counted(0) def open_center():
# %% print("-" * 15 + " Execution started " + "-" * 15 + "\n") # %% hub = MSHub() # %% hub.status_light.on('black') # %% [markdown] # # Configure motors # %% print("Configuring motors...") motor_steer = Motor('A') # Front wheels (for steering) motor_power = Motor('B') # Back wheels (for moving) print("DONE!") # %% [markdown] # # Set steering motor to starting position # %% print("Setting steering motor to position 0...") motor_steer.run_to_position(45, speed=100) motor_steer.run_to_position(0, speed=100) print("DONE!") # %% [markdown] # # Move MVP
# By setting its color to black # %% print("Turning center button off...") hub.status_light.on('black') print("DONE!") # %% [markdown] # # Set arm motors to starting position # In the original Scratch program, there's a `Charlie - Calibrate` block. I don't know exactly how the calibration is done, but in the end I think that it just sets the motor to position 0. # Notice that moving motors to a specific position needs to be done individually (and sequentially). In other words, we cannot run a `MotorPair` to a position, only one motor at a time. # # %% 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
# It is a little lower level, but it allows us making more things. # Fore more information, see [Maarten Pennings brilliant explanation and unofficial documentation about it](https://github.com/maarten-pennings/Lego-Mindstorms/blob/main/ms4/faq.md#why-are-there-so-many-ways-to-do--in-python). # %% # hub = MSHub() # %% # hub.status_light.on('black') hub.led(0, 0, 0) # %% [markdown] # # Initialize motors # %% print("Configuring motors...") motor_steer = Motor('A') # Front wheels (for steering) motor_power = Motor('C') # Back wheels (for moving) motor_turret = Motor('B') # Turrent spinning # %% print("Setting motors to position 0...") motor_steer.run_to_position(45, speed=100) motor_steer.run_to_position(0, speed=100) motor_turret.run_to_position(0, speed=75) print("DONE!") # %% [markdown] # # Initialize distance sensor
class AutonomousCar: def __init__(self, settings): self.settings = settings self.go_ahead = True self.turn = False self.turn_angle = 30 if bool(random.getrandbits(1)): self.turn_angle *= -1 self.initiate_objects() if settings['run_test']: self.test_function() else: self.calibrate() self.the_loop() def initiate_objects(self): self.hub = MSHub() self.hub.status_light.on('orange') self.motor_front = Motor(self.settings['motor_front_port']) self.motor_rear = Motor(self.settings['motor_rear_port']) self.dist_sens = DistanceSensor(self.settings['dist_sens_port']) def calibrate(self): self.motor_front.run_to_position(self.settings['motor_front_angle']) def test_function(self): self.hub.light_matrix.show_image('PACMAN') sounds_and_colors = [(65, "blue"), (70, "azure"), (60, "cyan")] for i in sounds_and_colors: self.hub.speaker.beep(i[0], 0.3) self.hub.status_light.on(i[1]) self.hub.light_matrix.off() def end_turn(self): self.turn = False self.motor_front.run_for_degrees(self.turn_angle * -1) def the_loop(self): self.hub.light_matrix.write('GO') self.hub.status_light.on('green') loop_time_diff = 0 loop_start_time = time.time() turn_time_diff = 0 turn_start_time = None while loop_time_diff < self.settings['how_long_run']: how_far = self.dist_sens.get_distance_cm() rear_speed = self.settings.get('motor_rear_speed') if self.turn: self.hub.status_light.on('yellow') turn_time_control = time.time() turn_time_diff = turn_time_control - turn_start_time if isinstance( how_far, int ) and how_far <= self.settings['dist_threshold_low']: self.go_ahead = False self.end_turn() elif turn_time_diff >= self.settings['turn_in_seconds']: self.end_turn() elif self.go_ahead: self.hub.status_light.on('green') rear_speed = rear_speed * -1 if isinstance( how_far, int ) and how_far <= self.settings['dist_threshold_low']: self.go_ahead = False else: self.hub.status_light.on('blue') if isinstance( how_far, int ) and how_far >= self.settings['dist_threshold_high']: self.turn = True turn_start_time = time.time() self.motor_front.run_for_degrees(self.turn_angle) self.go_ahead = True self.motor_rear.start(rear_speed) wait_for_seconds(0.25) loop_time_control = time.time() loop_time_diff = loop_time_control - loop_start_time self.motor_rear.stop() self.calibrate() self.hub.light_matrix.write('STOP')
# %% print("-" * 15 + " Execution started " + "-" * 15 + "\n") # %% 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`
# %% 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. # # %% print("Setting arm motors to position 0...")
# This code is meant as a boilerplate for your remote control project. # Connect to it using a steering wheel or app: # https://play.google.com/store/apps/details?id=com.antonsmindstorms.mindstormsrc&hl=nl&gl=US from projects.mpy_robot_tools.rc import (RCReceiver, L_STICK_HOR, L_STICK_VER, R_STICK_HOR, R_STICK_VER, L_TRIGGER, R_TRIGGER, SETTING1, SETTING2) from projects.mpy_robot_tools.helpers import clamp_int from mindstorms import MSHub, ColorSensor, Motor, DistanceSensor from mindstorms.control import wait_for_seconds import math # Create your objects here. rcv = RCReceiver(name="robot") ma = Motor("A") hub = MSHub() # Write your program here. while True: # Forever if rcv.is_connected(): speed, turn = rcv.controller_state(R_STICK_VER, R_STICK_HOR) if rcv.button_pressed(1): hub.speaker.beep() ma.start_at_power(clamp_int(speed)) else: # Stop all motors when not connected ma.stop() print("Waiting for connection...") wait_for_seconds(0.3) # async wait, better than sleep_ms
# driving with Tricky wheel_radius = 2.8 pen_radius = 8.6 axis_radius = 5.6 line_length = 5 # Create your objects here. hub = MSHub() # 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')