]> rtime.felk.cvut.cz Git - eurobot/public.git/commitdiff
eb_jaws_11: adding buttons from lift to jaws+adding 3-color mode
authorcvrcevo1 <cvrcekv@gmail.com>
Sun, 30 Sep 2012 15:19:44 +0000 (17:19 +0200)
committercvrcevo1 <cvrcekv@gmail.com>
Sun, 30 Sep 2012 15:19:44 +0000 (17:19 +0200)
src/eb_jaws_11/main.c

index e77e741eb1be62f38e1f8b768974085439226f39..a575025b53b8a9281c5a34228d20d8115adc3999 100644 (file)
 #define        CAN_SPEED       1000000         //< CAN bus speed
 #define CAN_ISR                0
 
+#define START_SEND_PRIOD_FAST  50      /* [miliseconds] */
+#define START_SEND_PRIOD_SLOW  300     /* [miliseconds] */
+#define START_SEND_FAST_COUNT  10      /* How many times to send start with small period (after a change) */
+
 #define ADC_ISR                1
-#define CL_SENSOR_IN   16
-#define        CL_SENSOR_OUT   EXPPORT_3
+#define CL_SENSOR_IN   16 //port for sending command to color sensor(via power switch)
 
 #define JAW_LEFT       0
 #define JAW_RIGHT      1
 
 #define TIMER_IRQ_PRIORITY     5
 
-#define BUMPER_LEFT     EXPPORT_5
-#define BUMPER_RIGHT   EXPPORT_4
+#define BUMPER_LEFT            GPIOPORT_8
+#define BUMPER_RIGHT           GPIOPORT_6
 
-#define BUMPER_LEFT_ACROSS     EXPPORT_6
-#define BUMPER_RIGHT_ACROSS    EXPPORT_8
+#define BUMPER_LEFT_ACROSS     GPIOPORT_3
+#define BUMPER_RIGHT_ACROSS    GPIOPORT_1
 
-#define BUMPER_REAR_LEFT       EXPPORT_7
-#define BUMPER_REAR_RIGHT      EXPPORT_1
+#define CL_SENSOR_OUT/*BUMPER_REAR_LEFT*/ GPIOPORT_7 //port for receiving data from color sensor used to be BUMPER_REAR_LEFT
+#define BUMPER_REAR            GPIOPORT_4
 
-#define COLOR_PIN              GPIOPORT_1
-#define SWITCH_STRATEGY_PIN    GPIOPORT_2
+#define COLOR_PIN0             EXPPORT_4
+#define COLOR_PIN1             EXPPORT_5
+#define SWITCH_STRATEGY_PIN    EXPPORT_8
+#define START_PIN              EXPPORT_7
        
 
 struct fsm fsm_jaw_right;
@@ -96,6 +101,48 @@ void set_irq_handler(uint8_t source, uint8_t irq_vect, void (*handler)()) {
        VICIntEnable = 1<<source;
 }
 
