Robot's source code

Dependencies:   mbed

Committer:
Near32
Date:
Sat Apr 18 08:40:24 2015 +0000
Revision:
88:77d6ced9977e
Parent:
87:e8b64b4174b8
Child:
89:d05001d85a02
pd regulator

Who changed what in which revision?

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