Literate CoffeeScript version of servo example

This commit is contained in:
Andrew Stewart 2013-11-27 16:50:34 -08:00
parent 9733a35f9a
commit 61efedb251
1 changed files with 46 additions and 0 deletions

View File

@ -0,0 +1,46 @@
# Servo
For this example, similar in structure to the led_brightness example, we're
going to take an Arduino, and modify the angle of an attached servo such that
it's continually turning back and forth.
Before we get started, make sure to have the `cylon-arduino` module installed.
First, let's require Cylon:
Cylon = require '../..'
Now we can start defining our robot:
Cylon.robot
We'll be connecting to an Ardunio, using the Firmata protocol, and a servo
attached to the Arduino on pin 3.
connection: { name: 'arduino', adaptor: 'firmata', port: '/dev/ttyACM0' }
device: { name: 'servo', driver: 'servo', pin: 3 }
We'll start defining the work for our robot next:
work: (my) ->
We'll define variables to hold our servo's angle, and the rate at which that
angle will change:
angle = 0
increment = 5
Every second, we'll increment the `angle`, set the servo to run at that angle,
and log the angle we're running at to the console. We'll also make sure to
change the increment if the angle is at the upper/lower bounds of the values
supported:
every 0.05.seconds(), ->
angle += increment
my.servo.angle(angle)
Logger.info "Current angle: #{my.servo.currentAngle() }"
increment = -increment if (angle is 0) or (angle is 180)
And with all that done, we can now start our robot:
.start()