Author Topic: Servo control from javascipt with widgetSetString & widgetGetString  (Read 2613 times)

jason

  • Administrator
  • Hero Member
  • *****
  • Posts: 502
    • ioBridge.com
Since we've added the new 'Variable Servo Position" widget, we are able to expose a few more control functions in javascript.

widgetGetString

- use this to get the position from a variable servo position widget
- the format is widgetGetString('WidgetID'), where WidgetID is the 12 character widget ID

widgetSetString

- use this to set the servo position
- the format is widgetSetString('WidgetID','Position'), where WidgetID is the 12 character widget ID and a PWM servo position

Just like the other ioBridge javascript functions, you'll need to actually include the widget on the webpage for these to work.  However you can hide the widget from view if you want.

As an example, I used these two new function to make a servo increment/decrement type control on for my fishcam.

http://www.picobay.com/fishcam/fishcamInc.html

Here is the source code:
Code: [Select]
<html>
<title>Servo Control Example</title>
<head>

<script type="text/javascript">

function moveServo(widgetID, direction, distance){
 
  distance = parseInt(distance);
  current = parseInt(widgetGetString(widgetID));

  if (direction == "CCW") {
       distance = current-distance;
  }
  else {
       distance = current+distance;
  }

   widgetSetString(widgetID, distance);

   current = Math.round(29 - (current - 800) * 60 / 500);

   document.getElementById("ServoPosition").innerHTML = current + "°";
}

</script>
</head>

<body onload="moveServo('k7bgNMyCTW9b', 'CW', '0')">
<iframe name="livecam" id="livecam" width="320" height="240" src="http://www.picobay.com/fishcam/inside.html" frameborder="0" allowtransparency="false" scrolling="no" ></iframe>
<br><br>

Click the arrow buttons to move pan the camera.
<br><br>

<form><input type="button" Name="CamLeft" Value="<-" onClick="moveServo('k7bgNMyCTW9b', 'CW', '40')"><input type="button" Name="CamRight" Value="->" onClick="moveServo('k7bgNMyCTW9b', 'CCW', '40')"></form>

Camera Servo Positon = <a id="ServoPosition"></a>

<div id="ioWidgets" style="display:none;">
<script type="text/javascript">document.write(unescape("%3Cscript src='" + "http://www.iobridge.com/widgets/io.js?k7bgNMyCTW9b' type='text/javascript'%3E%3C/script%3E"));</script>
</div>

</body>
</html>



« Last Edit: January 28, 2009, 11:52:58 PM by jason »
Jason Winters
ioBridge Developer

thecapacity

  • Newbie
  • *
  • Posts: 15
    • My Blog
Re: Servo control from javascipt with widgetSetString & widgetGetString
« Reply #1 on: January 10, 2009, 07:31:10 AM »
This actually answers my other Q but why is there no need to call WidgetExecute() w/ this?

iobridge

  • Administrator
  • Hero Member
  • *****
  • Posts: 669
    • ioBridge Support
Re: Servo control from javascipt with widgetSetString & widgetGetString
« Reply #2 on: January 10, 2009, 11:28:18 AM »
widgetSetString sets the position and executes the new input (all in one action). We are going to use this command for several new widgets that we are releasing soon, that's why it's a little generic. We are also putting together some documentation.
ioBridge Support
Community Team