Phidget handler (phid://)

Background

Phidget is a company that provides products for USB Sensing and Control. Phidget specialize in making affordable, easy to use sensors and controllers that require minimal electronics knowledge. Phidget covers vast range of sensors and motors.

Sensors

Phidget Protocol Handler Flow

TQLEngine provides phidget protocol handler, which is essentially a wrapper over the Phidget Driver that is provided by Phidget. Note that the developers are required to install the phidget driver for different platform (Linux, Windows, Mac) from the phidget website.

Phidget Protocol Handler Parameters

Phidget has a broad range of sensors and motors. The phidget protocol handler provides wrapper implementation for following Device Types. Adding support for additional device types will be provided on a need basis.

Phidget Input Parameter List

NameDescriptionHow Used?

DeviceType

Specify the type of device that requires interaction. Supported values are:

PhidgetRFID, PhidgetTextLCD, PhidgetGPS, PhidgetInterfaceKit, PhidgetAdvancedServo

DeviceType is passed as a modifier to Invoke.

<Invoke Method="Get" DeviceType="PhidgetInterfaceKit"/>

SerialNumberThis represents the interface port number to which the sensor or motor is connected.SerialNumber is passed as a modifier to Invoke
ServoAngleAngle to be applied to a Advanced Servo Motor.

ServoAngle is passed as a payload to Invoke wrapped within

Message/Value container.

<Invoke Method="Post" DeviceType="PhidgetTextLCD">

<Message><Value><ServoAngle>110.0</ServoAngle></Value></Message>

</Invoke>

SimulatedSimulates Sensor Behavior by sending random integer values between 1 and 100 
-

Some time if you need to pass a String value to the handler as input - usually in case of RFID Writer

or LCD Text Display, you can pass it as part of <Message><Value>Hello</Value></Message> as payload to Invoke.

Payload of <Invoke>.

<Invoke Method="Post" DeviceType="PhidgetTextLCD">

<Message><Value>Hello</Value></Message>

 </Invoke>

 

Phidget Output Format

Output from the Phidget Handler is simply a text value.

Special formatted case for GPS Sensors - In case of GPS the output text is aggregated and presented in a single formatted text as below: 

Phidget Handler GPS Output Text Format
Lon:<value>;Lat:<value>;Alt:<Value>;Vel:<Value>;Head:<Value>;Status:<Value>
Default Value for Lon, Lat, Alt, Vel, Head is: Unknown
Default Value for Status is false

Examples

Running Phidget Handler in Simulated Mode

Phidget Handler Thing Facet in Simulated mode
<ThingFacet Name="PhidgetSimulatedFacet">
  <Integer Name="SimulatedValue" Update="auto" KnownBy="PhidgetSimAction"/>
  <String Name="DeviceType"/>
  <String Name="SerialNumber"/>
  <Action Name="PhidgetSimAction" Documentation="Returns integer values from 1 - 100 in Random order">
    <Workflow Limit="1" Live="1" Timeout="-1">
      <Task Name="Main" While="True">
        <Event Name="Argument" As="ActionArgument"/>
        <Invoke Name="ReadValue" waitFor="Argument" Get="phid://" SerialNumber="[%:Event.Argument.SerialNumber.Value:%]" DeviceType="[%:Event.Argument.DeviceType.Value:%]" Simulated="true"/>
        <Output Name="Result" As="ActionResult">
          <Value>
            <SimulatedValue>[%:[%:@Output:%]Invoke.ReadValue.Message.Value:%]</SimulatedValue>
          </Value>
        </Output>
      </Task>
    </Workflow>
  </Action>
</ThingFacet>

Reading Data from Sensors Connected to Phidget Interface Kit

There are few important points to be noted here:

  • Note that PhidgetInterfaceKit is usually a multi port device (2/2/2 or 8/8/8) i.e it has 8 USB, and 8 GPI and 8 8 GPO pins.
  • When there are multiple sensors attached to the InterfaceKit, there will be multiple instances created in ThingModel to which PhidgetSensorFacet is attached.
  • Even though there is one single USB connection, the Protocol handler supports multiplexing of data to different Channel.
  • Therefore when we do a Invoke with Get="phid://[%:Event.Argument.SerialNumber.Value:%]" we attach the serial number to make the invocation unique.
  • Internally there will be multiple listeners attached to the USB serial port each receiving data and saving to SensorValue.

Phidget Handler Thing Facet to Read Sensors
<ThingFacet Name="PhidgetSensorFacet">
  <Integer Name="SensorValue" Update="auto" KnownBy="PhidgetSensorAction"/>
  <String Name="DeviceType"/>
  <String Name="SerialNumber"/>
  <String Name="SensorName"/>
  <Action Name="PhidgetSensorAction" Documentation="Returns integer values from 1 - 100 in Random order">
    <Workflow Limit="1" Live="1" Timeout="-1">
      <Task Name="Main" While="True">
        <Event Name="Argument" As="ActionArgument"/>
        <Invoke Name="ReadValue" waitFor="Argument" Get="phid://[%:Event.Argument.SerialNumber.Value:%]" SerialNumber="[%:Event.Argument.SerialNumber.Value:%]" DeviceType="[%:Event.Argument.DeviceType.Value:%]" Simulated="false"/>
        <Output Name="Result" As="ActionResult">
          <Value>
            <SensorValue>[%:[%:@Output:%]Invoke.ReadValue.Message.Value:%]</SensorValue>
          </Value>
        </Output>
      </Task>
    </Workflow>
  </Action>
</ThingFacet>
<!-- Combine with Model -->
<ThingModel Name="PhidgetSensors" Combines="PhidgetSensorFacet">
   <Sid Name="SensorID"/>
</ThingModel>
<!-- Activate Sensor -->
<Query>
  <Save format="version,current">
    <PhidgetSensors>
      <SensorName>Rotation</SensorName>
      <DeviceType>PhidgetInterfaceKit</DeviceType>
      <SerialNumber>0</SerialNumber>
      <SensorValue value="$Null()"/>
    </PhidgetSensors>
  </Save>
</Query>

 

Reading Data from a GPS Sensor

  • Note that GPS sensor is directly attached to the USB of controller i.e (Raspberry Pi or Laptop or VM)
  • If there are multiple GPS sensors you plan to attach you can use SerialNumber create a unique protocol string i.e. phid://1 etc
Phidget Handler Thing Facet to Read GPS Sensor
<ThingFacet Name="GPSPhidgetSensorFacet">
	<String Name="GPSOutput" update="auto" KnownBy="GPSPhidgetSensorAction" />
    <String Name="SerialNumber" />
	<String Name="DeviceType" />
	<Action Name="GPSPhidgetSensorAction" Documentation="Connect to GPS sensor">
	<Workflow Limit="1" Live="1" Timeout="-1">
		<Task name="Main" while="true">
				<Event name="Argument" as="ActionArgument" />
				<Invoke name="ReadValue" waitFor="Argument"
                       Post="phid://[%:Event.Argument.SerialNumber.Value:%]" SerialNumber="[%:Event.Argument.SerialNumber.Value:%]"
						DeviceType="[%:Event.Argument.DeviceType.Value:%]"/>
                <!-- Optionally parse the string if desired -->
				<Output name="Result" as="ActionResult">
					<Value>
						<GPSOutput>[%:[%:@Output:%]Invoke.ReadValue.Message.Value:%]</GPSOutput>
					</Value>
				</Output>
			</Task>
		</Workflow>
	</Action>
</ThingFacet>

Display Text on LCD Screen.

Phidget Handler Thing Facet display text on LCD Screen
<ThingFacet Name="PhidgetTextLCDFacet">
	<String Name="DisplayString" KnownBy="LCDDisplayAction" Documentation="This is a runtime parameter" />
	<String Name="DeviceType" />
	<Action Name="LCDDisplayAction"
		Documentation="Display the string to Phidget Text LCD">
		<Workflow Limit="1" Live="1" Timeout="-1">
			<Task name="Main" while="true">
				<Event name="ActionArgument" as="ActionArgument" />
				<Log Message="******Invoking Phidget Text LCD"/>
				<Invoke name="WriteToLCD" waitFor="ActionArgument"
					Post="[%:Event.Argument.SensorURL.Value:%]" 
					DeviceType="[%:Event.Argument.DeviceType.Value:%]">
				 <Message Type="text" Value="[%:Event.Argument.DisplayString.Value:%]" />
				 </Invoke>
				<Log Message="Invoked Phidget Text LCD"/>
				<Output name="Result" as="ActionResult">
					<Value>
						<DisplayString>[%:[%:@Output:%]Invoke.WriteToLCD.Message.Value:%]</DisplayString>
					</Value>
				</Output>
			</Task>
		</Workflow>
	</Action>
</ThingFacet>
<!-- Combine with Model -->
<ThingModel Name="PhidgetLCDScreenModel" Combines="PhidgetTextLCDFacet">
   <Sid Name="LCDScreenID"/>
</ThingModel>
<!-- Write Text for first time -->
<Query>
  <Save format="version,current">
    <PhidgetLCDScreenModel>
      <SensorName>Rotation</SensorName>
      <DeviceType>PhidgetTextLCD</DeviceType>
      <DisplayString>Hello TQL!</DisplayString>
    </PhidgetLCDScreenModel>
  </Save>
</Query>
<!-- Subsequent Writes can happen via Update Query -->
<Query>
 <Find format="version"> 
		<PhidgetLCDScreenModel>
			<LCDScreenID ne=""/>
		</PhidgetLCDScreenModel>
	</Find>
	<SetResponseData> 
		<key>Message.Value.Find.Result.PhidgetLCDScreenModel.DisplayString.Value</key>
		<value>Hello Again!!</value>
	</SetResponseData>
	<Update> 
		<from>Result</from>
		<Include>$Response.Message.Value.Find</Include>
	</Update>
</Query>
 

Driver Installation Steps

Installing Phidget Driver

Linux: - To install Phidget libraries on your Linux system, the following steps must be performed.

  • Install libusb-1.0 development libraries (libusb-1.0-0-dev). This libraries can be installed from the source (http://www.libusb.org/) or if you are running a debian based  system you can simply run the command shown below    
Command to install USB Lib
sudo apt-get install libusb-1.0-0-dev

Note: - For additional information and troubleshooting visit the following website (http://www.phidgets.com/docs/OS_-_Linux)

Â