Phidgets2 example

Phidgets2 is similar to the Phidgets1 example. The only difference lies in an extra operation that makes sure the servo motor spins in the same direction the rotation sensor is moved. That is, if you turn the rotation sensor clockwise the servo motor also spins clockwise.

Explaining the mix

Figure: Phidgets2 - The mix


The mix comprises a notify, a request/reply, and a consume operation.

The notify is triggered by rotating the sensor and the consume is used to set the position of the motor.

A JavaScript service provisioned on Tabserver inverts the spinning of the servo motor.

Tip: Get the code from the SPARKL public repository.
Table 1. Phidgets2 - The markup
Example Description
<folder name="Phidgets2">
  <field name="positionA" 
    type="integer"/>
  <field name="position" 
    type="integer"/>
  <service name="Connection"
    provision="tabserver_connection"/>
  <service name="Sequencer" 
    provision="sequencer"/>
  <service name="Servo" 
    provision="phidget">
    <prop name="phidget.spec" 
      kind="PhidgetAdvancedServo" 
      domains="domain.local"/>
    <prop name="phidget.params" 
      position0="position"/>
  </service>
  <service name="Rotary" 
    provision="phidget">
    <prop name="phidget.spec" 
      kind="PhidgetInterfaceKit" 
      domains="domain.local"/>
    <prop name="phidget.params" 
      sensor0="positionA"/>
  </service>
  <service name="Inverter" 
    provision="tabserver" 
    dependencies="Connection">
    <prop name="tabserver.browser.src" 
      src="Inverter?prop=tabserver.browser.src" 
      type="application/javascript"><![CDATA[
    ...JavaScript comes here...  
    ]]></prop>
  </service>
  <mix name="Mix">
  ...
  </mix>
</folder>
The Rotary service, which we defined as a client for the Turn operation, is a phidget service. It comprises an interface kit and a rotation sensor connected to it.

Once the rotation sensor is moved, the Rotary service fires a notify. The interface kit measures the rotation (on a scale of 1 to 999).

In the properties of the Rotary service we mapped the sensor to the field positionA ("position0=positionA").

The value of the field positionA, therefore, depends on the position of the rotation sensor.

The Servo service is a phidget service. It comprises a motor controller and a servo motor connected to it.

In the properties of Servo we mapped the position of the servo motor to the field position.

The Servo service, therefore, moves the servo motor to the position that corresponds to the value of the field position.

The Inverter service is provisioned using the Tabserver extension. The Connection service accepts connections from browsers. It is a dependency for the Inverter service.

Inverter has a JavaScript configuration used to subtract the value of the field positionA from 999 in order to harmonise the movement of the servo motor with the rotation sensor.

See Figure 3 on the script.

<notify name="Turn" 
  service="Sequencer" 
  clients="Rotary" 
  fields="positionA"/>
<request name="Invert" 
  service="Inverter" 
  fields="positionA">
  <reply name="Ok" 
    fields="position"/>
</request>
<consume name="SetPosition" 
  service="Servo" 
  fields="position"/>
The Turn notify starts a transaction by outputting the field positionA, with a value depending on the position of the sensor.

The Invert operation is invoked to add the field position to the field set.

The Invert operation is implemented by Inverter, a JavaScript service. This service uses its JavaScript to generate the field position, based on the value of positionA.

The SetPosition consume forms a goal. Servo, the service implementing the consume, expects the field position, which can be collected via the Invert operation.

Figure: JavaScript content of the Inverter service

sparkl.service({
  "Invert" : function(request, reply) {
    var positionA = request.value("positionA");
    var position = 999 - positionA;
    console.log("Inverting " + positionA);
    reply["Ok"]
      .value("position", position)
      .send();
  }
});