Skip to content

Commit

Permalink
ev3: split mechanics to file
Browse files Browse the repository at this point in the history
  • Loading branch information
laurensvalk committed Mar 12, 2021
1 parent f7d1b46 commit fcc8dcd
Show file tree
Hide file tree
Showing 2 changed files with 154 additions and 145 deletions.
151 changes: 6 additions & 145 deletions ev3/main.py
Original file line number Diff line number Diff line change
@@ -1,150 +1,11 @@
#!/usr/bin/env pybricks-micropython

from pybricks.hubs import EV3Brick
from pybricks.ev3devices import Motor
from pybricks.parameters import Port, Stop, Button
from pybricks.tools import wait
from pybricks.messaging import BluetoothMailboxServer, TextMailbox

SPEED = 100
from monitor import SpikeMonitor

class SpikeManager:

def __init__(self):
# Initialize devices.
self.ev3 = EV3Brick()
self.usb_motor = Motor(Port.D)
self.left_motor = Motor(Port.B)
self.right_motor = Motor(Port.A)

# Relax target tolerances so the motion is considered complete even
# if off by a few more degrees than usual. This way, it won't block.
# But set speed tolerance strict, so we move at least until fully
# stopped, which is when we are pressing the button.
self.left_motor.control.target_tolerances(speed=0, position=30)
self.right_motor.control.target_tolerances(speed=0, position=30)

# Run all motors to end points.
self.targets = {
'usb_in': self.usb_motor.run_until_stalled(-SPEED, duty_limit=50),
'usb_out': self.usb_motor.run_until_stalled(SPEED, duty_limit=50),
'center_pressed': self.left_motor.run_until_stalled(-SPEED, duty_limit=50) + 10,
'left_pressed': self.left_motor.run_until_stalled(SPEED, duty_limit=50),
'right_pressed': self.right_motor.run_until_stalled(SPEED, duty_limit=50) + 10,
'bluetooth_pressed': self.right_motor.run_until_stalled(-SPEED, duty_limit=50) - 10,
}

# Set other targets between end points.
self.targets['left_released'] = (self.targets['left_pressed'] + self.targets['center_pressed']) / 2
self.targets['center_released'] = self.targets['left_released']

self.targets['right_released'] = (self.targets['right_pressed'] + self.targets['bluetooth_pressed']) / 2
self.targets['bluetooth_released'] = self.targets['right_released']

# Get in initial state.
self.press_center(False)
self.press_bluetooth(False)
self.insert_usb(False)

# Turn the hub off.
self.shutdown()
self.speaker.beep()


def insert_usb(self, insert):
key = 'usb_in' if insert else 'usb_out'
self.usb_motor.run_target(SPEED, self.targets[key])

def press_left(self, press):
if press:
self.left_motor.run_target(SPEED, self.targets['left_pressed'])
self.left_motor.dc(80)
else:
while abs(self.left_motor.speed()) > 100:
wait(10)
self.left_motor.run_target(SPEED, self.targets['left_released'], Stop.COAST)

def press_center(self, press):
if press:
self.left_motor.run_target(SPEED, self.targets['center_pressed'])
else:
self.left_motor.run_target(SPEED, self.targets['center_released'], Stop.COAST)

def press_right(self, press):
if press:
self.right_motor.run_target(SPEED, self.targets['right_pressed'])
else:
self.right_motor.run_target(SPEED, self.targets['right_released'], Stop.COAST)

def press_bluetooth(self, press):
if press:
self.right_motor.run_target(SPEED, self.targets['bluetooth_pressed'])
self.right_motor.dc(-100)
else:
while abs(self.right_motor.speed()) > 100:
wait(10)
self.right_motor.run_target(SPEED, self.targets['bluetooth_released'], Stop.COAST)

def click_center(self, duration=100):
self.press_center(False)
self.press_center(True)
wait(duration)
self.press_center(False)

def click_bluetooth(self, duration=200):
self.press_bluetooth(False)
self.press_bluetooth(True)
wait(duration)
self.press_bluetooth(False)

def click_left(self, duration=100):
self.press_left(False)
self.press_left(True)
wait(duration)
self.press_left(False)

def click_right(self, duration=100):
self.press_right(False)
self.press_right(True)
wait(duration)
self.press_right(False)

def activate_dfu(self):
self.press_bluetooth(True)
wait(600)
self.insert_usb(True)
wait(8000)
self.press_bluetooth(False)

def shutdown(self):
self.click_center(duration=4000)

def test_buttons(self):
while True:
while True:
pressed = self.ev3.buttons.pressed()
if any(pressed):
break

if Button.CENTER in pressed:
self.click_center()
elif Button.UP in pressed:
self.click_bluetooth()
elif Button.LEFT in pressed:
self.click_left()
elif Button.RIGHT in pressed:
self.click_right()
elif Button.DOWN in pressed:
break

while any(self.ev3.buttons.pressed()):
wait(10)


# Initialize mechanisms
spike_manager = SpikeManager()
# spike_manager.test_buttons()
spike_manager.ev3.speaker.beep()
# Create monitor object and initialize mechanisms
spike = SpikeMonitor()

