]> rtime.felk.cvut.cz Git - eurobot/public.git/commitdiff
add CAN message sending and recieving to the carousel
authorOndra Vrzal <ondra.vrzal@gmail.com>
Sat, 26 Apr 2008 12:36:58 +0000 (14:36 +0200)
committerMichal Sojka <sojkam1@fel.cvut.cz>
Mon, 28 Jul 2008 11:17:10 +0000 (13:17 +0200)
ondra

src/eb_carousel/Makefile.omk
src/eb_carousel/carousel.c

index 0d329df582f0b47bb408eae91a120b819e0bc068..2ece3e040b70ab09487ffc1fbea7034bf056a457 100644 (file)
@@ -3,4 +3,4 @@
 bin_PROGRAMS = carousel
 
 carousel_SOURCES = carousel.c
-carousel_LIBS = ebb
+carousel_LIBS = ebb can
index 61ffa961b778216c6c9154bf62bc6f625b955d9c..e9ac0bfa7ca425bd093b956f56360516cb17340a 100644 (file)
@@ -6,11 +6,16 @@
 #include <powswitch.h>
 #include <system_def.h>
 #include <servo.h>
+#include <can_ids.h>
+#include <periph/can.h>
+#include <string.h>
+
 
 #define        CAN_SPEED       1000000
 #define CAN_ISR                0
 
 #define ENG_ISR                1
+// the interrupt for the timer is somehow set in this class perhaps 4
 
 #define SERVO_ISR      5
 
@@ -24,27 +29,35 @@ enum Event {EVENT_ENTRY, EVENT_DO, EVENT_EXIT};
 enum ErrorState {ERROR, NOTICE, NO_ERROR}; 
 
 
-unsigned int validReading= 0; // 0 - carusel position signal 0- not valid, 1- valid
-unsigned int validPosition= 0; // 0..4 read position from carusel
+volatile unsigned int validReading= 0; // 0 - carusel position signal 0- not valid, 1- valid
+volatile unsigned int validPosition= 0; // 0..4 read position from carusel
 
 unsigned int timeNextPos= 300; // default time to move to next postion, initialized on the beginning
 unsigned int timeNext2Pos= 600; // default time to move to next postion, initialized on the beginning
 
-unsigned int requestedPosition= 0; // the positon the carusel should move to
-unsigned int moveCarousel= 0; // request to move the carusel
+volatile unsigned int requestedPosition= 0; // the positon the carusel should move to
+volatile unsigned int moveCarousel= 0; // request to move the carusel
 enum Direction {FORWARD, BACKWARD}; // forward - 4,3,2,1,0, backward= 0,1,2,3,4
 enum Direction moveDirection= FORWARD; // the direction carousel should move
 
-unsigned int flashLEDB= 0; // when set blue led will flash
+unsigned int flashLEDB= 0; // when set blue led will flash; the sensors sense new valid position (overshoot position)
+unsigned int flashLEDY= 0; // when set blue led will flash; the requested position was set
+
+unsigned int sendCANmessage= 0; //flag to sent CAN data
+unsigned int sendCANdata= 0; // data to send
+
                
 enum ErrorState errorState= NO_ERROR;
 
+can_msg_t msg; // pointer to the recieved can message
+
 static void Init_timer(void);
 void delay(int interval);
 void init_carousel(void);
 unsigned int getCurrentPosition(void);
 void gotoPos(unsigned int position);
 unsigned int getTime(void);
+void can_rx(can_msg_t *msg);
 
 // main loop
 void handle_CAN(void);
@@ -77,7 +90,7 @@ void init_perip(void)      // inicializace periferii mikroprocesoru
 
         init_uart0((int)9600 ,UART_BITS_8, UART_STOP_BIT_1, UART_PARIT_OFF, 0 );
 
-        //can_init_baudrate(CAN_SPEED, CAN_ISR, can_rx);
+       can_init_baudrate(CAN_SPEED, CAN_ISR, can_rx);
 }
 
 
@@ -87,6 +100,18 @@ int main (void)
        
        init_perip();
        
+       moveCarousel= 0;
+                       
+       // send information on the CAN, that carusel is now at the right position
+       msg.id = CAN_CAROUSEL;
+       msg.flags = 0;
+       msg.dlc = 1;
+       msg.data[0] = 0x0ff;
+                       
+                       // !! what it the message would not be sent??
+       while(can_tx_msg(&msg));
+       
+       
        uart0SendCh(0x0D);
        uart0SendCh(0x0A);
        
@@ -177,22 +202,67 @@ static void Init_timer(void)
  * CAN temporary simulated by pressing the keys, unfortunately stops the state machine
  */
 int prevMoveCarousel=0;
