Advertisement
Guest User

motion.c

a guest
Jan 23rd, 2013
19
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.35 KB | None | 0 0
  1. /****************************************************************
  2.  
  3. Code by Rue_mohr, Mar 16 2008
  4.  
  5. wrapper for core functionality of a servo position controller
  6. the lines are blurred
  7.  
  8. 9600 N81
  9. servos go on port A
  10. software handles 8 of them
  11.  
  12. control as per http://www.lavrsen.dk/twiki/bin/view/Motion/MotionTrackerAPI
  13.  
  14. version 6 adds commands to read back the positions
  15.  
  16. *****************************************************************/
  17.  
  18.  
  19. //-----| Include Files |-------
  20.  
  21. // this is a link to the prycon lib for sending serial data
  22. #define SendByte(A)    uartSendByte(A)
  23.  
  24. #define SetBit(BIT, PORT)     PORT |= (1<<BIT)
  25. #define ClearBit(BIT, PORT)   PORT &= ~(1<<BIT)
  26. #define IsSet(BIT, PORT)    (PORT & (1<<BIT)) != 0
  27. #define IsLow(BIT, PORT)     (PORT & (1<<BIT)) == 0
  28.  
  29.  
  30.  
  31. #define Button0Bit   0
  32. #define Button0Port  PINC
  33. #define IsDown0()    IsLow(Button0Bit,Button0Port)
  34.  
  35. #define Button1Bit   1
  36. #define Button1Port  PINC
  37. #define IsDown1()    IsLow(Button1Bit,Button0Port)
  38.  
  39. #define Button2Bit   2
  40. #define Button2Port  PINC
  41. #define IsDown2()    IsLow(Button2Bit,Button0Port)
  42.  
  43. #define Button3Bit   3
  44. #define Button3Port  PINC
  45. #define IsDown3()    IsLow(Button3Bit,Button0Port)
  46.  
  47. #include <avr/io.h>    
  48. #include <avr/interrupt.h> 
  49. #include "global.h"        
  50. #include "timerx8.h"       
  51. #include "servo.h" 
  52. #include "uart.h"  
  53. #include "hservmot.c"
  54.  
  55.  
  56. #define OUTPUT             1
  57. #define INPUT              0
  58.  
  59.  
  60. void steptimeInit(void) ;
  61. void initUart(void) ;
  62. void servoSetup(void) ;
  63. void Delay(unsigned int delay) ;
  64.  
  65.  
  66. // --| main |--
  67.  
  68. int main( void ) {
  69.  
  70.   unsigned char i;
  71.  
  72.   PORTC  = (1<<PC0)|(1<<PC1)|(1<<PC2)|(1<<PC3)|(1<<PC4)|(1<<PC5)|(1<<PC6);
  73.  
  74.   initMotors();   // initialize motor system
  75.   initComms();    // initialize communication system
  76.   initUart();     // initialize serial system
  77.   timerInit();    // initialize timer system
  78.   steptimeInit(); // initialize motion timer
  79.   servoSetup();   // initialize servo system THIS MUST BE AFTER INITMOTORS
  80.   sei();          // enable interrupts
  81.  
  82.   i = 0;
  83.   while (1) {  
  84.     Delay(65535);
  85.     Delay(65535);
  86.    
  87.     if (0) {
  88.     } else if (IsDown0()) {  // next servo
  89.       i++;
  90.       if (i > 7) i = 0;
  91.     } else if (IsDown1()) {  // servo to the right
  92.    
  93.         // dont let it roll over 255
  94.        if ((255 - motors[i].position ) >= 4) {
  95.          motors[i].target = motors[i].position + 4;
  96.        } else {
  97.          motors[i].target = 255;
  98.        }
  99.        SetBit(BEHAVIOUR_DIRRIGHT, motors[i].behaviour );
  100.        ClearBit(BEHAVIOUR_SWEEP, motors[i].behaviour );
  101.  
  102.     } else if (IsDown2()) {  // servo to the left
  103.    
  104.        // dont let it roll under 0
  105.        if (motors[i].position >= 4) {
  106.          motors[i].target = motors[i].position - 4;
  107.        } else {
  108.          motors[i].target = 0;
  109.        }
  110.        ClearBit(BEHAVIOUR_DIRRIGHT, motors[i].behaviour );
  111.        ClearBit(BEHAVIOUR_SWEEP, motors[i].behaviour );
  112.  
  113.     } else if (IsDown3()) {  // servo shake it all about
  114.        motors[i].target = 128;
  115.        ClearBit(BEHAVIOUR_SWEEP, motors[i].behaviour );
  116.  
  117.     }
  118.  
  119.   }  
  120. }
  121.  
  122.  
  123. void Delay(unsigned int delay) {
  124.   unsigned int x;
  125.   for (x = delay; x != 0; x--) {
  126.     asm volatile ("nop"::);
  127.   }
  128. }
  129.  
  130.  
  131.  
  132.  
  133. void steptimeInit() {
  134.  // set up timer for interrupt rate of about .001 sec (divide by 64 at 16Mhz)
  135.  timer0SetPrescaler (TIMER_CLK_DIV64);
  136.  // set timer to call doTick
  137.  timerAttach ( TIMER0OVERFLOW_INT, doTick );
  138. }
  139.  
  140. void initUart() {
  141.   uartInit();                       // initialize UART (serial port)
  142.   uartSetBaudRate(9600);            // set UART speed to 9600 baud
  143.   uartSetRxHandler ( ReceiveByte );  // direct incomming data to our mouth
  144. }
  145.  
  146. void servoSetup(void)   {
  147.   unsigned char i;
  148.   servoInit();
  149.   servoSetChannelIO(0, _SFR_IO_ADDR(PORTB), PB0);
  150.   servoSetChannelIO(1, _SFR_IO_ADDR(PORTB), PB1);
  151.   servoSetChannelIO(2, _SFR_IO_ADDR(PORTB), PB2);
  152.   servoSetChannelIO(3, _SFR_IO_ADDR(PORTB), PB3);
  153.   servoSetChannelIO(4, _SFR_IO_ADDR(PORTB), PB4);
  154.   servoSetChannelIO(5, _SFR_IO_ADDR(PORTB), PB5);
  155.  
  156.   DDRB =  (OUTPUT<<PB0)|(OUTPUT<<PB1)|(OUTPUT<<PB2)|(OUTPUT<<PB3)|
  157.           (OUTPUT<<PB4)|(OUTPUT<<PB5);
  158.  
  159.   // enable and disable pullups
  160.   PORTB  = (1<<PB0)|(1<<PB1)|(1<<PB2)|(1<<PB3)|(1<<PB4)|(1<<PB5);
  161.  
  162.   // make sure position is set on startup
  163.   for (i = 0; i < 6; i++) {
  164.    servoSetPosition(i, motors[i].position);
  165.   }
  166.  
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement