Skip to content

Commit

Permalink
Add pyfrc tk simulation extension to show CANTalon
Browse files Browse the repository at this point in the history
  • Loading branch information
virtuald committed Feb 27, 2017
1 parent a2a6088 commit ad7cfef
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 4 deletions.
7 changes: 4 additions & 3 deletions ctre/_impl/cantalon_sim.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,15 +124,14 @@ def __init__(self, deviceNumber, controlPeriodMs, enablePeriodMs):
self.deviceNumber = deviceNumber

# Initialize items based on their param type
hal_data['CAN'][deviceNumber] = NotifyDict({
self.hal_data = NotifyDict({
v: 0 for v in _srx_param_map.values()
})

self.hal_data = hal_data['CAN'][deviceNumber]

# Initialize non-zero items or items that don't have an associated parameter
self.hal_data.update({
'type': 'talonsrx',
'sim_display': False, # used in sim

'override_limit_switch': 0,
'override_braketype': None,
Expand All @@ -158,6 +157,8 @@ def __init__(self, deviceNumber, controlPeriodMs, enablePeriodMs):
'mp_outputEnable': TalonSRXConst.kMotionProfile_Disable
})

hal_data['CAN'][deviceNumber] = self.hal_data

def Destroy(self):
del hal_data['CAN'][self.deviceNumber]
del self.hal_data
Expand Down
117 changes: 117 additions & 0 deletions ctre/_impl/sim_ui.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@

import tkinter as tk
from pyfrc.sim.ui_widgets import CheckButtonWrapper, Tooltip, ValueWidget

from hal_impl.data import hal_data
from .constants import TalonSRXConst as tsrxc

#from hal import TalonSRXConst as tsrxc

class CtreUI:

can_mode_map = {
tsrxc.kMode_CurrentCloseLoop: 'PercentVbus',
tsrxc.kMode_DutyCycle:'PercentVbus',
tsrxc.kMode_NoDrive:'Disabled',
tsrxc.kMode_PositionCloseLoop:'Position',
tsrxc.kMode_SlaveFollower:'Follower',
tsrxc.kMode_VelocityCloseLoop:'Speed',
tsrxc.kMode_VoltCompen:'Voltage'
}

def __init__(self):
self.can = {}

def update_tk_widgets(self, sim):

for k, data in hal_data['CAN'].items():

if data['type'] != 'talonsrx':
continue

if not data['sim_display']:
self._add_CAN(sim, k, data)
data['sim_display'] = True

(motor, fl, rl, mode_lbl_txt, enc_txt, analog_txt, pwm_txt) = self.can[k]
data = hal_data['CAN'][k]
mode = data['mode_select']
mode_lbl_txt.set(self.can_mode_map[mode])
#change how output works based on control mode
if mode == tsrxc.kMode_DutyCycle :
#based on the fact that the vbus has 1023 steps
motor.set_value(data['value']/1023)

elif mode == tsrxc.kMode_VoltCompen:
#assume voltage is 12 divide by muliplier in cantalon code (256)
motor.set_value(data['value']/12/256)

elif mode == tsrxc.kMode_SlaveFollower:
#follow the value of the motor value is equal too
motor.set_value(self.data[data['value']][0].get_value())
#
# currently other control modes are not correctly implemented
#
else:
motor.set_value(data['value'])

enc_txt.set('E: %s' % data['enc_position'])
analog_txt.set('A: %s' % data['analog_in_position'])
pwm_txt.set('P: %s' % data['pulse_width_position'])

ret = fl.sync_value(data['limit_switch_closed_for'])
if ret is not None:
data['limit_switch_closed_for'] = ret

ret = rl.sync_value(data['limit_switch_closed_rev'])
if ret is not None:
data['limit_switch_closed_rev'] = ret

def _add_CAN(self, sim, canId, device):

# TODO: this is not flexible

row = len(self.can)*2

lbl = tk.Label(sim.can_slot, text=str(canId))
lbl.grid(column=0, row=row)

motor = ValueWidget(sim.can_slot, default=0.0)
motor.grid(column=1, row=row)
sim.set_tooltip(motor, 'CAN', canId)

fl = CheckButtonWrapper(sim.can_slot, text='F')
fl.grid(column=2, row=row)

rl = CheckButtonWrapper(sim.can_slot, text='R')
rl.grid(column=3, row=row)

Tooltip.create(fl, 'Forward limit switch')
Tooltip.create(rl, 'Reverse limit switch')

mode_lbl_txt = tk.StringVar(value = self.can_mode_map[device['mode_select']])
mode_label = tk.Label(sim.can_slot, textvariable=mode_lbl_txt)
mode_label.grid(column=4, row=row)

labels = tk.Frame(sim.can_slot)
labels.grid(column=0, row=row+1, columnspan=6)

enc_value = tk.StringVar(value='E: 0')
enc_label = tk.Label(labels, textvariable=enc_value)
enc_label.pack(side=tk.LEFT)

analog_value = tk.StringVar(value='A: 0')
analog_label = tk.Label(labels, textvariable=analog_value)
analog_label.pack(side=tk.LEFT)

pwm_value = tk.StringVar(value='P: 0')
pwm_label = tk.Label(labels, textvariable=pwm_value)
pwm_label.pack(side=tk.LEFT)

Tooltip.create(enc_label, "Encoder Input")
Tooltip.create(analog_label, "Analog Input")
Tooltip.create(pwm_label, "PWM Input")

self.can[canId] = (motor, fl, rl, mode_lbl_txt, enc_value, analog_value, pwm_value)


4 changes: 3 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,5 +182,7 @@ def build_extensions(self):
install_requires=install_requires,
cmdclass=cmdclass,
zip_safe=False,
entry_points={'robotpylib': ['info = ctre._impl.info:Info']}
entry_points={
'robotpylib': ['info = ctre._impl.info:Info'],
'robotpysim': ['ctre = ctre._impl.sim_ui:CtreUI']}
)

0 comments on commit ad7cfef

Please sign in to comment.