Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FEA - Added motor function: run_to_position. (Without speed) #33

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
124 changes: 109 additions & 15 deletions simulator/api/spike/hub.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

import simulator_gui


# 'hub': {
# 'display': 'Display',
# 'config': 'dict',
Expand Down Expand Up @@ -36,13 +36,16 @@

def repl_restart():
pass



def temperature():
return simulator_gui.get_temperature()



def status():
pass


def led(*arguments):
if (len(arguments) == 0):
# Get current colour.
Expand All @@ -64,27 +67,118 @@ def led(*arguments):
if (len(arguments) == 3):
# Set from RGB
simulator_gui.set_led(arguments[0], arguments[1], arguments[2])



def reset():
pass



def powerdown_timeout():
pass



class Motion():
def yaw_pitch_roll(self):
return simulator_gui.get_yaw_pitch_roll()

def accelerometer(self):
pass
pass

def gyroscope(self):
pass
pass

def orientation(self):
pass
pass

def gesture(self):
pass

pass


motion = Motion()


class Motor():
def __init__(self, ):
self.velocity = 0

def default(self):
pass

def mode(self, state):
pass

def pid(self):
pass

def get(self):
pass

def pwm(self, pwm):
pass # self.velocity = pwm

def preset(self, preset):
pass

def brake(self):
pass

def float(self):
pass

def hold(self):
pass

def run_at_speed(self, speed, max_power, acceleration, deceleration, stall):
pass

def run_for_degrees(self, degrees, speed):
pass

def run_to_position(self, degrees, speed):
simulator_gui.ports["A"].angle.set(degrees)
pass

def run_for_time(self, time, speed):
pass

def busy(self, param): # param might be time
pass

def callback(self, param): # param not sure
pass


# Class for holding the Port reference 'A', 'B', etc
class PortRef():
pass


# dictionary of widgets to their name and class
WIDGET_LOOKUP = {None: ['motor', None],
"MotorLargeWidget": ['motor', Motor()],
"MotorSmallWidget": ['motor', Motor()],
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One concern I have is that these don't exist in the hub api. So if someone starts using the file to develop a script for the hub they may see it, use it, and then be frustrated when it doesn't work on the device itself.

I think ideally all complex functionality should live in the simulator.py script which is then imported by this file.



# This class connects Widgets to their relevant functions using the WIDGET_LOOKUP
class Port():
def __init__(self):
configured_ports = list()
for key, value in simulator_gui.ports.items():
# print("ITEM", key, value, )
if value:
configured_ports.append([key, value.reference])
else:
configured_ports.append([key, None])

for key, widget in configured_ports:
# print(f"{key=} {widget=}")
setattr(self, key, PortRef()) # Setting main PortRef 'A', 'B' etc
widget_name, widget_class = WIDGET_LOOKUP[widget]
widget = widget_class # What we are setting to
name = f"{key}.{widget_name}"
parent, child = name.split('.')
setattr(getattr(self, parent), child, widget) # Setting our sub attribute.


port = Port()
1 change: 1 addition & 0 deletions simulator/simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ def __call__(self, *args, **kwargs):
# Widget to visualise a large lego motor.
class MotorLargeWidget(object):
def __init__(self, port):
self.reference = "MotorLargeWidget" # Each widget needs this reference to allow hub to match to control functions
self.window = tkinter.Toplevel()
self.window.title(f"Large Motor : {port}")
self.window.resizable(False, False)
Expand Down