Advertisement
Catachan

Untitled

Aug 21st, 2012
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.33 KB | None | 0 0
  1. /*
  2.  * This program operates Dory the robot fish.
  3.  */
  4.  
  5. /* Include Statement Block */
  6. //#include <NewSoftSerial.h>
  7.  
  8. #include <PString.h>
  9.  
  10. #include <inttypes.h>
  11. #include <avr/io.h>
  12. #include <avr/pgmspace.h>
  13. #include <avr/eeprom.h>
  14. #include <avr/interrupt.h>
  15. #include <stdio.h>
  16. #include <util/delay.h>
  17.  
  18. /* End Include Statement Block */
  19.  
  20. /* Begin Defines block */
  21. #define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
  22. #define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
  23. #define FOSC 16000000 // Clock Speed
  24. #define BAUD 4800
  25. #define MYUBRR FOSC/16/BAUD-1
  26. # define ON 1
  27. # define OFF 0
  28. #define TOGGLE_IO        9  //Arduino pin to toggle in timer ISR
  29. #define TIMER_CLOCK_FREQ 1000000.0 //2MHz for /8 prescale from 16MHz
  30.  
  31. /* End Defines Block */
  32.  
  33. /* Begin Global Variable deffinitions */
  34. volatile unsigned char counter = 48;
  35. volatile unsigned char result = 0;
  36. char cFishStatus = ON;
  37. unsigned char ucCommandCnt = 0,
  38. ucCommand_1 = OFF,//jr
  39. ucCommand_2 = OFF,
  40. ucCommand_3 = OFF;
  41. unsigned char ucFrontPositionIndex = 128,
  42. ucRearPositionIndex = 128;
  43. float sensorValue0,
  44. sensorValue1;
  45. char s0[10];
  46. char s1[10];
  47. int sensorpin0 = 0 ;
  48. int sensorpin1 = 1;
  49. // char* current_ascii0= itoa(sensorValue0,s0,10);
  50. // char* current_ascii1= itoa(sensorValue1,s1,10);
  51. int i=0,
  52. j=0;
  53. int inByte=0;
  54. int pos_a = 128,
  55. pos_b = 128,    //variable to store the servo position
  56. pwm1 = 1500,
  57. pwm2 = 1500;
  58. unsigned char dat=0x00;
  59. char cbuffer[40] = "";     // receive buffer
  60. //NewSoftSerial IMU(2, 3);
  61. int stop_char=0,
  62. head_val=0;
  63. char bufarray[20]="";
  64. char comma_counter=0;
  65. int sensorPin = 3;
  66. char iii=0;
  67. //char s[10];
  68. //int sensorPin = 0;    // select the input pin for the potentiometer
  69. //    // select the pin for the LED
  70. //float sensorValue = 0;
  71. /* End Global Variable deffinitions */
  72.  
  73. PString buffer(cbuffer, sizeof(cbuffer));
  74.  
  75. void USART_Init( unsigned int ubrr)
  76. {
  77.   sbi(UCSR0A, U2X0);
  78.   sbi(UCSR0B, RXCIE0);
  79.   sbi(UCSR0B, RXEN0);
  80.   sbi(UCSR0B, TXEN0);
  81.   UCSR0C = B00000110;
  82.   //UBRR0H = B0;
  83.   //UBRR0L = B00010000;
  84.   UBRR0H = (unsigned char)(ubrr>>8);
  85.   UBRR0L = (unsigned char)ubrr;
  86. }
  87.  
  88.  
  89.  
  90.  
  91.  
  92. void uart_send(unsigned char dat)
  93. {
  94.   while(! (UCSR0A & ( 1 << UDRE0))  );  // until dararegister is NOT empty
  95.   UDR0 = dat;                          
  96. }
  97.  
  98. unsigned char uart_read()
  99. {
  100.   while(!  (UCSR0A & (1 << RXC0)) );  // until Receive complete is NOT complete
  101.   return UDR0;
  102. }
  103.  
  104.  
  105. ISR(USART_RX_vect)
  106. {
  107.   //   counter++;
  108.  
  109.   inByte = UDR0;
  110.  
  111.   //   if (cFishStatus == OFF && inByte ==0x55)
  112.   //    {
  113.   //      UDR0=inByte;
  114.   //      if (++ucCommandCnt == 5)
  115.   //      {
  116.   //         cFishStatus = ON;
  117.   //          uart_send(0x24);
  118.   //      }
  119.   //    return;  
  120.   //  }
  121.  
  122.   if(cFishStatus == ON)
  123.   {
  124.  
  125.  
  126.     if(ucCommand_1 == ON)
  127.     {
  128.       ucCommand_1 = OFF;
  129.       ucFrontPositionIndex = inByte;
  130.  
  131.       uart_send(inByte);
  132.  
  133.  
  134.       return;
  135.     }
  136.     if(ucCommand_2 == ON)
  137.     {
  138.       ucCommand_2 = OFF;
  139.       ucRearPositionIndex = inByte;
  140.  
  141.  
  142.       uart_send(pwm1);
  143.  
  144.  
  145.       return;  
  146.     }
  147.  
  148.  
  149.     if(ucCommand_1 == OFF && inByte == 0x31)
  150.     {
  151.       ucCommand_1 = ON;
  152.  
  153.  
  154.  
  155.  
  156.       return;
  157.     }
  158.     if(ucCommand_2 == OFF && inByte == 0x32)
  159.     {  
  160.       ucCommand_2 = ON;
  161.  
  162.       return;
  163.     }        
  164.   }  
  165.  
  166.  
  167.  
  168.  
  169. }
  170.  
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179. unsigned char SetupTimer1(float timeoutFrequency)
  180. {
  181.   unsigned char result; //The value to load into the timer to control the timeout interval.
  182.  
  183.   //Calculate the timer load value
  184.   result=(int)((257.0-(TIMER_CLOCK_FREQ/timeoutFrequency))+0.5); //the 0.5 is for rounding;
  185.   //The 257 really should be 256 but I get better results with 257, dont know why.
  186.  
  187.  
  188.   //  //Timer2 Overflow Interrupt Enable  
  189.   TCCR1A = _BV(COM1A1) | _BV(COM1B1) | _BV(WGM11);
  190.   // clear on compare, fast PWM, TOP=ICR1 (WGM13/WGM12 in TCCR1B)
  191.   TCCR1B = _BV(WGM12) | _BV(WGM13) |_BV(CS11); //_BV(CS10) | _BV(CS12); // prescaler 1024
  192.   ICR1 = 40000;//result;
  193.   OCR1A = 1100;//off
  194.   OCR1B = 1100;//off
  195.   DDRB |= _BV(1) | _BV(2); // output on B1 and B2
  196.   //}
  197.  
  198.   TIMSK1 = 1<<TOIE1;
  199.  
  200.  
  201.   return(result);
  202. }
  203.  
  204.  
  205.  
  206.  
  207.  
  208. //ISR(ADC_vect)
  209. //{
  210. //  sensorValue = analogRead(sensorPin);
  211.  
  212. //float sens_tran=sensorValue*.0017+.02;
  213.  
  214. // char* current_ascii= itoa(sensorValue,s,10);
  215.  
  216. // uart_send(*current_ascii);
  217. // uart_send(*current_ascii++);
  218. // uart_send(0xA);
  219. //}
  220.  
  221. //Timer2 overflow interrupt vector handler
  222. ISR(TIMER1_OVF_vect)
  223. {
  224.   pwm1=2*(1900-(int)(3*ucFrontPositionIndex));
  225.   pwm2=2*(1900-(int)(3*ucRearPositionIndex));
  226.  
  227.   OCR1A=pwm1;
  228.   OCR1B=pwm2;
  229.  
  230.   //set_servo1(pwm1);}?K;=p0
  231.   //set_servo2(pwm2);
  232. }
  233.  
  234.  
  235. // sets "high" time of B1 for pwm*64us
  236. void set_servo1(int pwm1)
  237. {
  238.   OCR1A = pwm1;
  239. }
  240.  
  241. // sets "high" time of B2 for pwm*64us
  242. void set_servo2(int pwm2)
  243. {
  244.   OCR1B = pwm2;
  245. }
  246.  
  247.  
  248.  
  249. //void IMU_Rx_Tx()
  250. //{
  251. //
  252. //    if (IMU.available())
  253. //    {
  254. //      
  255. //      int c = IMU.read();
  256. //      
  257. //      if (c==44){  
  258. //  
  259. //        comma_counter=comma_counter+1;
  260. //                }
  261. //  
  262. //   if (comma_counter==2){
  263. //  
  264. //     if(c!=44 && c!=10){
  265. //  
  266. //     bufarray[iii]=c;
  267. //          
  268. //     iii=iii+1;
  269. //  
  270. //     }
  271. //     if(c==10)
  272. //     {
  273. //       comma_counter=0;
  274. //  
  275. //  
  276. //   uart_send(0x21);
  277. //   uart_send(0xA);
  278. //   uart_send(bufarray[0]);
  279. //   uart_send(bufarray[1]);
  280. //   uart_send(bufarray[2]);
  281. //   uart_send(bufarray[3]);
  282. //   uart_send(bufarray[4]);
  283. //   uart_send(bufarray[5]);
  284. //   uart_send(bufarray[6]);
  285. //   uart_send(0xA);
  286. //
  287. //sensorValue = (analogRead(sensorPin)*.009+.01)*100;//.00169+.01)*100;//.77487)*100;
  288. //  
  289. //   //float sens_tran=sensorValue*.0017+.02;
  290. //  
  291. //  char* current_ascii= itoa(sensorValue,s,10);
  292. //  
  293. //   uart_send(0x23);
  294. //   uart_send(0xA);
  295. //   uart_send(s[0]);
  296. //   uart_send(s[1]);
  297. //   uart_send(s[2]);
  298. //   uart_send(s[4]);
  299. //   uart_send(0xA);
  300. //
  301. //iii=0;
  302. //     }
  303. //   }
  304. //}
  305. //  }
  306.  
  307.  
  308.  
  309.  
  310. //void current_val(void)
  311. //{
  312. //  
  313. //   sensorValue = analogRead(sensorPin);
  314. //  
  315. //   //float sens_tran=sensorValue*.0017+.02;
  316. //  
  317. //  char* current_ascii= itoa(sensorValue,s,10);
  318. //  
  319. //   uart_send(*current_ascii);
  320. //   uart_send(*current_ascii++);
  321. //   uart_send(0xA);
  322. //  
  323. //}
  324.  
  325.  
  326. void setup()
  327. {
  328.   SetupTimer1(50);
  329.   USART_Init(MYUBRR);
  330.  
  331.  
  332.   //IMU.begin(9600);
  333.  
  334.   uart_send(0x21);
  335.   uart_send(0x40);
  336.   uart_send(0x23);
  337.  
  338. }
  339.  
  340.  
  341. void loop()
  342. {
  343.   //sensorValue0 = analogRead(0);    
  344.  
  345.   //uart_send(0x30);
  346.   //uart_send(0xA);
  347.   //Serial.print(sensorValue0);    
  348.   // uart_send(0x30);
  349.   //ISR(ADC_vect)
  350.   //{
  351.  
  352.  
  353.   // TO ACTIVATE VOLTAGE READINGS, UNCOMMENT FROM HERE-----------
  354.  
  355.       delay(100);
  356.       sensorValue0 = analogRead(sensorpin0);
  357.       sensorValue1 = analogRead(sensorpin1);
  358.      //float sens_tran=sensorValue*.0017+.02;
  359.    
  360.      char* current_ascii0= itoa(sensorValue0,s0,10);
  361.      char* current_ascii1= itoa(sensorValue1,s1,10);
  362.      
  363.      uart_send(0x30);
  364.      uart_send(0xA);
  365.      uart_send(*current_ascii0);
  366.      uart_send(*current_ascii0++);
  367.      uart_send(0xA);
  368.      
  369.      uart_send(0x31);
  370.      uart_send(0xA);
  371.      uart_send(*current_ascii1);
  372.      uart_send(*current_ascii1++);
  373.      uart_send(0xA);
  374.   //}
  375.  
  376.   //   TO HERE -------------------------
  377.  
  378.  
  379.  
  380.  
  381.   // while(1){};
  382.   //  buffer="";
  383.   //
  384.   // if(cFishStatus == ON){
  385.   //    IMU_Rx_Tx();
  386.   //    //delay(50);
  387.   //    //current_val();
  388.   //    //delay(50);
  389.   //}
  390. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement