An archive of community.esquilo.io as of Saturday January 26, 2019.

Servo + ERPC example

mdoan7

An example of using the browser to control two servos.
--Enjoy

Browser page with sliders for each servo and a sweep button that sweeps one of the servos 0-180-0:


The application nut...

dofile("sd:/LIB/motors/SERVO/SERVO.NUT");

led <- GPIO(LED_BUILTIN);       // LED built into board 
led.output();                   // sets the digital pin as output
 
// /* Control a servo on PWM0 channel 0 (GPIO pin 2 on Esquilo) */
 pwm <- PWM(0)
 servo1 <- Servo(pwm, 0);
 servo1.setPulses(0.70,2.58); //pulse width as measured for my particular servos
 servo1.setRange(0.0,180.0); //degrees
 servo1.setFrequency(50); //frequency

 // /* Control a servo on PWM0 channel 1 (GPIO pin 3 on Esquilo) */
 servo2 <- Servo(pwm, 1);
 servo2.setPulses(0.70,2.58); //pulse width
 servo2.setRange(0.0,180.0); //degrees
 servo2.setFrequency(50); //frequency

function sweep() {
    for (local i = 0.0; i < 180.0; i++) {
        servo1.position(i);
        delay(1);
    }
    servo1.position(0.0);
    led.low();                    // sets the LED off
}

function set_position1 (_angle) {
    servo1.position(_angle);
    delay(1);
    led.low();                    // sets the LED off
}

function set_position2 (_angle) {
    servo2.position(_angle);
    delay(1);
    led.low();                    // sets the LED off
}


while (true) {
    led.high();                   // sets the LED on
    delay(500);
    led.low();                    // sets the LED off
    delay(500);
}

Webpage code uses this free javascript sliders. Download and place in your project folder... or you can roll your own.

<!DOCTYPE html>
<html>
<head>
	<title>Web Servo</title>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
	<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
	<link rel="stylesheet" type="text/css" href="./dhtmlxslider_v421/codebase/dhtmlxslider.css"/>
	<script src="./dhtmlxslider_v421/codebase/dhtmlxslider.js"></script>
    <script type="text/javascript" src="/js/erpc.js"></script>
	<style>
		#sliderObj {
			position: relative;
			margin-top: 10px;
			width: 300px;
			height: 30px;
		}
	</style>
	<script>
		var mySlider, mySlider2;
		
		function doOnLoad() {
			mySlider = new dhtmlXSlider({
				parent:	"sliderObj",
				step:	1.0,
                size:   300,
				min:	0.0,
				max:	180.0,
				value:	90.0,
				linkTo:	"inp1"
            });
                
 			mySlider2 = new dhtmlXSlider({
				parent:	"sliderObj2",
				step:	1.0,
                size:   300,
				min:	0.0,
				max:	180.0,
				value:	90.0,
				linkTo:	"inp2"
 			});

       
            mySlider.attachEvent("onSlideEnd", function(value) {
                erpc("set_position1",value);
            });

            mySlider2.attachEvent("onSlideEnd", function(value) {
                erpc("set_position2",value);
            });


		};
        
        function sweepme() {
            erpc("sweep");
        };
	</script>
</head>
<body onload="doOnLoad();">
	<table cellspacing="0" cellpadding="0" border="0">
		<tr>
			<td style="padding-right:10px;"><div id="sliderObj"></div></td>
            <td><input type="text" id="inp1"></td>
		</tr>
        <tr>
			<td style="padding-right:10px;"><div id="sliderObj2"></div></td>
            <td><input type="text" id="inp2"></td>           
        </tr>
        <tr>
			<td style="padding-right:10px;"><div id="sweep"></div><button onclick="sweepme()">Sweep Me</button></td>
        </tr>

	</table>
</body>
</html>
patrick

Sweet! Thanks a lot for posting that. Nice example.

mdoan7

If you don't watch them carefully... 2 servos quickly become 8!

patrick

Whoa. That looks like something you need to post a video for. Does it walk?

mdoan7

It does walk.... and Sit, Sleep, Run, and Creep super slowly... all via RPC from my phone. The frame is 3d printed. Once I get the board mount printed I'll see about posting an untethered video.

Plan is to add an IMU for balance control. A streaming camera feed would be sweet. We'll see.

patrick

Very cool. Please post details you don't mind sharing (Github repo, etc.).

This would be great for https://www.hackster.io/esquilo. If you haven't posted a project there before, they make it really easy.