#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
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);
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);
}
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);
* 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;
}
*/
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
}
}
+ // 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;
+ }
+ }
+
}
// 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');
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;
+ }
+}
/**
*/
void delay(int interval)
{
- int myTime= getTime();
+ unsigned int myTime= getTime();
while( getTime()< (myTime+interval) );
}