Author Topic: Get Current Servo Position  (Read 1131 times)

bbfreak

  • Newbie
  • *
  • Posts: 20
Get Current Servo Position
« on: December 27, 2009, 03:30:19 PM »
Hey guys,
      What im trying to do is this when you click a button it moves a servo: (current servo position + 20). Is this possible? Instead of making a different widget for every position Im wondering if its possible to move it like this.

Thanks

iobridge

  • Administrator
  • Hero Member
  • *****
  • Posts: 669
    • ioBridge Support
Re: Get Current Servo Position
« Reply #1 on: December 27, 2009, 04:24:17 PM »
All it takes is a little JavaScript added to the head of the HTML page.

1) Add "moveServo" JS to head
2) Embed Variable Servo widget to the body and hide it
3) Add buttons or links to send the direction and distance to "moveServo"

Here is a sample (you would replace the widgetID with yours):

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')">

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>

More detail: http://www.iobridge.net/forum/index.php/topic,94.0.html
ioBridge Support
Community Team

bbfreak

  • Newbie
  • *
  • Posts: 20
Re: Get Current Servo Position
« Reply #2 on: December 28, 2009, 02:04:37 AM »
thanks :) worked like a charm

1 more question. Is is possible to change the speed of the servo? Its going very fast. Is this possible?

iobridge

  • Administrator
  • Hero Member
  • *****
  • Posts: 669
    • ioBridge Support
Re: Get Current Servo Position
« Reply #3 on: December 28, 2009, 02:13:50 AM »
Yes, on the module settings tab, find the channel that the servo controller is on and click "Settings". This should pop open specific settings for the Servo Controller and one of them is speed. The default is 100 - which is the fastest.
ioBridge Support
Community Team

iobridge

  • Administrator
  • Hero Member
  • *****
  • Posts: 669
    • ioBridge Support
Re: Get Current Servo Position
« Reply #4 on: December 28, 2009, 02:48:34 AM »
I added this source code to the Wiki:

http://www.iobridge.net/wiki/javascript
ioBridge Support
Community Team

bbfreak

  • Newbie
  • *
  • Posts: 20
Re: Get Current Servo Position
« Reply #5 on: December 28, 2009, 03:23:22 AM »
awesome, also 1 quick question. say i had code to move 2 servos at the exact same time. How can i make sure this is done? They are both connected to the same object so they must both move within .1 seconds of each other or else it wont work. Is there a trick to this?

iobridge

  • Administrator
  • Hero Member
  • *****
  • Posts: 669
    • ioBridge Support
Re: Get Current Servo Position
« Reply #6 on: December 28, 2009, 05:28:45 AM »
The onboard actions would get you multiple movements.

Those are found on the Actions tab - look for Onboard Rules and Actions. It will take some getting used to. But, you use Variables to make sure Servo positions are sent once.

http://www.iobridge.net/wiki/onboard-actions
ioBridge Support
Community Team

bbfreak

  • Newbie
  • *
  • Posts: 20
Re: Get Current Servo Position
« Reply #7 on: January 02, 2010, 04:45:04 AM »
alright, i've been playing around with the rules for a couple days now. and i'm wondering how or if its possible to use the javascript servo position with these rules. I need to move 2 servos at the same time in increments of 20. How would i end up doing this? With just 2 buttons a down and up. I hope this makes sense.

iobridge

  • Administrator
  • Hero Member
  • *****
  • Posts: 669
    • ioBridge Support
Re: Get Current Servo Position
« Reply #8 on: January 02, 2010, 05:03:13 PM »
OK, I have a solution for you. It's not intuitive, but it's possible.

1) Create a Variable Pulse Count Control Widget
2) Embed that widget into the following source code
3) Create onboard rules for certain pulse counts (see attached screenshot)

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);

}
 
</script>
</head>
 
<body>
 
Click the arrow buttons to move the servo Clockwise or Counter-clockwise.
<br /><br />
 
<form><input type="button" Name="CounterClockWise" Value="<-" onClick="moveServo('SbuaA7xrLoGn', 'CCW', '100')"><input type="button" Name="Home" Value="--" onClick="widgetSetString('SbuaA7xrLoGn', '1500')"><input type="button" Name="ClockWise" Value="->" onClick="moveServo('SbuaA7xrLoGn', 'CW', '100')"></form>
 
