FIRST Robot Simulator - LedObject


What Is Simulated

LedObject simulates a single LED such as is on the Operator Interface Module supplied by Innovation First with the 2004 FIRST Robotic kit.


Where Used

LedObject.tcl will typically be sourced in OperatorInterface.tcl. One or more declarations for Led_new will then typically be created in OperatorInterface.tcl.

But it can be also useful in TheRobot.tcl to simulate LEDs that the team chooses to install on the robot itself.


GUI View

LEDs are very simple devices. They respond to the value of a variable by switching between the off state and the on state.

In the example shown, there are two green LEDs. The bottom one, green0, is off. The top one, green1, is on.


Programmers View

There are two proc[eedures] in LedObject.tcl. The first few lines of each proc lists the parameters required.

  proc Led_new { {LedName} {NameOnCanv} {expression} \
		     {Xs} {Ys} {ColorOn} {ColorOff} } \
  {
     # LedName = Unique name for the LED.  Also the label on the LED.
     # NameOnCanv = Name of this object relative to the frame or
     #		CanvasObject on which it lives.  Usually it is best
     #		to let Canvas_IncludeObject provide this name.
     # expression = The expression being monitored,
     #		generally using fully qualified (::thisvar) names.
     # Xs and Ys = Size of LED.
     # ColorOn and ColorOff = Color of LED when expression!=0 or ==0.
  

The parameters LedName and NameOnCanv are constants. The parameter expression is evaluated each time Led_Update is called. It can be an arbitrarily complex expression, but the output will be viewed as boolean.

   # Given a LedName, the corresponding object is updated, depending
   # on the value returned from the expression.
   # Typically, Led_Update will be called by CanvasObject, and does
   # not need to be handled by the user.
  proc Led_Update { {LedName} } \
  {
  

As the comment implies, we really don't have to do anything special with Led_Update as long as we let CanvasObject manage it. See the example below.


Example

First we declare a CanvasObject using Canvas_new.

  set canvasname [Canvas_new led .oper_interf.left \
                      {#7f6f7f} {#000000}  {Led Monitor}]
  

We can now declare one or more LEDs to be managed by it.

  set labelname [Canvas_IncludeObject led Led_Update green1But]
  Led_new green1But $labelname {$::LED_byte1 & 0x02} 7 3 "#00ff00" "#006000"
  pack $labelname -side top
  set labelname [Canvas_IncludeObject led Led_Update green0But]
  Led_new green0But $labelname {$::LED_byte1 & 0x01} 7 3 "#00ff00" "#006000"
  pack $labelname -side top
  

In this example we are extracting two bits from the variable ::LED_byte1. Those bits are 0 (0x01) and 1 (0x02).

The ON color is bright green, #00ff00. The OFF color is dim green, #006000.


Caveats

The LEDs are only updated to match the expression when the corresponding Canvas_UpdateAll is called.



Last modified 4 Apr 2004