Revision: 1815
Author: [email protected]
Date: Wed Mar 17 19:19:03 2010
Log: added constant to servo lib, allows user to choose to wait for slave before continue. also modified some comments.
http://code.google.com/p/jallib/source/detail?r=1815

Modified:
 /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

=======================================
--- /trunk/include/external/motor/servo/servo_rc_master_dedicated.jal Sat Mar 13 21:35:07 2010 +++ /trunk/include/external/motor/servo/servo_rc_master_dedicated.jal Wed Mar 17 19:19:03 2010
@@ -15,71 +15,112 @@
 -- Master clock/oscillator should be equal or less then slave clock/osc
 -- otherwise you may need to run a slower I2C bus.
 --
+-- if you set servo_rc_master_dedicated_wait to false, you will need to check +-- if the slave is ready by using the servo_rc_is_ready_i2c() function before +-- sending another command. device must be setting this as false will allow your +-- main program to continue while slave is still busy. Slave will stay busy for
+-- about 1ms
+--
 -- Sources:
-- http://www.horrorseek.com/home/halloween/wolfstone/Motors/svoint_RCServos.html
 --

--- servo commands that may be used in the procedure
+-- servo commands that you may pass to the send_command 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
+-- holds error & ready bit recieved from slave, internal use only
+var byte _servo_rc_master_dedicated_data
+var bit _servo_rc_master_dedicated_error at _servo_rc_master_dedicated_data : 7 +var bit _servo_rc_master_dedicated_ready at _servo_rc_master_dedicated_data : 6 +var bit _servo_rc_master_dedicated_error2 at _servo_rc_master_dedicated_data : 5 +var bit _servo_rc_master_dedicated_ready2 at _servo_rc_master_dedicated_data : 4 +var bit _servo_rc_master_dedicated_error3 at _servo_rc_master_dedicated_data : 3 +var bit _servo_rc_master_dedicated_ready3 at _servo_rc_master_dedicated_data : 2 +var bit _servo_rc_master_dedicated_error4 at _servo_rc_master_dedicated_data : 1 +var bit _servo_rc_master_dedicated_ready4 at _servo_rc_master_dedicated_data : 0
+
+-- setup uart for communication
+const serial_hw_baudrate  = 115200   -- set the baudrate
+include serial_hardware
+serial_hw_init()
+
+--------------------------------------------------------------------------------
+-- Check if dedicated servo pic is ready via I2C. returns true/false
+--------------------------------------------------------------------------------
+function servo_rc_is_ready_i2c(byte in servo_ic_address) return bit 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
+   -- get the data
+   i2c_start()                                            -- send start bit
+ ack_ok = i2c_transmit_byte(servo_ic_address + 1) -- transmit data & wait for ack
+   _servo_rc_master_dedicated_data = i2c_receive_byte(0)  -- nack (no ack)
+   i2c_stop()                                             -- send stop bit
+
+   -- do some _servo_rc_master_dedicated_error checking on recieved data
+ if (_servo_rc_master_dedicated_ready == _servo_rc_master_dedicated_ready2) & (_servo_rc_master_dedicated_ready2 == !_servo_rc_master_dedicated_ready3) & (_servo_rc_master_dedicated_ready3 == _servo_rc_master_dedicated_ready4) then
+      return _servo_rc_master_dedicated_ready    -- return the ready bit
+   else
+ return false -- error check failed, return false
+   end if
+end function
+
+--------------------------------------------------------------------------------
+-- Check if dedicated servo pic has an error via I2C. returns true/false. For
+-- internal use only.
+--------------------------------------------------------------------------------
+function servo_rc_error_i2c(byte in servo_ic_address) return bit is
+   var byte ack_ok
+
+   -- get the data
+   i2c_start()                                            -- send start bit
+ ack_ok = i2c_transmit_byte(servo_ic_address + 1) -- transmit data & wait for ack
+   _servo_rc_master_dedicated_data = i2c_receive_byte(0)  -- nack (no ack)
+   i2c_stop()                                             -- send stop bit
+
+   -- do some _servo_rc_master_dedicated_error checking on recieved data
+ if (_servo_rc_master_dedicated_error == _servo_rc_master_dedicated_error) & (_servo_rc_master_dedicated_error2 == !_servo_rc_master_dedicated_error3) & (_servo_rc_master_dedicated_error3 == _servo_rc_master_dedicated_error4) then
+      return _servo_rc_master_dedicated_error     -- return the error bit
+   else
+ return true -- error check failed, return true
+   end if
+end function
+
+--------------------------------------------------------------------------------
+-- send a command to the dedicated servo pic such as move, reverse, on/off
+-- via I2C
+--------------------------------------------------------------------------------
+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
+
+   _servo_rc_master_dedicated_error = TRUE
+   _servo_rc_master_dedicated_ready = FALSE
+
+   forever 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 + 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 _servo_rc_master_dedicated_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
+      -- check if dedicated servo pic recieved data ok, if so, exit loop.
+      if !servo_rc_error_i2c(servo_ic_address) then
+         exit loop
       end if
    end loop
+
+   -- check if dedicated servo pic is ready for the next command
+   if SERVO_RC_MASTER_DEDICATED_WAIT == TRUE then
+      while !servo_rc_is_ready_i2c(servo_ic_address) loop
+      end loop
+   end if
+
 end procedure
=======================================
--- /trunk/sample/18f452_servo_rc_master_dedicated_slave_i2c.jal Sat Mar 13 21:35:07 2010 +++ /trunk/sample/18f452_servo_rc_master_dedicated_slave_i2c.jal Wed Mar 17 19:19:03 2010
@@ -14,7 +14,7 @@
 -- 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.
+-- otherwise you may need to limit I2C bus speed.
 --
 -- Sources:
-- http://www.horrorseek.com/home/halloween/wolfstone/Motors/svoint_RCServos.html
@@ -22,7 +22,7 @@

 -- 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
=======================================
--- /trunk/sample/18f4620_servo_rc_master_dedicated_master_i2c.jal Sat Mar 13 21:35:07 2010 +++ /trunk/sample/18f4620_servo_rc_master_dedicated_master_i2c.jal Wed Mar 17 19:19:03 2010
@@ -13,10 +13,11 @@
 -- 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.
+-- otherwise you may need to limit I2C bus speed.
+--
+-- The external pic should be written with a sample such as
+-- 18fxxx_servo_rc_master_dedicated_slave_i2c.jal
 --
 -- Sources:
-- http://www.horrorseek.com/home/halloween/wolfstone/Motors/svoint_RCServos.html
@@ -48,16 +49,21 @@
 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
+--
+-- specify weather to wait for slave device to be ready before continuing
+-- main program. If set to false you must check the ready bit via ready function,
+-- or delay 1ms between commands.
+const byte SERVO_RC_MASTER_DEDICATED_WAIT = TRUE
+--
+-- include the dedicated servo library.
+-- slave/dedicated pic runs 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,
+-- movement affects all servo's. if you do not sendA 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)

--
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.

Reply via email to