Revision: 1613
Author: [email protected]
Date: Thu Jan 21 12:21:44 2010
Log: servo controller lib
http://code.google.com/p/jallib/source/detail?r=1613
Added:
/trunk/include/external/motor/servo/servo_control.jal
/trunk/sample/18F4620_servo_control.jal
=======================================
--- /dev/null
+++ /trunk/include/external/motor/servo/servo_control.jal Thu Jan 21
12:21:44 2010
@@ -0,0 +1,70 @@
+-- Title: servo control library
+-- Author: Matthew Schinkel - borntechi.com, copyright (c) 2009, all
rights reserved.
+-- Adapted-by:
+-- Compiler: >=2.4m
+--
+-- This file is part of jallib (http://jallib.googlecode.com)
+-- Released under the ZLIB license
(http://www.opensource.org/licenses/zlib-license.html)
+--
+-- Description: This library moves a servo such as a servo used in radio
control (RC)
+--
+-- Sources:
+--
http://www.horrorseek.com/home/halloween/wolfstone/Motors/svoint_RCServos.html
+--
+-- notes:
+-- procedure run_servo must be continuously called within your main
program loop
+--
+-- choose speed to clock the servo:
+-- SERVO_USE_TIMER = TRUE - reduces delay in main program (ie. fastest
program)
+-- SERVO_USE_TIMER = FALSE - high value = slow servo clock, (slowest
program) / low value = fast servo, (fast program)
+-- SERVO_USE_TIMER = TRUE - low value = fast servo clock, (slow program)
/ high value = slow servo clock (fastest program)
+-- if SERVO_DELAY is too low with SERVO_USE_TIMER = TRUE you may get weird
movments
+--
+
+
+const word servo_values = servo_max - servo_min
+
+if SERVO_USE_TIMER == TRUE then
+ procedure run_servo(word in value) is
+ if (check_delay(0)) then
+ var word calc1, calc2
+ var word final
+ var byte final2
+
+ ;calculate the pulse length
+ calc1 = (value * 180)
+ calc2 = calc1 / 255
+ final = calc2 + servo_min
+
+ -- change final into a byte
+ var byte val[2] at final
+ final2 = val[0]
+
+ set_delay(0, servo_delay) -- 4 ticks on delay-slot 0
+ servo = on -- start the pulse
+ delay_10us(final2) -- move the servo by pulse length
+ servo = off -- end the pulse
+ end if
+ end procedure
+else
+ procedure run_servo(word in value) is
+ var word calc1, calc2
+ var word final
+ var byte final2
+
+ ;calculate the pulse length
+ calc1 = (value * 180)
+ calc2 = calc1 / 255
+ final = calc2 + servo_min
+
+ -- change final into a byte
+ var byte val[2] at final
+ final2 = val[0]
+
+ servo = on -- start the pulse
+ delay_10us(final2) -- move the servo by pulse length
+ servo = off -- end the pulse
+ delay_1ms(SERVO_DELAY) -- the rest of the servo pulse
+ end procedure
+end if
+
=======================================
--- /dev/null
+++ /trunk/sample/18F4620_servo_control.jal Thu Jan 21 12:21:44 2010
@@ -0,0 +1,94 @@
+-- Title: Test program for servo control
+-- Author: Matthew Schinkel - borntechi.com, Copyright (c) 2008-2009, all
rights reserved.
+-- Adapted-by:
+-- Compiler: >=2.4m
+--
+-- This file is part of jallib (http://jallib.googlecode.com)
+-- Released under the BSD license
(http://www.opensource.org/licenses/bsd-license.php)
+--
+-- Description: this sample moves a servo such as a servo used in radio
control (RC)
+--
+-- Sources:
+--
http://www.horrorseek.com/home/halloween/wolfstone/Motors/svoint_RCServos.html
+--
+-- notes:
+-- procedure run_servo must be continuously called within your main
program loop
+--
+-- choose speed to clock the servo:
+-- SERVO_USE_TIMER = TRUE - reduces delay in main program (ie. fastest
program)
+-- SERVO_USE_TIMER = FALSE - high value = slow servo clock, (slowest
program) / low value = fast servo, (fast program)
+-- SERVO_USE_TIMER = TRUE - low value = fast servo clock, (slow program)
/ high value = slow servo clock (fastest program)
+-- if SERVO_DELAY is too low with SERVO_USE_TIMER = TRUE you may get weird
movments
+--
+
+
+-- include chip
+include 18F4620 -- target picmicro
+-- this program assumes a 20 mhz resonator or crystal
+-- is connected to pins osc1 and osc2.
+;pragma target osc INTOSC_NOCLKOUT -- hs crystal or resonator
+pragma target osc hs -- hs crystal or resonator
+pragma target clock 20_000_000 -- oscillator frequency
+--
+pragma target wdt disabled
+pragma target lvp disabled
+pragma target MCLR external -- reset externally
+--
+;OSCCON_IRCF = 0b110 -- set int osc to 4mhz
+;OSCCON_IRCF = 0b111 -- set internal osc to 8mhz
+;OSCTUNE_PLLEN = true -- multiply internal osc by 4
+--
+
+
+-- servo control pin
+alias servo is pin_c2
+alias servo_direction is pin_c2_direction
+servo_direction = output
+
+enable_digital_io()
+
+-- aliases for timer0 on 18f chips
+alias option_reg_t0cs is t0con_t0cs
+alias option_reg_psa is t0con_psa
+alias option_reg_ps IS t0con_t0ps
+
+include delay -- include the delay library required by servo.jal
+
+-- setup the timer0_isr_interval library, optional for servo.jal
+const timer0_isr_rate = 500 -- 1 kHz isr rate
+const DELAY_SLOTS = 1 -- support 1 delays at the same time
+include timer0_isr_interval
+timer0_isr_init() -- init timer0 isr
+
+const byte SERVO_MIN = 55 -- max left - default 55
+const byte SERVO_MAX = 235 -- max right - default 235
+const byte SERVO_USE_PERCENT = FALSE -- use 0-100 or 0-255
+const bit SERVO_USE_TIMER = TRUE -- use the timer library to speed up
processes
+const byte SERVO_DELAY = 10 -- speed to clock servo, see notes
+--
+include servo -- include the servo library
+
+-- example move servo to center
+;Forever loop
+; run_servo(127)
+;end loop
+
+-- example move servo left to right and back
+var byte speed = 3 -- the speed of the movement
+
+var byte servo_value = 0
+forever loop
+ for 255 loop
+ servo_value = servo_value + 1 -- move right
+ run_servo(servo_value) -- clock the servo
+ delay_1ms(speed) -- delay before next movement
+ end loop
+
+ for 255 loop
+ servo_value = servo_value - 1 -- move left
+ run_servo(servo_value) -- clock the servo
+ delay_1ms(speed) -- delay before next movement
+ end loop
+end loop
+
+
--
You received this message because you are subscribed to the Google Groups
"jallib" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/jallib?hl=en.