# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

import rpyc
import time
from IPython.display import display, clear_output

ROBOT_HOSTNAME = "ev3dev.local"

# # Preparation: Start rpyc on the ev3
#
# For the remote procedure calls to work, a rpcy server has to be started on the ev3. This can be done by executing the `rpyc_server.sh` file. The commands below perform this action
# ```
# ssh robot@ev3dev.local
# maker
# ./rpyc_server.sh
# ```

# # Establish and test connection

conn = rpyc.classic.connect(ROBOT_HOSTNAME)
conn.execute("print('Hello Slave. I am your master!')")

# # Robot setup:
#
# 1. Plug a large motor into output A
# 2. Plug a medium motor into output B

# # Test motors
# This rotates the motors

motors = conn.modules['ev3dev2.motor'] 
m1 = motors.LargeMotor('outA')
m1.stop_action = 'brake'
m2 = motors.MediumMotor('outB')
m2.stop_action = 'brake'

m1.run_timed(time_sp=1000, speed_sp=600)
m2.run_timed(time_sp=1000, speed_sp=600)
m1.wait_until_not_moving()
m2.wait_until_not_moving()

# +
# change speed dynamicly
speeds = [10*i for i in range(0,11)] + [10*i for i in range(9,-11, -1)] + [10*i for i in range(-9, 1)]

m2.run_direct(duty_cycle_sp=0)
for speed in speeds:
    m2.duty_cycle_sp=speed
    time.sleep(0.1)
m2.stop()

# -

# # Measure angles
# This measurs the angle of the modium motor for 30s. Try turning it!

# +
# reset motor position
m2.position = 0

# mesure position in degrees
start = time.time()
while time.time() - start < 30:
    clear_output(wait=True)
    display('Angle: ' + str(m2.position))

StarcoderdataJupyterScriptsDedupFiltered