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 TheOperatorInterface.tcl. One or more declarations for ::Led::new will then typically be created in TheOperatorInterface.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[edures] in LedObject.tcl. The first few lines of each proc lists the parameters required.

  proc 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.
   # You need to set up ::Led::update to be called by CanvasObject
   # or NotifyObject.
  proc update { {LedName} } \
  {
  

Example

First we declare a CanvasObject using ::Canvas::new. And then we subscribe that Canvas to be Notified every cycle.

  set canvasname [::Canvas::new led .oper_interf.left \
                      {#7f6f7f} {#000000}  {Led Monitor}]
  ::Notify::Subscribe Cycle ::Canvas::UpdateAll led
  

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.

If you create LEDs but they come out a dull color rather than the color you requested it is possible they are not being updated. If they are part of a CanvasObject, ensure that object is told how to update them. And ensure the CanvasObject is getting updates. If they are not part of a CanvasObject then the LEDs need to use the "::Notify::Subscribe Cycle" mechanism.


Caveats

The LEDs are only updated to match the expression when the corresponding "::Canvas::UpdateAll" or "::Notify::Publish" is called.



Last modified 11 Dec 2006
http://brown.armoredpenguin.com/~abrown/contact.html
http://brown.armoredpenguin.com/~abrown/first/first2006/Scalawags/Simulator2006/LedObject.html