Revision: 1779
Author: [email protected]
Date: Sat Mar 13 21:35:07 2010
Log: completed servo master/slave i2c
http://code.google.com/p/jallib/source/detail?r=1779
Added:
/trunk/include/external/motor/servo/servo_rc_master_dedicated.jal
/trunk/sample/18f452_servo_rc_master_dedicated_slave_i2c.jal
/trunk/sample/18f4620_servo_rc_master_dedicated_master_i2c.jal
Modified:
/trunk/include/external/motor/servo/servo_rc_master.jal
/trunk/sample/18f452_servo_rc_master.jal
=======================================
--- /dev/null
+++ /trunk/include/external/motor/servo/servo_rc_master_dedicated.jal Sat
Mar 13 21:35:07 2010
@@ -0,0 +1,85 @@
+-- Title: Library for use with a dedicated RC servo PIC
+-- 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 controls an external PIC that works as a
slave device
+-- and is a dedicated PIC to servo movements.
+--
+-- Notes:
+-- Also see servo_rc_master library, this is not the main servo library.
+--
+-- Master clock/oscillator should be equal or less then slave clock/osc
+-- otherwise you may need to run a slower I2C bus.
+--
+-- Sources:
+--
http://www.horrorseek.com/home/halloween/wolfstone/Motors/svoint_RCServos.html
+--
+
+-- servo commands that may be used in the procedure
+const byte SERVO_RC_COMMAND_MOVE = 0 -- move command
+const byte SERVO_RC_COMMAND_REVERSE = 32 -- reverse servo command
+const byte SERVO_RC_COMMAND_ON_OFF = 64 -- turn servo on/off
+const byte SERVO_RC_COMMAND_MIN = 96 -- set servo min movement
+const byte SERVO_RC_COMMAND_MAX = 128 -- set servo max movement
+
+procedure servo_rc_send_command_i2c(byte in servo_ic_address, byte in
command, byte in command_data, byte in servo_num) is
+ var byte ack_ok
+
+ var byte pic_i2c_servo_return_data
+ var bit error at pic_i2c_servo_return_data : 7
+ var bit ready at pic_i2c_servo_return_data : 6
+ var bit error2 at pic_i2c_servo_return_data : 5
+ var bit ready2 at pic_i2c_servo_return_data : 4
+ var bit error3 at pic_i2c_servo_return_data : 3
+ var bit ready3 at pic_i2c_servo_return_data : 2
+ var bit error4 at pic_i2c_servo_return_data : 1
+ var bit ready4 at pic_i2c_servo_return_data : 0
+
+ error = TRUE
+ ready = FALSE
+
+ while error == TRUE loop -- send data till dedicated servo pic recieved
data ok
+ -- send servo command_data and servo number to slave
+ i2c_start() -- send start bit
+ ack_ok = i2c_transmit_byte(servo_ic_address) -- transmit data &
wait for ack
+
+ -- send servo number and command
+ ack_ok = i2c_transmit_byte(command + servo_num) -- transmit command
+ servo number
+ ack_ok = i2c_transmit_byte(command_data) -- transmit transmit
servo command_data or command
+ ack_ok = i2c_transmit_byte( (command + servo_num) ^ command_data )
-- transmit error check data
+
+ i2c_stop() -- send stop bit
+
+ var bit data_recieved_ok = FALSE
+ while data_recieved_ok == FALSE loop -- wait till data has been
recieved and error checking is ok
+ -- get the data
+ i2c_start() -- send start bit
+ ack_ok = i2c_transmit_byte(servo_ic_address + 1) -- transmit data
& wait for ack
+ pic_i2c_servo_return_data = i2c_receive_byte(0) -- nack (no ack)
+ i2c_stop() -- send stop bit
+
+ -- do some error checking on recieved data
+ if (error == error) & (error2 == !error3) & (error3 == error4)
then
+ data_recieved_ok = TRUE
+ end if
+ end loop
+ end loop
+
+ -- wait till dedicated servo pic is ready for next command
+ while ready == FALSE loop
+ -- get the data
+ i2c_start() -- send start bit
+ ack_ok = i2c_transmit_byte(servo_ic_address + 1) -- transmit data &
wait for ack
+ pic_i2c_servo_return_data = i2c_receive_byte(0) -- nack (no ack)
+ i2c_stop() -- send stop bit
+
+ -- do some error checking on recieved data
+ if !((ready == ready2) & (ready2 == !ready3) & (ready3 == ready4))
then
+ ready = FALSE
+ end if
+ end loop
+end procedure
=======================================
--- /dev/null
+++ /trunk/sample/18f452_servo_rc_master_dedicated_slave_i2c.jal Sat Mar 13
21:35:07 2010
@@ -0,0 +1,406 @@
+-- Title: Sample of a dedicated RC servo controller PIC (slave PIC)
+-- 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 BSD license
(http://www.opensource.org/licenses/bsd-license.php)
+--
+-- Description: This sample creates a dedicated servo slave device that
+-- is a dedicated PIC for servo movements. Supports 24 servos
that
+-- are the type used in radio control (RC)
+--
+-- Notes:
+-- See other servo samples before using this one.
+--
+-- Master clock/oscillator should be equal or less then slave clock/osc
+-- otherwise you may need to run a slower I2C bus.
+--
+-- Sources:
+--
http://www.horrorseek.com/home/halloween/wolfstone/Motors/svoint_RCServos.html
+--
+
+-- include chip
+include 18f452 -- target picmicro
+
+-- This program assumes a 20 MHz resonator or crystal
+-- is connected to pins OSC1 and OSC2.
+pragma target clock 20_000_000 -- oscillator frequency
+-- configuration memory settings (fuses)
+pragma target OSC HS -- HS crystal or resonator
+pragma target WDT disabled -- no watchdog
+pragma target LVP disabled -- no Low Voltage Programming
+
+--------------------------------------------------------------------------------
+-- setup servos
+--------------------------------------------------------------------------------
+
+-- setup servo pins
+alias servo_1 is pin_b0
+alias servo_1_direction is pin_b0_direction
+servo_1_direction = output
+--
+alias servo_2 is pin_b1
+alias servo_2_direction is pin_b1_direction
+servo_2_direction = output
+--
+alias servo_3 is pin_b2
+alias servo_3_direction is pin_b2_direction
+servo_3_direction = output
+--
+alias servo_4 is pin_b3
+alias servo_4_direction is pin_b3_direction
+servo_4_direction = output
+--
+alias servo_5 is pin_b4
+alias servo_5_direction is pin_b4_direction
+servo_5_direction = output
+--
+alias servo_6 is pin_b5
+alias servo_6_direction is pin_b5_direction
+servo_6_direction = output
+--
+alias servo_7 is pin_b6
+alias servo_7_direction is pin_b6_direction
+servo_7_direction = output
+--
+alias servo_8 is pin_b7
+alias servo_8_direction is pin_b7_direction
+servo_8_direction = output
+--
+alias servo_9 is pin_d0
+alias servo_9_direction is pin_d0_direction
+servo_9_direction = output
+--
+alias servo_10 is pin_d1
+alias servo_10_direction is pin_d1_direction
+servo_10_direction = output
+--
+alias servo_11 is pin_d2
+alias servo_11_direction is pin_d2_direction
+servo_11_direction = output
+--
+alias servo_12 is pin_d3
+alias servo_12_direction is pin_d3_direction
+servo_12_direction = output
+--
+alias servo_13 is pin_d4
+alias servo_13_direction is pin_d4_direction
+servo_13_direction = output
+--
+alias servo_14 is pin_d5
+alias servo_14_direction is pin_d5_direction
+servo_14_direction = output
+--
+alias servo_15 is pin_d6
+alias servo_15_direction is pin_d6_direction
+servo_15_direction = output
+--
+alias servo_16 is pin_d7
+alias servo_16_direction is pin_d7_direction
+servo_16_direction = output
+--
+alias servo_17 is pin_a0
+alias servo_17_direction is pin_a0_direction
+servo_17_direction = output
+--
+alias servo_18 is pin_a1
+alias servo_18_direction is pin_a1_direction
+servo_18_direction = output
+--
+alias servo_19 is pin_a2
+alias servo_19_direction is pin_a2_direction
+servo_19_direction = output
+--
+alias servo_20 is pin_a3
+alias servo_20_direction is pin_a3_direction
+servo_20_direction = output
+--
+alias servo_21 is pin_a5
+alias servo_21_direction is pin_a5_direction
+servo_21_direction = output
+--
+alias servo_22 is pin_e0
+alias servo_22_direction is pin_e0_direction
+servo_22_direction = output
+--
+alias servo_23 is pin_e1
+alias servo_23_direction is pin_e1_direction
+servo_23_direction = output
+--
+alias servo_24 is pin_e2
+alias servo_24_direction is pin_e2_direction
+servo_24_direction = output
+
+-- choose min & max servo movment / pulse size
+var byte servo_min = 50 -- default is 50 (0.5ms min pulse)
+var byte servo_max = 250 -- default is 255 (2.5ms max pulse)
+
+-- choose pic internal timers
+const byte SERVO_USE_TIMER = 1 -- timer for servo's 1 to 8
+const byte SERVO_9_TO_16_USE_TIMER = 3 -- timer for servo's 9 to 16
+const byte SERVO_17_TO_24_USE_TIMER = 0 -- timer for servo's 17 to 24
+include servo_rc_master -- include the servo library
+servo_init()
+
+--------------------------------------------------------------------------------
+-- setup i2c slave
+--------------------------------------------------------------------------------
+
+include i2c_hw_slave
+
+const byte SLAVE_ADDRESS = 0x6E -- address of this pic will be 0x6C
+i2c_hw_slave_init(SLAVE_ADDRESS) -- init I2C hw slave
+
+-- declare variables
+
+var byte servo_number -- servo number (first byte to be recieved)
+-- servo number holds the commands in 3 most segnificant bits
+var bit command_bit_0 at servo_number : 5
+var bit command_bit_1 at servo_number : 6
+var bit command_bit_2 at servo_number : 7
+var byte command
+
+var byte data -- holds command data or servo location
+var bit reverse_state at data : 0 -- reverse command - reverse state bit
(TRUE/FALSE)
+var bit on_off_state at data : 1 -- on_off command - reverse state bit
(ON/OFF)
+ALIAS servo_location is data -- servo location uses all bytes
+
+var byte byte_select = 0 -- counts byte number recieved from
master
+
+-- Command names & values, can be compared to most segnificant bits of
servo_number
+const byte COMMAND_MOVE = 0 -- move command
+const byte COMMAND_REVERSE = 1 -- reverse servo command
+const byte COMMAND_ON_OFF = 2 -- turn servo on/off
+const byte COMMAND_MIN = 3 -- set servo min movement
+const byte COMMAND_MAX = 4 -- set servo max movement
+
+-- When slave reads via I2C, this is the status byte
+-- error = Slave did not get the data correctly, data should be sent again
by master
+-- ready = Slave is ready for another command
+-- Other bits allow master to error check recieved data.
+var byte i2c_return_data
+var bit error at i2c_return_data : 7 -- error bit
+var bit ready at i2c_return_data : 6 -- ready bit
+var bit error2 at i2c_return_data : 5 -- error2 = same as error
+var bit ready2 at i2c_return_data : 4 -- ready2 = same as ready
+var bit error3 at i2c_return_data : 3 -- error3 = inverse of error
+var bit ready3 at i2c_return_data : 2 -- ready3 = inverse of ready
+var bit error4 at i2c_return_data : 1 -- error4 = inverse of error
+var bit ready4 at i2c_return_data : 0 -- ready4 = inverse of ready
+
+-- Before including i2c_hw_slave_isr library, several callbacks
+-- must be defined (callbacks are procedure which supposed to be defined
+-- and be called on appriopriate time)
+
+-- this callback is used when something wrong happened
+-- during communication between master and us
+procedure i2c_hw_slave_on_error() is
+ pragma inline
+end procedure
+
+-- this callback is used when master wants to talk to us
+-- and our i2c address has been recognized
+procedure i2c_hw_slave_on_state_1(byte in _trash) is
+ pragma inline
+ -- _trash is read from master, but it's a dummy data
+ -- usually (always ?) ignored
+ byte_select = 0 -- mark as 1st byte comming in
+end procedure
+
+-- This callback is used when master sends a data byte
+procedure i2c_hw_slave_on_state_2(byte in rcv) is
+ pragma inline
+
+ if byte_select == 0 then
+ servo_number = rcv -- get the servo number/command
+ byte_select = byte_select + 1 -- 2nd byte is next
+ elsif byte_select == 1 then
+ data = rcv -- get the data for the command
+ byte_select = byte_select + 1 -- 3rd byte is next
+ elsif byte_select == 2 then
+ -- get error checking bit and do xor
+ if (servo_number ^ data) == rcv then
+ error = false -- data recieved is ok, no
error
+ else
+ error = true -- data recieved is not ok,
error
+ end if
+
+ ready = FALSE -- allow main loop to run
+ end if
+
+end procedure
+
+-- this callback is used when master wants to read something
+-- from us. It should use i2c_hw_slave_write() to send something
+procedure i2c_hw_slave_on_state_3() is
+ pragma inline
+ error2 = error -- set error bit
+ ready2 = ready -- set ready bit
+
+ error3 = !error -- set inverse error bit
+ ready3 = !ready -- set inverse ready bit
+ error4 = !error -- set inverse error bit
+ ready4 = !ready -- set inverse ready bit
+
+ i2c_hw_slave_write_i2c(i2c_return_data) -- send data to master
+end procedure
+
+-- this callback is used when master, after having read something,
+-- still wants to read and get data from us.
+procedure i2c_hw_slave_on_state_4() is
+ pragma inline
+end procedure
+
+-- this callback is used when master does not want to talk
+-- with us anymore... This is an appropriate place to reset
+-- data for instance
+procedure i2c_hw_slave_on_state_5() is
+ pragma inline
+end procedure
+
+-- callbacks are defined, now include ISR
+include i2c_hw_slave_isr
+
+--------------------------------------------------------------------------------
+-- main program
+--------------------------------------------------------------------------------
+
+ready = TRUE -- device is ready for a command
+forever loop
+
+ -- stay ready until next command
+ while ready == TRUE loop
+ end loop
+
+ -- get the command type out of 3 MSB of servo_number
+ command = command_bit_2
+ command = (command << 1) + command_bit_1
+ command = (command << 1) + command_bit_0
+
+
+ if command == COMMAND_MOVE then
+ -- move the servo
+ servo_move (servo_location, servo_number)
+
+ elsif command == COMMAND_ON_OFF then
+ -- servo command bits are part of servo number so remove the command
bits
+ command_bit_0 = 0
+ command_bit_1 = 0
+ command_bit_2 = 0
+
+ -- set servo on/off state and direction
+ if servo_number == 1 then
+ servo_1_on = on_off_state
+ elsif servo_number == 2 then
+ servo_2_on = on_off_state
+ elsif servo_number == 3 then
+ servo_3_on = on_off_state
+ elsif servo_number == 4 then
+ servo_4_on = on_off_state
+ elsif servo_number == 5 then
+ servo_5_on = on_off_state
+ elsif servo_number == 6 then
+ servo_6_on = on_off_state
+ elsif servo_number == 7 then
+ servo_7_on = on_off_state
+ elsif servo_number == 8 then
+ servo_8_on = on_off_state
+ elsif servo_number == 9 then
+ servo_9_on = on_off_state
+ elsif servo_number == 10 then
+ servo_10_on = on_off_state
+ elsif servo_number == 11 then
+ servo_11_on = on_off_state
+ elsif servo_number == 12 then
+ servo_12_on = on_off_state
+ elsif servo_number == 13 then
+ servo_13_on = on_off_state
+ elsif servo_number == 14 then
+ servo_14_on = on_off_state
+ elsif servo_number == 15 then
+ servo_15_on = on_off_state
+ elsif servo_number == 16 then
+ servo_16_on = on_off_state
+ elsif servo_number == 17 then
+ servo_17_on = on_off_state
+ elsif servo_number == 18 then
+ servo_18_on = on_off_state
+ elsif servo_number == 19 then
+ servo_19_on = on_off_state
+ elsif servo_number == 20 then
+ servo_20_on = on_off_state
+ elsif servo_number == 21 then
+ servo_21_on = on_off_state
+ elsif servo_number == 22 then
+ servo_22_on = on_off_state
+ elsif servo_number == 23 then
+ servo_23_on = on_off_state
+ elsif servo_number == 24 then
+ servo_24_on = on_off_state
+ end if
+ elsif command == COMMAND_REVERSE then
+ -- servo command bits are part of servo number so remove the command
bits
+ command_bit_0 = 0
+ command_bit_1 = 0
+ command_bit_2 = 0
+
+ -- set servo on/off state and direction
+ if servo_number == 1 then
+ servo_1_reverse = reverse_state
+ elsif servo_number == 2 then
+ servo_2_reverse = reverse_state
+ elsif servo_number == 3 then
+ servo_3_reverse = reverse_state
+ elsif servo_number == 4 then
+ servo_4_reverse = reverse_state
+ elsif servo_number == 5 then
+ servo_5_reverse = reverse_state
+ elsif servo_number == 6 then
+ servo_6_reverse = reverse_state
+ elsif servo_number == 7 then
+ servo_7_reverse = reverse_state
+ elsif servo_number == 8 then
+ servo_8_reverse = reverse_state
+ elsif servo_number == 9 then
+ servo_9_reverse = reverse_state
+ elsif servo_number == 10 then
+ servo_10_reverse = reverse_state
+ elsif servo_number == 11 then
+ servo_11_reverse = reverse_state
+ elsif servo_number == 12 then
+ servo_12_reverse = reverse_state
+ elsif servo_number == 13 then
+ servo_13_reverse = reverse_state
+ elsif servo_number == 14 then
+ servo_14_reverse = reverse_state
+ elsif servo_number == 15 then
+ servo_15_reverse = reverse_state
+ elsif servo_number == 16 then
+ servo_16_reverse = reverse_state
+ elsif servo_number == 17 then
+ servo_17_reverse = reverse_state
+ elsif servo_number == 18 then
+ servo_18_reverse = reverse_state
+ elsif servo_number == 19 then
+ servo_19_reverse = reverse_state
+ elsif servo_number == 20 then
+ servo_20_reverse = reverse_state
+ elsif servo_number == 21 then
+ servo_21_reverse = reverse_state
+ elsif servo_number == 22 then
+ servo_22_reverse = reverse_state
+ elsif servo_number == 23 then
+ servo_23_reverse = reverse_state
+ elsif servo_number == 24 then
+ servo_24_reverse = reverse_state
+ end if
+
+ elsif command == COMMAND_MIN then
+ servo_min = data -- set servo min movement position
+ elsif command == COMMAND_MAX then
+ servo_max = data -- set servo mas movement position
+ end if
+
+ ready = TRUE -- set ready for next command
+end loop
=======================================
--- /dev/null
+++ /trunk/sample/18f4620_servo_rc_master_dedicated_master_i2c.jal Sat Mar
13 21:35:07 2010
@@ -0,0 +1,92 @@
+-- Title: Sample master to control a dedicated RC servo controller PIC
+-- 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 BSD license
(http://www.opensource.org/licenses/bsd-license.php)
+--
+-- Description: This sample controls an external PIC that acts as a slave
device
+-- and is a dedicated PIC for servo movements. Slave PIC can
support
+-- 24 servos that are the type used in radio control (RC)
+--
+-- Notes:
+-- See other servo samples before using this one
+--
+-- See usage notes in library file servo_rc_master.jal, very important!
+--
+-- Master clock/oscillator should be equal or less then slave clock/osc
+-- otherwise you may need to run a slower I2C bus.
+--
+-- Sources:
+--
http://www.horrorseek.com/home/halloween/wolfstone/Motors/svoint_RCServos.html
+--
+
+include 18f4620 -- target PICmicro
+--
+-- This program assumes a 20 MHz resonator or crystal
+-- is connected to pins OSC1 and OSC2.
+-- Unspecified configuration bits may cause a different frequency!
+pragma target clock 20_000_000 -- oscillator frequency
+-- configuration memory settings (fuses)
+pragma target OSC HS -- HS crystal or resonator
+pragma target WDT disabled -- no watchdog
+pragma target LVP disabled -- no Low Voltage Programming
+pragma target MCLR external -- reset externally
+
+-- Setup I2C
+--
+-- I2C pin definition
+alias i2c_scl is pin_c3
+alias i2c_scl_direction is pin_c3_direction
+alias i2c_sda is pin_c4
+alias i2c_sda_direction is pin_c4_direction
+--
+const word _i2c_bus_speed = 4 -- 400kHz
+const bit _i2c_level = true -- i2c levels (not SMB)
+include i2c_software -- include i2c software lib
+i2c_initialize() -- initialize i2c lib
+
+-- Setup the dedicated pic servo library, to control an external servo pic.
+-- This external pic should be written with a sample such as
+-- 18fxxx_servo_rc_master_dedicated_slave_i2c.jal
+-- It will be a dedicated device using the servo_rc_master library
+include servo_rc_master_dedicated
+
+-- main program here
+var byte servo_ic_address = 0x6E -- the dedicate/external servo pic i2c
address
+
+-- set min/max movement. servo number cannot be selected, changing min/max
+-- movement affects all servo's. if you do not send these 2 commands,
+-- the default is min 50, max 250 (500us to 2.50ms pulses)
+servo_rc_send_command_i2c(servo_ic_address, SERVO_RC_COMMAND_MIN, 50, 0)
+servo_rc_send_command_i2c(servo_ic_address, SERVO_RC_COMMAND_MAX, 250, 0)
+
+forever loop
+ -- move pic 1 & 17 to center position
+ servo_rc_send_command_i2c(servo_ic_address, SERVO_RC_COMMAND_MOVE, 127,
1)
+ servo_rc_send_command_i2c(servo_ic_address, SERVO_RC_COMMAND_MOVE, 127,
17)
+ _usec_delay(1_000_000)
+
+ -- move pic 1 & 17 to one side
+ servo_rc_send_command_i2c(servo_ic_address, SERVO_RC_COMMAND_MOVE, 255,
1)
+ servo_rc_send_command_i2c(servo_ic_address, SERVO_RC_COMMAND_MOVE, 255,
17)
+ _usec_delay(1_000_000)
+
+ -- move pic 1 & 17 to center position
+ servo_rc_send_command_i2c(servo_ic_address, SERVO_RC_COMMAND_MOVE, 127,
1)
+ servo_rc_send_command_i2c(servo_ic_address, SERVO_RC_COMMAND_MOVE, 127,
17)
+ _usec_delay(1_000_000)
+
+ -- move pic 1 & 17 to other side
+ servo_rc_send_command_i2c(servo_ic_address, SERVO_RC_COMMAND_MOVE, 0, 1)
+ servo_rc_send_command_i2c(servo_ic_address, SERVO_RC_COMMAND_MOVE, 0,
17)
+ _usec_delay(1_000_000)
+
+ -- reverse direction of servo 1
+ servo_rc_send_command_i2c(servo_ic_address, SERVO_RC_COMMAND_REVERSE,
TRUE, 1)
+
+ -- turn off servo 17
+ ;servo_rc_send_command_i2c(servo_ic_address, SERVO_RC_COMMAND_ON_OFF,
OFF, 17)
+end loop
+
=======================================
--- /trunk/include/external/motor/servo/servo_rc_master.jal Wed Mar 10
18:41:42 2010
+++ /trunk/include/external/motor/servo/servo_rc_master.jal Sat Mar 13
21:35:07 2010
@@ -7,33 +7,33 @@
-- Released under the ZLIB license
(http://www.opensource.org/licenses/zlib-license.html)
--
-- Description: This library controls up to 24 servos that are the type
used in
--- radio control (RC)
+-- radio control (RC). This is the main servo library.
--
-- Sources:
--
http://www.horrorseek.com/home/halloween/wolfstone/Motors/svoint_RCServos.html
--
-- usage notes:
--- 1. Set your SERVO_MIN values to restrict movement.
--- This step must be completed correctly. First use code as follows
--- directly after you call servo_1_to_8_init()
+-- 1. Set your servo_min values to restrict movement.
+-- This step should be completed. First use code as follows
+-- directly after you call servo_init()
--
--- servo_1_to_8_move(0,1)
+-- servo_move(0,1)
-- forever loop
-- end loop
--
--- Now run your circuit and gradually change SERVO_MIN value so the
servo
+-- Now run your circuit and gradually increase servo_min value so the
servo
-- is at the correct location on one side. Try to get the servo to be
-- 1mm from it's min location.
--- 2. Repeat step 1 with SERVO_MAX but use this:
+-- 2. Repeat step 1 by decreasing servo_max but use this:
--
--- servo_1_to_8_move(255,1)
+-- servo_move(255,1)
-- forever loop
-- end loop
--
-- other notes:
-- This lib uses timer0, timer1 or timer3
-- Up to 24 servos are supported
--- you may modify servo_1_to_8_steps_per_2_5_msec directly for more exact
movements
+--
-- check if current pic has selected timers
if SERVO_USE_TIMER == 0 then
@@ -144,8 +144,6 @@
alias servo_1_to_8_ps is T3CON_T3CKPS -- timer prescaler
select bits
end if
--- calculate difference between min & max
-const byte _SERVO_1_TO_8_CALC_DIFF = SERVO_MAX - SERVO_MIN
var bit _servo_1_to_8_isr_count = 0 -- ISR step count (0 - servo pulse
/ 1 - idle state)
var byte _servo_1_to_8_number_count = 0 -- ISR servo control count 0 to 7
@@ -214,9 +212,6 @@
alias servo_9_to_16_ps is T3CON_T3CKPS -- timer prescaler
select bits
end if
- -- calculate difference between min & max
- const byte _SERVO_9_TO_16_CALC_DIFF = SERVO_MAX - SERVO_MIN
-
var bit _servo_9_to_16_isr_count = 0 -- ISR step count (0 - servo
pulse / 1 - idle state)
var byte _servo_9_to_16_number_count = 8 -- ISR servo control count 0
to 7
end if
@@ -286,9 +281,6 @@
alias servo_17_to_24_ps is T3CON_T3CKPS -- timer prescaler
select bits
end if
- -- calculate difference between min & max
- const byte _SERVO_17_TO_24_CALC_DIFF = SERVO_MAX - SERVO_MIN
-
var bit _servo_17_to_24_isr_count = 0 -- ISR step count (0 - servo
pulse / 1 - idle state)
var byte _servo_17_to_24_number_count = 16 -- ISR servo control count 0
to 7
end if
@@ -492,41 +484,41 @@
if defined(servo_9) == TRUE then
if servo_9_to_16_if then -- check if it is a timer interrupt
- servo_9_to_16_if =
off -- reset counter
- if _servo_9_to_16_isr_count == 0
then -- do the servo pulse (high)
+ servo_9_to_16_if =
off -- reset counter
+ if _servo_9_to_16_isr_count == 0
then -- do the servo pulse (high)
servo_9_to_16_tmr =
_servo_9_to_16_pulse[_servo_9_to_16_number_count - 8] -- set timer to
pulse high width
- _servo_9_to_16_set_level
(_servo_9_to_16_number_count,HIGH) -- set level to high
- _servo_9_to_16_isr_count = _servo_9_to_16_isr_count +
1 -- go to next step
- elsif _servo_9_to_16_isr_count == 1
then -- do the servo idle state (low)
+ _servo_9_to_16_set_level
(_servo_9_to_16_number_count,HIGH) -- set level to high
+ _servo_9_to_16_isr_count = _servo_9_to_16_isr_count +
1 -- go to next step
+ elsif _servo_9_to_16_isr_count == 1
then -- do the servo idle state (low)
servo_9_to_16_tmr =
_servo_9_to_16_low[_servo_9_to_16_number_count - 8] -- set timer to
pulse low width
- _servo_9_to_16_set_level
(_servo_9_to_16_number_count,LOW) -- set level to low
- _servo_9_to_16_isr_count = _servo_9_to_16_isr_count +
1 -- go to next step
- _servo_9_to_16_number_count = _servo_9_to_16_number_count +
1 -- change to next servo
- if _servo_9_to_16_number_count == 16
then -- change to first servo if needed
- _servo_9_to_16_number_count =
8 -- go back to first servo
+ _servo_9_to_16_set_level
(_servo_9_to_16_number_count,LOW) -- set level to low
+ _servo_9_to_16_isr_count = _servo_9_to_16_isr_count +
1 -- go to next step
+ _servo_9_to_16_number_count = _servo_9_to_16_number_count +
1 -- change to next servo
+ if _servo_9_to_16_number_count == 16
then -- change to first servo if needed
+ _servo_9_to_16_number_count =
8 -- go back to first servo
end if
- _servo_9_to_16_isr_count =
0 -- go to first step
+ _servo_9_to_16_isr_count =
0 -- go to first step
end if
end if
end if
if defined(servo_17) == TRUE then
- if servo_17_to_24_if then -- check if it is a timer interrupt
- servo_17_to_24_if = off -- reset
counter
- if _servo_17_to_24_isr_count == 0 then -- do
the servo pulse (high)
- servo_17_to_24_tmr =
_servo_17_to_24_pulse[_servo_17_to_24_number_count - 16] -- set timer to
pulse high width
- _servo_17_to_24_set_level
(_servo_17_to_24_number_count,HIGH) -- set level to high
- _servo_17_to_24_isr_count = _servo_17_to_24_isr_count +
1 -- go to next step
- elsif _servo_17_to_24_isr_count == 1 then -- do
the servo idle state (low)
- servo_17_to_24_tmr =
_servo_17_to_24_low[_servo_17_to_24_number_count - 16] -- set timer to
pulse low width
- _servo_17_to_24_set_level
(_servo_17_to_24_number_count,LOW) -- set level to low
- _servo_17_to_24_isr_count = _servo_17_to_24_isr_count +
1 -- go to next step
- _servo_17_to_24_number_count = _servo_17_to_24_number_count +
1 -- change to next servo
- if _servo_17_to_24_number_count == 24 then --
change to first servo if needed
- _servo_17_to_24_number_count = 16 -- go
back to first servo
+ if servo_17_to_24_if
then -- check if
it is a timer interrupt
+ servo_17_to_24_if =
off -- reset
counter
+ if _servo_17_to_24_isr_count == 0
then -- do the servo pulse (high)
+ servo_17_to_24_tmr =
_servo_17_to_24_pulse[_servo_17_to_24_number_count - 16] -- set timer to
pulse high width
+ _servo_17_to_24_set_level
(_servo_17_to_24_number_count,HIGH) -- set level to high
+ _servo_17_to_24_isr_count = _servo_17_to_24_isr_count +
1 -- go to next step
+ elsif _servo_17_to_24_isr_count == 1
then -- do the servo idle state (low)
+ servo_17_to_24_tmr =
_servo_17_to_24_low[_servo_17_to_24_number_count - 16] -- set timer to
pulse low width
+ _servo_17_to_24_set_level
(_servo_17_to_24_number_count,LOW) -- set level to low
+ _servo_17_to_24_isr_count = _servo_17_to_24_isr_count +
1 -- go to next step
+ _servo_17_to_24_number_count = _servo_17_to_24_number_count +
1 -- change to next servo
+ if _servo_17_to_24_number_count == 24
then -- change to first servo if needed
+ _servo_17_to_24_number_count =
16 -- go back to first servo
end if
- _servo_17_to_24_isr_count = 0 -- go
to first step
+ _servo_17_to_24_isr_count =
0 -- go to first step
end if
end if
end if
@@ -650,10 +642,12 @@
var dword final
var word location2 = location -- change location to word for overflow
- -- map input location to 0-255 according to SERVO_MIN/SERVO_MAX
- calc1 = (location2 * _SERVO_1_TO_8_CALC_DIFF)
+ -- map input location to 0-255 according to servo_min/servo_max
+ -- calculate difference between min & max
+ var byte servo_calc_diff = servo_max - servo_min
+ calc1 = (location2 * servo_calc_diff)
calc2 = calc1 / 255
- final = calc2 + SERVO_MIN
+ final = calc2 + servo_min
if servo_number < 9 then
if SERVO_USE_TIMER == 0 then -- if 8 bit timer 0
@@ -807,8 +801,8 @@
--------------------------------------------------------------------------------
procedure servo_init() is
-- enable global interupts
- INTCON_GIE = on -- Enables all unmasked interrupts
- INTCON_PEIE = on -- Enables all unmasked peripheral interrupts
+ INTCON_GIE = on -- Enables all unmasked interrupts
+ INTCON_PEIE = on -- Enables all unmasked peripheral interrupts
servo_1_to_8_if = off -- clear interrupt overflow bit
servo_1_to_8_ie = on -- timer overflow interrupt enable
=======================================
--- /trunk/sample/18f452_servo_rc_master.jal Wed Mar 10 18:41:42 2010
+++ /trunk/sample/18f452_servo_rc_master.jal Sat Mar 13 21:35:07 2010
@@ -13,7 +13,7 @@
--
http://www.horrorseek.com/home/halloween/wolfstone/Motors/svoint_RCServos.html
--
-- notes:
--- See usage notes in library file servo.jal, very important!
+-- See usage notes in library file servo_rc_master.jal, very important!
--
-- include chip
@@ -74,18 +74,18 @@
alias servo_8_direction is pin_b7_direction
servo_8_direction = output
--
-alias servo_9 is pin_a0
-alias servo_9_direction is pin_a0_direction
-servo_9_direction = output
+;alias servo_9 is pin_a0
+;alias servo_9_direction is pin_a0_direction
+;servo_9_direction = output
-- choose min & max servo movment / pulse size
const byte SERVO_MIN = 50 -- default is 50 (0.5ms min pulse)
const byte SERVO_MAX = 255 -- default is 255 (2.5ms max pulse)
--- servo lib currently only supports timer 0, an 8 bit clock
+-- choose pic internal timers
const byte SERVO_USE_TIMER = 0 -- timer for servo's 1 to 8
-const byte SERVO_9_TO_16_USE_TIMER = 1 -- timer for servo's 9 to 16
-const byte SERVO_17_TO_24_USE_TIMER = 1 -- timer for servo's 17 to 24
+;const byte SERVO_9_TO_16_USE_TIMER = 1 -- timer for servo's 9 to 16
+;const byte SERVO_17_TO_24_USE_TIMER = 3 -- timer for servo's 17 to 24
include servo_rc_master -- include the servo library
servo_init()
@@ -102,10 +102,6 @@
servo_move(127,7)
servo_move(127,8)
--- stop here (good for measurements)
-;forever loop
-;end loop
-
-- example moving servos one and two and blink led
forever 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.