+const unsigned int useKeyboard=0;
+const unsigned int useArray=1;
+const unsigned int sequens[14]= {0,1,2,3,4,3,2,1,0,2,4,3,1,4};
+unsigned int sequensX= 0;
+
+unsigned int readyToGo=0;
+unsigned int timeToGo=0;
 void handle_CAN(void)
 {
        // !! need to be implemented
        unsigned int implement=1;
        implement++;
 
-       if( (moveCarousel==0)&&(prevMoveCarousel!=0) )
-       {
-               uart0SendCh('x');
-               unsigned int next_pos= (unsigned int) uart0GetCh();
-               next_pos= next_pos -48; // recived character is in ASCII
-               if (next_pos>4) next_pos=0;
-               requestedPosition= next_pos;
-               moveCarousel= 1;
+       // send information on the CAN, that carusel is now at the right position
+       if ( (sendCANmessage)&&(useKeyboard==0)&&(useArray==0) )// real mode should used rx, tx CAN data
+               {
+               sendCANmessage=0;
+               msg.id = CAN_CAROUSEL;
+               msg.flags = 0;
+               msg.dlc = 1;
+               msg.data[0] = sendCANdata;
+       
+               // !! what it the message would not be sent??
+               while(can_tx_msg(&msg));
+       }
 
+       // auto setting new the positions, used for debugging
+       else if( (moveCarousel==0)&&(prevMoveCarousel!=0) )
+       {
+               if (useKeyboard)
+               {
+                       // used to set the position of carousel by keyboard
+                       uart0SendCh('x');
+                       unsigned int next_pos= (unsigned int) uart0GetCh();
+                       next_pos= next_pos -48; // recived character is in ASCII
+                       if (next_pos>4) next_pos=0;
+                       requestedPosition= next_pos;
+                       moveCarousel= 1;
+       
+               }
+               // preprogrammed sequence of positions
+               else if (useArray);
+               {
+                       // !!! wait just for can
+                       readyToGo= 1;
+                       // wait a while before next turn
+                       timeToGo = getTime() +2000;
+               }
+       }
+       
+       if (readyToGo)
+       {
+               if (timeToGo< getTime())
+               {
+                       readyToGo= 0;
+                       requestedPosition= sequens[sequensX];
+                       if (sequensX<12) sequensX++;
+                       moveCarousel= 1;                        
+               }
        }
+       
        prevMoveCarousel= moveCarousel;
 
 }
@@ -213,16 +283,20 @@ void send_data(void)
  */
 unsigned int okTimeToBlink= 0;
 unsigned int okLEDstate= 0;
-unsigned int okLEDinterval= 500;
+const unsigned int okLEDinterval= 500;
 
 unsigned int errTimeToBlink= 0;
 unsigned int errLEDstate= 0;
-unsigned int errLEDinterval= 200;
-unsigned int noticeLEDinterval= 1000;
+const unsigned int errLEDinterval= 200;
+const unsigned int noticeLEDinterval= 1000;
 
 unsigned int blueTimeToBlink= 0;
 unsigned int blueLEDstate= 0;
-unsigned int blueLEDinterval= 500;
+const unsigned int blueLEDinterval= 100;
+
+unsigned int yBlinkTime=0;
+unsigned int yLEDstate= 0;
+const unsigned int yLEDinterval= 1000;
 
 enum ErrorState lastErrorState= NO_ERROR; // used to keep information about error, used to cover the case, when NOTICE change errorState from ERROR
 
@@ -291,6 +365,24 @@ void blink_LED(void)
                }
        }
        
+       // check if blue LED should flash
+       if(flashLEDY)
+       {
+               flashLEDY= 0;   
+               yLEDstate=1;
+               deb_led_on(LEDY);
+               yBlinkTime= getTime() +yLEDinterval;
+       }
+       // after specified interval turn off the blue LED
+       if(yLEDstate==1)
+       {
+               if( yBlinkTime<getTime() )
+               {
+                       deb_led_off(LEDY);
+                       yLEDstate=0;
+               }
+       }       
+       
 }
 
 
@@ -482,6 +574,11 @@ void state_returnToPosition(enum Event my_event)
                        // the task was achieved, carousel is at the right position
                        moveCarousel= 0;
                        
+                       sendCANmessage= 1;
+                       sendCANdata= 1;
+                       
+                       flashLEDY= 1;
+                       
                        // stop the carousel
                        engine_A_pwm(0);                        
                        uart0SendCh('d');
@@ -663,6 +760,57 @@ void init_carousel(void)
        uart0SendCh('i');       
 }
 
+/**
+ * handels messages recieved by the CAN controller
+ *
+ */
+void can_rx(can_msg_t *msg) {
+       can_msg_t rx_msg;
+       unsigned int newPos;    
+       
+       memcpy(&rx_msg, msg, sizeof(can_msg_t));
+       
+       switch (rx_msg.id) 
+       {               
+               case CAN_SERVO:
+                       //set_servo(0, rx_msg.data[0]);
+                       //set_servo(2, rx_msg.data[2]);
+                       break;
+               
+               case CAN_DRIVES:
+                       
+                       newPos= rx_msg.data[3];
+                       // check for valid position                     
+                       if ( (newPos>=0) && (newPos<=4) ) 
+                       {
+                               // when carousel is already moving
+                               if(moveCarousel==1) 
+                               {
+                                       if (newPos== requestedPosition)
+                                       {
+                                               // BOA is repatly sending the position
+                                       }
+                                       else
+                                       {
+                                               // while carousel was moving new position was requested
+                                               current_state= &state_stop;
+                                               requestedPosition= newPos;
+                                       }
+                               }
+                               // carousel is in stop position
+                               else
+                               {
+                                       moveCarousel= 1;
+                                       requestedPosition= newPos;
+                               }
+                       
+                       }
+                       break;
+                       
+               default:
+                       break;
+       }
+}
 
 
 /**
@@ -683,6 +831,6 @@ unsigned int getTime(void)
  */
 void delay(int interval)
 {
-       int myTime= getTime();
+       unsigned int myTime= getTime();
        while( getTime()< (myTime+interval) );
 }