Robot's source code

Dependencies:   mbed

Committer:
sype
Date:
Sat Apr 18 09:39:15 2015 +0000
Revision:
85:8e95432d99d3
Parent:
84:24d727006218
Child:
92:b403f2724252
Child:
94:5c37bcf73d14
Mise en place de l'asserv de l'ETAT 2

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Jagang 0:41149573d577 1 #include "mbed.h"
Jagang 39:09c04fd42c94 2
sype 80:5399183aa39b 3 #define PLAN_B
Jagang 74:88be86f83d17 4
sype 84:24d727006218 5 //#define OUT_USB
Jagang 39:09c04fd42c94 6 #include "defines.h"
Jagang 39:09c04fd42c94 7
Jagang 0:41149573d577 8 #include "QEI.h"
Jagang 23:228317fe0457 9 #include "Map.h"
Jagang 39:09c04fd42c94 10 #include "AX12.h"
Jagang 0:41149573d577 11
Jagang 71:95d76c181b22 12 #ifdef PLAN_A
Jagang 71:95d76c181b22 13 #include "Odometry.h"
Jagang 71:95d76c181b22 14 #include "Asserv.h"
Jagang 71:95d76c181b22 15 #else
Jagang 71:95d76c181b22 16 #include "Odometry2.h"
Jagang 71:95d76c181b22 17 #include "planB.h"
Jagang 71:95d76c181b22 18 #endif
Jagang 71:95d76c181b22 19
Near32 36:54f86bc6fd80 20 #include "Motor.h"
Near32 17:f360e21d3307 21 /*----------------------------------------------------------------------------------------------*/
Jagang 39:09c04fd42c94 22 /*Serial*/
sype 80:5399183aa39b 23
sype 84:24d727006218 24 //Serial logger(OUT_TX, OUT_RX); // tx, rx
sype 84:24d727006218 25 Serial logger(USBTX,USBRX);
Near32 48:cb3ebbc27db3 26 //logger.baud((int)115200);
Near32 17:f360e21d3307 27 /*----------------------------------------------------------------------------------------------*/
Jagang 21:5443f93819db 28
Jagang 23:228317fe0457 29 /* --- Initialisation de la liste des obstable --- */
Jagang 23:228317fe0457 30 int Obstacle::lastId = 0;
Jagang 23:228317fe0457 31
Jagang 0:41149573d577 32 int main()
Jagang 0:41149573d577 33 {
Jagang 39:09c04fd42c94 34 logger.printf("Initialisation...\r\n");
Near32 36:54f86bc6fd80 35
Jagang 78:7c7cefbe1772 36 AX12 test(AX12_TX,AX12_RX,5,250000);
Jagang 74:88be86f83d17 37 test.setMode(0);
Jagang 74:88be86f83d17 38
sype 84:24d727006218 39 /*while(1)
Jagang 74:88be86f83d17 40 {
Jagang 74:88be86f83d17 41 test.setMaxTorque(0x1FF);
Jagang 74:88be86f83d17 42 test.setGoal(0);
Jagang 74:88be86f83d17 43 logger.printf("0\r\n");
Jagang 74:88be86f83d17 44 wait(2);
Jagang 74:88be86f83d17 45 test.setMaxTorque(0x1FF);
Jagang 74:88be86f83d17 46 test.setGoal(90);
Jagang 74:88be86f83d17 47 logger.printf("180\r\n");
Jagang 74:88be86f83d17 48 wait(4);
sype 84:24d727006218 49 }*/
Jagang 74:88be86f83d17 50
Jagang 40:83ce8d1072ef 51 PwmOut pw1(PWM_MOT1);
Jagang 40:83ce8d1072ef 52 DigitalOut dir1(DIR_MOT1);
Jagang 40:83ce8d1072ef 53 PwmOut pw2(PWM_MOT2);
Jagang 40:83ce8d1072ef 54 DigitalOut dir2(DIR_MOT2);
Near32 17:f360e21d3307 55
Near32 36:54f86bc6fd80 56
Near32 48:cb3ebbc27db3 57 Motor motorL(PWM_MOT1,DIR_MOT1);
Near32 48:cb3ebbc27db3 58 Motor motorR(PWM_MOT2,DIR_MOT2);
Jagang 57:ab13f4e7a2b2 59
Jagang 39:09c04fd42c94 60 Timer t;
Jagang 39:09c04fd42c94 61
Jagang 63:fd9af0693e50 62 //AX12 test(PA_9,NC,0x03,250000);
Jagang 54:e0e58c36658a 63
Jagang 39:09c04fd42c94 64 AnalogIn ain0(PA_0);
Jagang 39:09c04fd42c94 65 AnalogIn ain1(PA_1);
Jagang 39:09c04fd42c94 66 AnalogIn ain2(PA_4);
Jagang 39:09c04fd42c94 67 AnalogIn ain3(PB_0);
Jagang 39:09c04fd42c94 68 AnalogIn ain4(PC_1);
Near32 24:3c0422e1ebd6 69
Near32 3:573a0dc8383f 70 /*----------------------------------------------------------------------------------------------*/
Near32 3:573a0dc8383f 71 /*Odometry*/
Jagang 71:95d76c181b22 72 #ifdef PLAN_A
Jagang 71:95d76c181b22 73 QEI qei_right(PB_3,PA_10,NC,1024*4,QEI::X4_ENCODING);
Jagang 71:95d76c181b22 74 QEI qei_left(PB_4,PB_5,NC,1024*4,QEI::X4_ENCODING);
Jagang 71:95d76c181b22 75
sype 79:d97090bb6470 76 Odometry odometry(&qei_left,&qei_right,63/2.,63/2.,255);
Jagang 78:7c7cefbe1772 77 odometry.setTheta(0);
Jagang 78:7c7cefbe1772 78 odometry.setX(0);
Jagang 78:7c7cefbe1772 79 odometry.setY(0);
Jagang 71:95d76c181b22 80 #else
Jagang 71:95d76c181b22 81 QEI qei_left(ODO_G_A,ODO_G_B,NC,1024*4,QEI::X4_ENCODING);
Jagang 71:95d76c181b22 82 QEI qei_right(ODO_D_A,ODO_D_B,NC,1024*4,QEI::X4_ENCODING);
Jagang 71:95d76c181b22 83
sype 79:d97090bb6470 84 Odometry2 odometry(&qei_left,&qei_right,63/2.,63/2.,255);
Jagang 71:95d76c181b22 85
Jagang 71:95d76c181b22 86 odometry.setTheta(PI/2);
Jagang 71:95d76c181b22 87 odometry.setX(0);
Jagang 71:95d76c181b22 88 odometry.setY(0);
Jagang 71:95d76c181b22 89 #endif
Jagang 60:546d7b43333b 90
Jagang 63:fd9af0693e50 91 DigitalOut led1(LED1);
Jagang 63:fd9af0693e50 92 DigitalOut led2(LED2);
Jagang 63:fd9af0693e50 93 DigitalOut led3(LED3);
Jagang 39:09c04fd42c94 94
Jagang 71:95d76c181b22 95 bool testOdo = false;
Jagang 71:95d76c181b22 96
Jagang 71:95d76c181b22 97 #ifdef PLAN_A
Jagang 71:95d76c181b22 98 Asserv<float> instanceAsserv(&odometry);
Near32 81:08eacb6179d8 99 instanceAsserv.setGoal((float)0.0,(float)0.0,(float)PI/2);
Jagang 71:95d76c181b22 100
Jagang 74:88be86f83d17 101 char state = 0;
Jagang 74:88be86f83d17 102
Jagang 71:95d76c181b22 103 t.start();
Jagang 63:fd9af0693e50 104 t.reset();
Jagang 63:fd9af0693e50 105
Jagang 71:95d76c181b22 106 while(!testOdo)
Jagang 71:95d76c181b22 107 {
Jagang 71:95d76c181b22 108 float dt = t.read();
Jagang 71:95d76c181b22 109 t.reset();
Jagang 74:88be86f83d17 110
Jagang 71:95d76c181b22 111 odometry.update(dt);
Jagang 74:88be86f83d17 112 instanceAsserv.update(dt);
Near32 73:d8e1b543fbe3 113 float phi_r = (float)instanceAsserv.getPhiR();
Near32 73:d8e1b543fbe3 114 float phi_l = (float)instanceAsserv.getPhiL();
Jagang 74:88be86f83d17 115 float phi_max = (float)instanceAsserv.getPhiMax();
Jagang 71:95d76c181b22 116
Jagang 74:88be86f83d17 117 motorR.setSpeed(0.08+(phi_r/phi_max));
Jagang 74:88be86f83d17 118 motorL.setSpeed(0.06+(phi_l/phi_max));
Near32 81:08eacb6179d8 119 #ifdef test
Jagang 76:a0c09fd62be1 120 if(state == 0 && instanceAsserv.isArrivedRho())
Jagang 76:a0c09fd62be1 121 {
Jagang 76:a0c09fd62be1 122 state = 1;
Jagang 76:a0c09fd62be1 123 logger.printf("Arrive en 0,0 !!!!!");
Jagang 76:a0c09fd62be1 124 wait(5);
Jagang 76:a0c09fd62be1 125 instanceAsserv.setGoal(200.0,200.0,0);
Jagang 76:a0c09fd62be1 126 }
Jagang 76:a0c09fd62be1 127 else if(state == 1 && instanceAsserv.isArrivedRho())
Jagang 76:a0c09fd62be1 128 {
Jagang 76:a0c09fd62be1 129 state = 2;
Jagang 76:a0c09fd62be1 130 logger.printf("Arrive en 200,200 !!!!!");
Jagang 76:a0c09fd62be1 131 wait(5);
Jagang 76:a0c09fd62be1 132 instanceAsserv.setGoal(0.0,200.0,0);
Jagang 76:a0c09fd62be1 133 }
Jagang 76:a0c09fd62be1 134 else if(state == 2 && instanceAsserv.isArrivedRho())
Jagang 76:a0c09fd62be1 135 {
Jagang 76:a0c09fd62be1 136 state = 0;
Jagang 76:a0c09fd62be1 137 logger.printf("Arrive en 0,200 !!!!!");
Jagang 76:a0c09fd62be1 138 wait(5);
Jagang 76:a0c09fd62be1 139 instanceAsserv.setGoal(0.0,0.0,0);
Jagang 76:a0c09fd62be1 140 }
Near32 81:08eacb6179d8 141 #endif
Jagang 71:95d76c181b22 142 }
Jagang 71:95d76c181b22 143 #else
Jagang 71:95d76c181b22 144 aserv_planB asserv(odometry,motorL,motorR);
sype 85:8e95432d99d3 145 asserv.setGoal(45,45,0);
Jagang 71:95d76c181b22 146
Jagang 71:95d76c181b22 147 t.start();
Jagang 63:fd9af0693e50 148 t.reset();
Jagang 71:95d76c181b22 149
Jagang 71:95d76c181b22 150 while(!testOdo)
Jagang 71:95d76c181b22 151 {
Jagang 71:95d76c181b22 152 //Parametrage des coef par bluetooth
sype 84:24d727006218 153 /*while(logger.readable())
sype 84:24d727006218 154 {
Jagang 71:95d76c181b22 155 char c = logger.getc();
sype 79:d97090bb6470 156 if(c=='a') asserv.Kd += 0.001;
sype 79:d97090bb6470 157 else if(c=='z') asserv.Kd -= 0.001;
sype 79:d97090bb6470 158 logger.printf("%f\n\r",asserv.Kd);
sype 84:24d727006218 159 }*/
Jagang 71:95d76c181b22 160
Jagang 71:95d76c181b22 161 //Asservissement :
Jagang 71:95d76c181b22 162 float dt = t.read();
Jagang 71:95d76c181b22 163 t.reset();
Jagang 71:95d76c181b22 164 odometry.update(dt);
Jagang 71:95d76c181b22 165 asserv.update(dt);
sype 84:24d727006218 166 wait_ms(10);
Jagang 71:95d76c181b22 167 }
Jagang 71:95d76c181b22 168 #endif
Near32 43:87bdce65341f 169 }