fsmdet.c

00001 /*
00002  * detfsm.c                     07/04/20
00003  *
00004  * Finite state machine of the detection part.
00005  *
00006  * Copyright: (c) 2007 DCE Eurobot Dragon Team
00007  *            CTU FEE - Department of Control Engineering
00008  * License: GNU GPL v.2
00009  */
00010 
00011 #define FSM_DETECT
00012 #include "robot.h"
00013 #include "fsm.h"
00014 #include "fsmmain.h"
00015 #include "movehelper.h"
00016 #include "serva.h"
00017 #include <math.h>
00018 
00019 #define WASTE_STACK_EMPTYm              0xe0
00020 #define WASTE_INNER_STACK_BOTTLE1       0x1e
00021 #define WASTE_INNER_STACK_BOTTLE2       0x0f
00022 
00023 #define CLOSE_LIMIT 850
00024 #define FAR_LIMIT 650
00025 
00026 #define VMAX 0.5;
00027 //#define JOYSTICK_NAV 1
00028 #define R 0.15
00029 
00030 void evaluate_distance(uint16_t value, uint8_t *result) {
00031         if((value > CLOSE_LIMIT)) *result = CLOSE;
00032         else if((value >= FAR_LIMIT)&&(value < CLOSE_LIMIT)) *result = FAR;
00033         else if(value < FAR_LIMIT) *result = NOTHING;
00034 }
00035 
00036 FSM_STATE_DECL(det_start);
00037 FSM_STATE_DECL(det_scan);
00038 
00039 FSM_STATE(det_start) {
00040         FSM_TIMEOUT(100);
00041 //      if(robot.start) {
00042                 FSM_SIGNAL(MAIN, EV_START);
00043                 FSM_TRANSITION(det_scan);
00044 //      }
00045 }
00046 
00047 FSM_STATE(det_scan) {
00048 
00049         FSM_TIMEOUT(100);
00050 //      ROBOT_LOCK();
00051         
00052         robot.waste_cnt = 0;
00053 /*      if(robot.sharpsWaste.short1 > VERY_FAR_LIMIT) robot.waste_cnt++;
00054         if(robot.sharpsWaste.short2 > VERY_FAR_LIMIT) robot.waste_cnt++;
00055         if(robot.sharpsWaste.short3 > VERY_FAR_LIMIT) robot.waste_cnt++;
00056         if(robot.sharpsWaste.short4 > VERY_FAR_LIMIT) robot.waste_cnt++;*/
00057         
00058 //      printf("waste_cnt=%d\n",robot.waste_cnt);
00059         
00060         switch (FSM_EVENT) {
00061                 case EV_STATE_ENTERED:
00062                         break;
00063                 case EV_TIMEOUT:
00064                         //printf("EVENT_STATE_ENTERED - pickup ready\n");
00065                         if(robot.waste_cnt) {
00066                                 evaluate_distance(robot.sharpsWaste.short1, &robot.bott_sens.sens1);
00067                                 evaluate_distance(robot.sharpsWaste.short2, &robot.bott_sens.sens2);
00068                                 evaluate_distance(robot.sharpsWaste.short3, &robot.bott_sens.sens3);
00069                                 evaluate_distance(robot.sharpsWaste.short4, &robot.bott_sens.sens4);
00070                                 
00071                                 FSM_SIGNAL(MAIN, EV_DETECTED_BOTTLE);
00072                                 break;
00073                         }
00074                         
00075                 // parsing joy buttons
00076 
00077                         if(robot.joy.button1 == 1) innerTransporterStop();
00078                         if(robot.joy.button2 == 1) frontTransporterStop();
00079                         if(robot.joy.button3 == 1) frontDoorDown();
00080                         if(robot.joy.button4 == 1) frontTransporterBackward();
00081                         if(robot.joy.button5 == 1) frontDoorUp();
00082                         if(robot.joy.button6 == 1) frontTransporterForward();
00083                         if(robot.joy.button7 == 1) innerDoorDown();
00084                         if(robot.joy.button8 == 1) innerDoorUp();
00085                         if(robot.joy.button9 == 1) backDoorDown();
00086                         if(robot.joy.button10 == 1) backDoorUp();
00087                         if(robot.joy.button11 == 1) innerTransporterLeft();
00088                         if(robot.joy.button12 == 1) innerTransporterRight();
00089 
00090 #ifdef JOYSTICK_NAV                                             // if joystick is used for navigating
00091                         v = (double)robot.joy.axisY/32768;
00092                         omega = (double)robot.joy.axisX/32768;
00093                         lenght = sqrt( pow(v,2) + pow(omega,2));
00094                         if (lenght >= 1)                // If lenght si more than 1 is used unit circle
00095                         {
00096                                 angle = tan2(robot.joy.axisX/robot.joy.axisY);  // computing new speed and omega
00097                                 v = sin(angle);
00098                                 omega = cos(angle);
00099                         }
00100                         
00101                         speedL = (v - R*omega)*VMAX;
00102                         speedR = -(v + R*omega)*VMAX;
00103                         robot_send_speed(speedL, speedR);       // forcing speed command 
00104                         printf("v %f, omega %f\n",v, omega);//#endif
00105                         printf("length %f, angle %f\n",lenght, angle);//#endif
00106                         printf("axisX %d, axisY %d\n",robot.joy.axisX, robot.joy.axisY);//#endif
00107                         printf("sent speed: %f, %f\n",speedL, speedR);//#endif
00108 #endif
00109                         break;
00110                 default:
00111                         DBG_PRINT_EVENT("Unhandled event:");
00112         }       
00113         
00114 //      ROBOT_UNLOCK();
00115 }
00116 
00117 /*
00118                 // definovat nove ev na joy
00119         EV_JOY_B0
00120         EV_JOY_B1               
00121         EV_JOY_B2
00122         EV_JOY_B0
00123         EV_JOY_B4
00124         EV_JOY_B5
00125         EV_JOY_B6
00126         EV_JOY_B7
00127         EV_JOY_B8
00128         EV_JOY_B9
00129         EV_JOY_B10
00130         EV_JOY_B11
00131 
00132 
00133 
00134 */

Generated on Thu Sep 13 11:28:28 2007 for DCE-Eurobot by  doxygen 1.5.3