+void start_button(void)
+{
+       can_msg_t msg;
+       bool start_condition;
+       static bool last_start_condition = 0;
+
+       static int count = 0;
+       static uint32_t next_send = 0;
+
+       
+       start_condition = (IO0PIN & (1<<START_PIN)) == 0;
+
+       if (start_condition != last_start_condition) {
+         
+               
+               last_start_condition = start_condition;
+               count = 0;
+               next_send = timer_msec; /* Send now */
+               
+       }
+
+       if (timer_msec >= next_send) {
+               msg.id = CAN_ROBOT_CMD;
+               msg.flags = 0;
+               msg.dlc = 1;
+               msg.data[0] = start_condition;
+               
+//             send_rs_str("start\n");
+               
+               /*while*/ (can_tx_msg(&msg));
+
+               if (count < START_SEND_FAST_COUNT) {
+                       count++;
+                       next_send = timer_msec + START_SEND_PRIOD_FAST;
+               } else
+                       next_send = timer_msec + START_SEND_PRIOD_SLOW;
+       }
+
+               
+}
+
+
 void robot_switches_handler()
 {
        static uint32_t color_time = 0;
@@ -107,17 +154,22 @@ void robot_switches_handler()
 
                color_time = timer_msec;
                
-               if (IO1PIN & (1<<COLOR_PIN))
-                       sw |= CAN_SWITCH_COLOR;
+               if (IO0PIN & (1<<COLOR_PIN0))
+                       sw |= CAN_SWITCH_COLOR_0;
+               else
+                       sw &= ~CAN_SWITCH_COLOR_0;
+               
+               if (IO0PIN & (1<<COLOR_PIN1))
+                       sw |= CAN_SWITCH_COLOR_1;
                else
-                       sw &= ~CAN_SWITCH_COLOR;
+                       sw &= ~CAN_SWITCH_COLOR_1;
 
-               if (IO1PIN & (1<<SWITCH_STRATEGY_PIN))
+               if (IO0PIN & (1<<SWITCH_STRATEGY_PIN))
                        sw &= ~CAN_SWITCH_STRATEGY;
                else
                        sw |= CAN_SWITCH_STRATEGY;
                
-               if (sw & CAN_SWITCH_COLOR){
+               if ((sw & CAN_SWITCH_COLOR_0) || (sw & CAN_SWITCH_COLOR_1)){
                        deb_led_off(LEDY);
                
                        send_rs_str("color\n");}
@@ -142,7 +194,6 @@ void robot_switches_handler()
                tx_msg.dlc = 1;
                tx_msg.flags = 0;
                tx_msg.data[0] = sw;
-               
                can_tx_msg(&tx_msg);
        }
 }
@@ -290,6 +341,16 @@ void can_send_status(struct fsm *fsm){
         
 }
 
+void cl_sensor_send_status(){
+       can_msg_t tx_msg;
+       tx_msg.id = CAN_CL_SENSOR_STATUS;
+       tx_msg.dlc = 5;
+       tx_msg.flags = 0;
+       tx_msg.data[0] = ((IO1PIN & 1<<CL_SENSOR_OUT) != 0) & 0xFF;// 1 - pattern match 0 - no match
+       if((IO1PIN & 1<<CL_SENSOR_OUT) != 0) send_rs_str("trefa\n");
+       (can_tx_msg(&tx_msg));
+}
+
 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
@@ -353,7 +414,7 @@ void handle_bumper()
                
                        
                        
