Skip to content

Commit

Permalink
try to simplify motor pack updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Iris-TheRainbow committed Jun 21, 2024
1 parent b8f0f38 commit 47274dc
Showing 1 changed file with 12 additions and 33 deletions.
45 changes: 12 additions & 33 deletions REVHubInterface/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ def update_ms0(self, event):

def update_ms1(self, event):
self.ms_1_callback()

1

class dc_motor():
def __init__(self, root, speed_slider_callback, speed_button_callback, java_button_callback):
Expand Down Expand Up @@ -515,8 +515,6 @@ def __init__(self, root, java_button_callback):
self.root.grid_rowconfigure(0, weight=1)
self.Frame_5 = tkinter.ttk.Frame(root)
self.pid_pack = tkinter.ttk.Labelframe(self.Frame_5)
self.ZeroButton = tkinter.ttk.Button(self.pid_pack)
self.Speed_slider = tkinter.ttk.Scale(self.pid_pack)
self.pid_values = tkinter.ttk.Label(self.pid_pack)
self.Java_label = tkinter.ttk.Label(self.pid_pack)
self.Speed_button = tkinter.ttk.Button(self.pid_pack)
Expand Down Expand Up @@ -688,54 +686,35 @@ def send_all_KA(self):
module.getStatus()

def speedMotorSlider(self, speed, moduleNumber, motorNumber, *args):
self.Motor_packs[moduleNumber * 4 + motorNumber].Java_entry.delete(0, END)
self.Motor_packs[moduleNumber * 4 + motorNumber].Java_entry.insert(0, '%.2f' % (float(speed) / 32000))
self.REVModules[moduleNumber].motors[motorNumber].disablePIDs()
self.REVModules[moduleNumber].motors[motorNumber].setPower(float(speed))
self.REVModules[moduleNumber].motors[motorNumber].enable()
self.repetitiveFunctions = [
(lambda: self.send_all_KA())]
self.repetitiveFunctions.append((lambda: self.updateMotorLabels(motorNumber, moduleNumber)))
return True
self.updateMotorPower(motorNumber, moduleNumber, float(speed))

def zeroMotorSpeed(self, motorNumber, moduleNumber, *args):
speed = 0
self.Motor_packs[moduleNumber * 4 + motorNumber].Speed_slider.set(speed)
self.Motor_packs[moduleNumber * 4 + motorNumber].Java_entry.delete(0, END)
self.Motor_packs[moduleNumber * 4 + motorNumber].Java_entry.insert(0, '%.2f' % float(speed / 32000))
self.REVModules[moduleNumber].motors[motorNumber].disablePIDs()
self.REVModules[moduleNumber].motors[motorNumber].setPower(float(speed))
self.REVModules[moduleNumber].motors[motorNumber].enable()
self.repetitiveFunctions = [
(lambda: self.send_all_KA())]
self.repetitiveFunctions.append((lambda: self.updateMotorLabels(motorNumber, moduleNumber)))
return True
self.updateMotorPower(motorNumber, moduleNumber, 0)

def javaMotorEntry(self, motorNumber, moduleNumber, *args):
try:
speed = float(self.Motor_packs[moduleNumber * 4 + motorNumber].Java_entry.get())
except ValueError:
print('Invalid speed entered: ' + self.Motor_packs[moduleNumber * 4 + motorNumber].Java_entry.get())
return False

self.updateMotorPower(motorNumber, moduleNumber, speed)

def updateMotorPower(self, motorNumber, moduleNumber, speed, *args):
self.Motor_packs[moduleNumber * 4 + motorNumber].Speed_slider.set(speed * 32000)
self.REVModules[moduleNumber].motors[motorNumber].setMode(0, 1)
self.REVModules[moduleNumber].motors[motorNumber].disablePIDs()
self.REVModules[moduleNumber].motors[motorNumber].setPower(float(speed * 32000))
self.REVModules[moduleNumber].motors[motorNumber].enable()
self.repetitiveFunctions = [
(lambda: self.send_all_KA())]
self.Motor_packs[moduleNumber * 4 + motorNumber].Java_entry.delete(0, END)
self.Motor_packs[moduleNumber * 4 + motorNumber].Java_entry.insert(0, '%.2f' % (float(speed)))
self.REVModules[moduleNumber].motors[motorNumber].setPower(speed)
self.repetitiveFunctions = [(lambda: self.send_all_KA())]
self.repetitiveFunctions.append((lambda: self.updateMotorLabels(motorNumber, moduleNumber)))
return True

def javaTargetEntry(self, motorNumber, moduleNumber, *args):
self.zeroMotorSpeed(motorNumber, moduleNumber)
target = int(self.pid_packs[moduleNumber * 4 + motorNumber].Java_entry.get())
self.REVModules[moduleNumber].motors[motorNumber].setTargetPosition(target, 1)
self.REVModules[moduleNumber].motors[motorNumber].setMode(2, 2)
self.REVModules[moduleNumber].motors[motorNumber].setTargetVelocity(32000)
self.REVModules[moduleNumber].motors[motorNumber].enable()

self.repetitiveFunctions = [(lambda: self.send_all_KA())]
self.repetitiveFunctions.append((lambda: self.updateMotorLabels(motorNumber, moduleNumber)))
return True
Expand Down Expand Up @@ -1021,7 +1000,7 @@ def on_connect_button_callback(self):
frame.grid(row=motorNumber, column=moduleNumber, sticky=(N, S, E, W))
self.Motor_packs.append(
dc_motor(frame, partial(self.speedMotorSlider, motorNumber=motorNumber, moduleNumber=moduleNumber),
partial(self.speedMotorEntry, motorNumber=motorNumber, moduleNumber=moduleNumber),
partial(self.zeroMotorSpeed, motorNumber=motorNumber, moduleNumber=moduleNumber),
partial(self.javaMotorEntry, motorNumber=motorNumber, moduleNumber=moduleNumber)))
self.Motor_packs[-1].Motor_pack.config(
text='Module: ' + str(moduleNumber) + ' Motors: ' + str(motorNumber))
Expand Down

0 comments on commit 47274dc

Please sign in to comment.