# Wait for incoming bluetooth connection
server = BluetoothMailboxServer()
Expand All @@ -161,11 +22,11 @@ def test_buttons(self):

# Execute command
if command == 'activate_dfu':
spike_manager.activate_dfu()
spike.activate_dfu()
elif command == 'remove_usb':
spike_manager.insert_usb(False)
spike.insert_usb(False)
elif command == 'shutdown':
spike_manager.shutdown()
spike.shutdown()
elif command == 'stop':
break

Expand Down
148 changes: 148 additions & 0 deletions ev3/monitor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
#!/usr/bin/env pybricks-micropython

from pybricks.hubs import EV3Brick
from pybricks.ev3devices import Motor
from pybricks.parameters import Port, Stop, Button
from pybricks.tools import wait
from pybricks.messaging import BluetoothMailboxServer, TextMailbox


SPEED = 100


class SpikeMonitor:

def __init__(self):
# Initialize devices.
self.ev3 = EV3Brick()
self.usb_motor = Motor(Port.D)
self.left_motor = Motor(Port.B)
self.right_motor = Motor(Port.A)

# Relax target tolerances so the motion is considered complete even
# if off by a few more degrees than usual. This way, it won't block.
# But set speed tolerance strict, so we move at least until fully
# stopped, which is when we are pressing the button.
self.left_motor.control.target_tolerances(speed=0, position=30)
self.right_motor.control.target_tolerances(speed=0, position=30)

# Run all motors to end points.
self.targets = {
'usb_in': self.usb_motor.run_until_stalled(-SPEED, duty_limit=50) + 10,
'usb_out': self.usb_motor.run_until_stalled(SPEED, duty_limit=50) - 10,
'center_pressed': self.left_motor.run_until_stalled(-SPEED, duty_limit=50) + 10,
'left_pressed': self.left_motor.run_until_stalled(SPEED, duty_limit=50),
'right_pressed': self.right_motor.run_until_stalled(SPEED, duty_limit=50) + 10,
'bluetooth_pressed': self.right_motor.run_until_stalled(-SPEED, duty_limit=50) - 10,
}

# Set other targets between end points.
self.targets['left_released'] = (self.targets['left_pressed'] + self.targets['center_pressed']) / 2
self.targets['center_released'] = self.targets['left_released']

self.targets['right_released'] = (self.targets['right_pressed'] + self.targets['bluetooth_pressed']) / 2
self.targets['bluetooth_released'] = self.targets['right_released']

# Get in initial state.
self.press_center(False)
self.press_bluetooth(False)
self.insert_usb(False)

# Turn the hub off.
self.shutdown()
self.ev3.speaker.beep()

def insert_usb(self, insert):
key = 'usb_in' if insert else 'usb_out'
self.usb_motor.run_target(SPEED, self.targets[key])

def press_left(self, press):
if press:
self.left_motor.run_target(SPEED, self.targets['left_pressed'])
self.left_motor.dc(80)
else:
while abs(self.left_motor.speed()) > 100:
wait(10)
self.left_motor.run_target(SPEED, self.targets['left_released'], Stop.COAST)

def press_center(self, press):
if press:
self.left_motor.run_target(SPEED, self.targets['center_pressed'])
else:
self.left_motor.run_target(SPEED, self.targets['center_released'], Stop.COAST)

def press_right(self, press):
if press:
self.right_motor.run_target(SPEED, self.targets['right_pressed'])
else:
self.right_motor.run_target(SPEED, self.targets['right_released'], Stop.COAST)

def press_bluetooth(self, press):
if press:
self.right_motor.run_target(SPEED, self.targets['bluetooth_pressed'])
self.right_motor.dc(-100)
else:
while abs(self.right_motor.speed()) > 100:
wait(10)
self.right_motor.run_target(SPEED, self.targets['bluetooth_released'], Stop.COAST)

def click_center(self, duration=100):
self.press_center(False)
self.press_center(True)
wait(duration)
self.press_center(False)

def click_bluetooth(self, duration=200):
self.press_bluetooth(False)
self.press_bluetooth(True)
wait(duration)
self.press_bluetooth(False)

def click_left(self, duration=100):
self.press_left(False)
self.press_left(True)
wait(duration)
self.press_left(False)

def click_right(self, duration=100):
self.press_right(False)
self.press_right(True)
wait(duration)
self.press_right(False)

def activate_dfu(self):
self.press_bluetooth(True)
wait(600)
self.insert_usb(True)
wait(8000)
self.press_bluetooth(False)

def shutdown(self):
self.click_center(duration=4000)

def test_buttons(self):
while True:
while True:
pressed = self.ev3.buttons.pressed()
if any(pressed):
break

if Button.CENTER in pressed:
self.click_center()
elif Button.UP in pressed:
self.click_bluetooth()
elif Button.LEFT in pressed:
self.click_left()
elif Button.RIGHT in pressed:
self.click_right()
elif Button.DOWN in pressed:
break

while any(self.ev3.buttons.pressed()):
wait(10)


if __name__ == "__main__":
spike = SpikeMonitor()
spike.test_buttons()

0 comments on commit fcc8dcd

Please sign in to comment.