Home
Articles
Futaba servo signal interface
C Code
Previous
1
2
3
4
A
B
C
D
Next
Previous
1
2
3
4
A
B
C
D
Next
Articles
Futaba servo signal interface
C CodeFutaba servo signal interface
Appendix
C Code
| '****************************************************************
'* Name : maindrive.BAS * '* Author : Barrett R. de Visser * '* Notice : Copyright (c) 2006 Barrett R. de Visser * '* : All Rights Reserved * '* Date : 3/16/06 * '* Version : 1.2 * '* Notes : Code for Futaba servo interface used for main * '* : drive and claw operation. used for presentation * '* : at the Miles Mac Science Fair. PIC16F877/A * '**************************************************************** trisb = %00000000 'set Portb to outputs for relay driving trisd = %11111111 'set Portd to inputs for servo signal trisa = %11111111 'set porta to inputs for ICD connector horz var byte 'variable for horizontal axis of control joystick vert var byte 'variable for vertical axis of control joystick claw var byte 'variable for claw state sevhigh con 165 'sets servo maximum trigger threshold to 165us pulse width sevlow con 135 'sets servo minimum trigger threshold to 135us pulse width 'These can be adjusted as desired to achive best sensitivity to 'suite your needs. portb = 0 'sets portb low to prevent complications durring program start start: 'Start of program pulsin portd.0, 1, horz 'Measures width of logic high pulse on portd.0 'connected to horizontal axis servo channel pulsin portd.1, 1, vert 'Measures width of logic high pulse on portd.1 'connected to vertical axis servo channel pulsin portd.2, 1, claw 'Measures width of logic high pulse on portd.2 'connected to servo channel of a toggle switch if horz + vert + claw < 300 then portb = 0 VI goto start endif 'Checks to see if all inputs have at least a '100us pulse (3inputs * 100 = 300). 'If they dont, jump to start to prevent the 'robot from running away if radio transmitter 'is inactive, out of range or if premature battery 'failure occurs durring operation. If claw > servhigh then portb.6 = 1 portb.7 = 0 endif 'Checks claw state. If it exceeds max threshold 'triggers relays on portb pin 7 active and 6 inactive 'to open claw if claw < servlow then portb.6 = 0 portb.7 = 1 endif 'Checks claw state. If it decends below minimum threshold 'trigger relays on portb pin 7 inactive and 6 active 'to close claw. if horz < servlow or horz > servhigh then goto turn endif 'Checks to see if horizontal component 'is outside of neutral threshold and 'jumps to the turn sub if true. This 'creates higher sensitivity in turning 'the robot if the control joystick is 'put into a diagonal position. It also 'creates ease in menuverability as you 'wont have tomove your finger as far in 'order to turn. If vert > servhigh then portb.0 = 1 portb.2 = 1 portb.1 = 0 portb.3 = 0 endif 'Checks if vertical component is beyond maximum threshold and sets 'ports controlling the forward drive relays to high and the reverse 'drive relays to low. This activates both motors in forward motion 'simotaniously resulting in forward motion. if vert < servlow then portb.1 = 1 VII portb.3 = 1 portb.0 = 0 portb.2 = 0 endif 'Checks if vertical component decends below minimum threshold and sets 'ports controlling the reverse drive relays high and the forward drive 'relays low. This activates both motors in reverse motion simotaniously 'resulting in backward motion. If vert > servlow and vert < servhigh then portb.0 = 0: portb.1 = 0: portb.2 = 0: portb.3 = 0 endif 'Checks to see if vertical component is between 'the minimum and maximum thresholds (neutral state) and sets 'all drive motor ports low to stop robot motion. goto start 'Jumps to start to gather new pulse widths. turn: 'defines turn sub If horz > servhigh then portb.0 = 1 portb.3 = 1 portb.1 = 0 portb.2 = 0 endif 'Checks if the horizontal component is beyond maximum threshold 'and sets pins controlling right motor reverse and left motor forward 'high, while setting right motor forward and left motor reverse low 'resulting in a right turn. If horz < servlow then portb.1 = 1 portb.2 = 1 portb.0 = 0 portb.3 = 0 endif 'Checks if the horizontal component is beyond maximum threshold 'and sets pins controlling right motor forward and left motor reverse 'high, while setting right motor reverse and left motor forward low 'resulting in a left turn. If horz > servlow and horz < servhigh then portb.0 = 0: portb.1 = 0: portb.2 = 0: portb.3 = 0 endif 'Checks to see if horizontal component is between minimum and 'maximun threshold (neutral state) and sets all drive pins low 'stopping robot motion. goto start 'jumps to start to gather new pulse widths. |