-               if (IO0PIN & (1<<BUMPER_REAR_LEFT)){
+               /*if (IO1PIN & (1<<BUMPER_REAR_LEFT)){
                        sw &= ~CAN_BUMPER_REAR_LEFT;
                        
                        send_rs_str("rear_left\n");
@@ -364,22 +425,23 @@ void handle_bumper()
                        sw |= CAN_BUMPER_REAR_LEFT;
                        
 //                     deb_led_off(LEDY);
-               }
+               }*/
                
-               if (IO0PIN & (1<<BUMPER_REAR_RIGHT)){
+               if (IO1PIN & (1<<BUMPER_REAR)){
                        sw &= ~CAN_BUMPER_REAR_RIGHT;
-                       
-                       send_rs_str("rear_right\n");
+                       sw &= ~CAN_BUMPER_REAR_LEFT;
+                       send_rs_str("reart\n");
                        
 //                     deb_led_on(LEDY);
                }
                else{
                        sw |= CAN_BUMPER_REAR_RIGHT;
+                       sw |= CAN_BUMPER_REAR_LEFT;
                        
 //                     deb_led_off(LEDY);
                }
                
-               if (IO0PIN & (1<<BUMPER_LEFT)){
+               if (IO1PIN & (1<<BUMPER_LEFT)){
                        
                        sw &= ~CAN_BUMPER_LEFT;
                        
@@ -393,7 +455,7 @@ void handle_bumper()
                
 //                 deb_led_off(LEDB);
                }
-               if (IO0PIN & (1<<BUMPER_RIGHT)){
+               if (IO1PIN & (1<<BUMPER_RIGHT)){
                        sw &= ~CAN_BUMPER_RIGHT;
                        send_rs_str("right\n");
 //                     deb_led_on(LEDG);
@@ -403,7 +465,7 @@ void handle_bumper()
                
 //                     deb_led_off(LEDG);
                }
-               if (IO0PIN & (1<<BUMPER_LEFT_ACROSS)){
+               if (IO1PIN & (1<<BUMPER_LEFT_ACROSS)){
                        sw &= ~CAN_BUMPER_LEFT_ACROSS;
                        send_rs_str("left_across\n");
 //                     deb_led_on(LEDR);
@@ -413,7 +475,7 @@ void handle_bumper()
                
 //                     deb_led_off(LEDR);
                }
-               if (IO0PIN & (1<<BUMPER_RIGHT_ACROSS)){
+               if (IO1PIN & (1<<BUMPER_RIGHT_ACROSS)){
                        sw &= ~CAN_BUMPER_RIGHT_ACROSS;
                        send_rs_str("right_across\n");
 //                     deb_le0d_on(LEDY);
@@ -432,20 +494,17 @@ void handle_bumper()
 //             send_rs_int(IO1PIN);
 //             send_rs_int(sw);
 //             send_rs_str("\n");
-//             
-                       
                tx_msg.id = CAN_ROBOT_BUMPERS;
                tx_msg.dlc = 1;
                tx_msg.flags = 0;
-               tx_msg.data[0] = sw;
-               
+               tx_msg.data[0] = sw;            
                can_tx_msg(&tx_msg);
        }
 }
 
 void cl_sensor_init(){
        IO0DIR |= 1<<CL_SENSOR_IN;
-       IO0DIR &= ~(1<<CL_SENSOR_OUT);
+       IO1DIR &= ~(1<<CL_SENSOR_OUT);
        IO0SET = (1<<CL_SENSOR_IN);
        option = 3;
        option_new = 0;
@@ -467,21 +526,18 @@ int main(void)
        uint32_t status_time_left = timer_usec;
         uint32_t status_time_right = timer_usec;
        uint32_t status_time_sensor = timer_msec;
-       bool feedback;          
+       //bool feedback;                
        
        init_periphery();
                
-       SET_PIN(PINSEL1, 1, PINSEL_0);  
-       SET_PIN(PINSEL1, 2, PINSEL_0);
-       SET_PIN(PINSEL1, 3, PINSEL_0);
-       SET_PIN(PINSEL0, BUMPER_REAR_LEFT, PINSEL_0);
-       SET_PIN(PINSEL0, BUMPER_REAR_RIGHT, PINSEL_0);
-       SET_PIN(PINSEL0, BUMPER_RIGHT, PINSEL_0);
-       
-       
-//     IO0DIR &= ~((1<<BUMPER_REAR) | (1<<BUMPER_RIGHT) | (1<<BUMPER_LEFT) | (1<<BUMPER_RIGHT_ACROSS) | (1<<BUMPER_LEFT_ACROSS));
-       PINSEL2 &= ~(1 << 3) ;          
-       IO1DIR &= ~((1<<COLOR_PIN) | (1<<SWITCH_STRATEGY_PIN));
+       SET_PIN(PINSEL0, COLOR_PIN0, PINSEL_0);         
+       SET_PIN(PINSEL0, START_PIN, PINSEL_0);  
+       SET_PIN(PINSEL1, SWITCH_STRATEGY_PIN-16, PINSEL_0);
+       SET_PIN(PINSEL1, COLOR_PIN1-16, PINSEL_0);
+                       
+       PINSEL2 &= ~(1 << 3) ;          //setting P1.25:16 as GPIO pins -> GPIO PORT on eb_board
+       IO1DIR &= ~((1<<BUMPER_REAR) | /*(1<<BUMPER_REAR_LEFT) |*/ (1<<BUMPER_RIGHT) | (1<<BUMPER_LEFT) | (1<<BUMPER_RIGHT_ACROSS) | (1<<BUMPER_LEFT_ACROSS));  
+       IO0DIR &= ~((1<<COLOR_PIN0) | (1<<COLOR_PIN1) | (1<<START_PIN) | (1<<SWITCH_STRATEGY_PIN));
        cl_sensor_init();
        send_rs_str("Jaws started\n");
        //uint32_t i = 5000000;
@@ -491,27 +547,28 @@ int main(void)
        //letf jaw potenciometer on ADC 0, header J32 on board
        fsm_jaw_left.max_pos=0x320; //max 0x324
        fsm_jaw_left.min_pos=0xB0; //min 0xC3
-       fsm_jaw_left.act_pos = adc_val[0];
+       //fsm_jaw_left.act_pos = adc_val[0];
        
        //close jaw, max_pos, open jaw min_pos
        //letf jaw potenciometer on ADC 1, header J33 on board
        fsm_jaw_right.max_pos=0x380; //max 0x382
        fsm_jaw_right.min_pos=0x010; //min 0xF5
-       fsm_jaw_right.act_pos = adc_val[1];
+       //fsm_jaw_right.act_pos = adc_val[1];
        
        //left jaw engine is engine A, conector MOTA on board
-       fsm_jaw_left.engine_en = &engine_A_en;  
-       fsm_jaw_left.engine_dir = &engine_A_dir;
-       fsm_jaw_left.engine_pwm = &engine_A_pwm;
+       //fsm_jaw_left.engine_en = &engine_A_en;        
+       //fsm_jaw_left.engine_dir = &engine_A_dir;
+       //fsm_jaw_left.engine_pwm = &engine_A_pwm;
         
         fsm_jaw_left.can_id=CAN_JAW_LEFT_STATUS;
        
        //right jaw engine is engine B, conector MOTB on board
-       fsm_jaw_right.engine_en = &engine_B_en; 
-       fsm_jaw_right.engine_dir = &engine_B_dir;
-       fsm_jaw_right.engine_pwm = &engine_B_pwm;
+       //fsm_jaw_right.engine_en = &engine_B_en;       
+       //fsm_jaw_right.engine_dir = &engine_B_dir;
+       //fsm_jaw_right.engine_pwm = &engine_B_pwm;
         
         fsm_jaw_right.can_id=CAN_JAW_RIGHT_STATUS;
+
        
        init_fsm(&fsm_jaw_right, &fsm_jaw_init);
        init_fsm(&fsm_jaw_left, &fsm_jaw_init);
@@ -524,10 +581,10 @@ int main(void)
                        //dbg_print_time();
 
                        //fsm_jaw_left.act_pos = adc_val[0];
-                       //run_fsm(&fsm_jaw_left);
+                       run_fsm(&fsm_jaw_left);
                        
                        //fsm_jaw_right.act_pos = adc_val[1];
-                       //run_fsm(&fsm_jaw_right);
+                       run_fsm(&fsm_jaw_right);
                  
                }
                if(timer_msec >= status_time_sensor + 10){
@@ -545,6 +602,7 @@ int main(void)
                              }
                        }
                        }
+                       cl_sensor_send_status();
                //      feedback = (IO0PIN & 1<<CL_SENSOR_OUT) != 0;
                //      if(feedback) send_rs_str("shoda barev\n");
                //              else send_rs_str("jina barva\n");                       
@@ -553,7 +611,7 @@ int main(void)
                    fsm_jaw_left.trigger_can_send*/) {   //or when something important happen
                        //fsm_jaw_left.trigger_can_send = false;
                        status_time_left = timer_msec; //save new time, when message was sent
-               //      can_send_status(&fsm_jaw_left);
+                       can_send_status(&fsm_jaw_left);
 
                }
                 
@@ -561,12 +619,12 @@ int main(void)
                     fsm_jaw_right.trigger_can_send*/) {   //or when something important happen
                         //fsm_jaw_right.trigger_can_send = false;
                         status_time_right = timer_msec; //save new time, when message was sent
-                 //       can_send_status(&fsm_jaw_right);
+                        can_send_status(&fsm_jaw_right);
                
                        
 
-                }
-                
+                }                
+                start_button();
                handle_bumper();
                robot_switches_handler();
                blink_led();