<div id="ioWidgets" style="display:none;">
<script type="text/javascript">document.write(unescape("%3Cscript src='" + "http://www.iobridge.com/widgets/io.js?SbuaA7xrLoGn' type='text/javascript'%3E%3C/script%3E"));</script>
</div>
 
</body>
</html>

Considerations:

My servos (1 and 2) were on module channel 4. The home position is 1500. The left and right buttons increment by 100. There is a rule for each step. I made a few of them for the example. The draw back is that you have to have the steps created as an onboard rule. So, when a pulse count hits a certain number you can move both servos.

It will look a little crazy at first, but it does work...!!!??? Let me know it you try it out and what your results are.
ioBridge Support
Community Team

bbfreak

  • Newbie
  • *
  • Posts: 20
Re: Get Current Servo Position
« Reply #9 on: January 02, 2010, 05:49:20 PM »
alright that makes sense, only problem is when i try to make the variable pulse count widget it says i need a firmware upgrade. however its using version c4.2. so i dont think i can upgrade anymore.


edit: nevermind, i guess it wouldn't let me create the widget without first connecting the iobridge.. weird

bbfreak

  • Newbie
  • *
  • Posts: 20
Re: Get Current Servo Position
« Reply #10 on: January 03, 2010, 12:49:47 AM »
alright i did everything. and when i send the pulse the iobridge reads it with an incoming channel but they arent moving. Do i need a board to send the pulses? I also manually set the pulse value to 1500 and 1600 and they don't move either.

also, in the code here:

Code: [Select]
<form><input type="button" Name="CounterClockWise" Value="<-" onClick="moveServo('GPk3hs1HazZV', 'CCW', '100')"><input type="button" Name="Home" Value="--" onClick="widgetSetString('GPk3hs1HazZV', '1500')"><input type="button" Name="ClockWise" Value="->" onClick="moveServo('GPk3hs1HazZV', 'CW', '100')"></form>
 
<div id="ioWidgets" style="display:none;">
<script type="text/javascript">document.write(unescape("%3Cscript src='" + "http://www.iobridge.com/widgets/io.js?GPk3hs1HazZV' type='text/javascript'%3E%3C/script%3E"));</script>

i set the widget id to the variable pulse count correct?
« Last Edit: January 03, 2010, 12:54:51 AM by bbfreak »

iobridge

  • Administrator
  • Hero Member
  • *****
  • Posts: 669
    • ioBridge Support
Re: Get Current Servo Position
« Reply #11 on: January 03, 2010, 01:30:15 AM »
No board is needed for the pulse count, just use a channel that is not using the servo board.

Your variable pulse count widgetID should be in the code that you highlighted.

When you created all of the rules, did you hit "Sync Rules"?
ioBridge Support
Community Team

bbfreak

  • Newbie
  • *
  • Posts: 20
Re: Get Current Servo Position
« Reply #12 on: January 05, 2010, 03:36:08 AM »
i found a little problem in the code you posted:
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);

}
 
</script>
</head>
 
<body>
 
Click the arrow buttons to move the servo Clockwise or Counter-clockwise.
<br /><br />
 
<form><input type="button" Name="CounterClockWise" Value="<-" onClick="moveServo('SbuaA7xrLoGn', 'CCW', '100')"><input type="button" Name="Home" Value="--" onClick="widgetSetString('SbuaA7xrLoGn', '1500')"><input type="button" Name="ClockWise" Value="->" onClick="moveServo('SbuaA7xrLoGn', 'CW', '100')"></form>
 
<div id="ioWidgets" style="display:none;">
<script type="text/javascript">document.write(unescape("%3Cscript src='" + "http://www.iobridge.com/widgets/io.js?SbuaA7xrLoGn' type='text/javascript'%3E%3C/script%3E"));</script>
</div>
 
</body>
</html>

It only starts working when i click the home button. Is there a way to not use the home button?

iobridge

  • Administrator
  • Hero Member
  • *****
  • Posts: 669
    • ioBridge Support
Re: Get Current Servo Position
« Reply #13 on: January 06, 2010, 02:20:30 AM »
In some browsers you can add an "onload" to the body tag.

When the page loads it can set the default position.
ioBridge Support
Community Team