JoystickObject simulates an analog joystick similar to the one suppied with the 2004 FIRST Robotic kit. It has two analog outputs for x and y. It also has four buttons.
JoystickObject.tcl will typically be sourced in TheOperatorInterface.tcl. One or more declarations for ::Joystick::new will then typically be created in TheOperatorInterface.tcl.
|
The knob of the joystick is manipulatable by the user by click-and-drag with his mouse. Clicking on a button will toggle it. In this image, the joystick has been shifted up and a little to the right. The variable names for x and y and their values are shown near the middle of the image. In the example shown, joystick x is controlling the variable oi_an05 and is currently set to decimal 100 based on the position of the joystick knob. The variable names for the switches are shown to the right of their buttons. In the example shown, all are set to 0 except for joy_trig4 which is 1. |
There are two proc[edures] in JoystickObject.tcl. The first few lines of each proc lists the parameters required.
proc new { {JoystickName} {NameOnCanv} \
{joyX} {joyY} {joy_trig} {joy_top} {joy_aux1} {joy_aux2}
{Fg} {Bg} } \
{
  # $joyX and $joyY = names of the variables controlled by the joystick.
  # $joy_trig = name of the joystick trig button.
  # $joy_top = name of the joystick top button.
  # $joy_aux1 and $joy_aux2 = names of two more joystick buttons.
  # $Fg and $Bg = Foreground and background colors for the joystick.
The parameters JoystickName and NameOnCanv are constants. The parameters joyX, joyY, joy_trig, joy_top, joy_aux1, and joy_aux2 are the names of variables that you provide to JoystickObject. It will set those variables based on the operator's interactions with the joystick on the GUI of the simulator.
  # ::Joystick::update does not need to be explicitly called by the
  # user. That is handled by the bindings in side of ::Joystick::new.
proc update { {JoystickName} {ScreenX} {ScreenY} } \
{
  # ScreenX,ScreenY = Screen coordinates of the mouse.
As the comment implies, we really don't have to do anything special with ::Joystick::update. It will be called by the bind in ::Joystick::new when that is appropriate.
We can put a joystick against the right side of a frame using code like this:
frame .oper_interf.joygroup
::Joystick::new joy4 .oper_interf.joygroup.joy4 \
{::oi_an08} {::oi_an04} \
{::joy_trig4} {::joy_top4} {::joy_aux41} {::joy_aux42} \
{#000000} {#8f8f33}
pack .oper_interf.joygroup.joy4 -side right
The double colons in front of each variable name makes them operate at the root namespace of the Tcl program.
In theory a joystick can put out values from 0 to 255. JoystickObject is a theoretical joystick. Actual joysticks cannot reach the extreme values. But since every joystick's limits are different, and are probably not stable over time and temperature, it makes no sense to simulate a real joystick. What this means is that your robot code needs to be written to gracefully deal with values as extreme as 0 and 255, but not to require them.
This means that the robot joystick reading code should condition the input from the joystick. Any value from the expected high limit up to 255 should probably mapped to the same maximum value. And similarly for the lower limit of the joystick.