From 6e570bc3c94d2bd2b82614a1c541b1068e2f3397 Mon Sep 17 00:00:00 2001 From: Ondra Vrzal Date: Sat, 26 Apr 2008 14:36:58 +0200 Subject: [PATCH] add CAN message sending and recieving to the carousel ondra --- src/eb_carousel/Makefile.omk | 2 +- src/eb_carousel/carousel.c | 186 +++++++++++++++++++++++++++++++---- 2 files changed, 168 insertions(+), 20 deletions(-) diff --git a/src/eb_carousel/Makefile.omk b/src/eb_carousel/Makefile.omk index 0d329df5..2ece3e04 100644 --- a/src/eb_carousel/Makefile.omk +++ b/src/eb_carousel/Makefile.omk @@ -3,4 +3,4 @@ bin_PROGRAMS = carousel carousel_SOURCES = carousel.c -carousel_LIBS = ebb +carousel_LIBS = ebb can diff --git a/src/eb_carousel/carousel.c b/src/eb_carousel/carousel.c index 61ffa961..e9ac0bfa 100644 --- a/src/eb_carousel/carousel.c +++ b/src/eb_carousel/carousel.c @@ -6,11 +6,16 @@ #include #include #include +#include +#include +#include + #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=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) ); } -- 2.39.2