Robot's source code
Dependencies: mbed
main.cpp@52:96ccd958c80a, 2015-04-07 (annotated)
- Committer:
- Near32
- Date:
- Tue Apr 07 17:48:42 2015 +0000
- Revision:
- 52:96ccd958c80a
- Parent:
- 51:960ed3e8b648
- Child:
- 53:eca003fcc800
pid position : coefficient; todo coefficients...
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Jagang | 0:41149573d577 | 1 | #include "mbed.h" |
Jagang | 39:09c04fd42c94 | 2 | |
Jagang | 39:09c04fd42c94 | 3 | #include "defines.h" |
Jagang | 39:09c04fd42c94 | 4 | |
Jagang | 0:41149573d577 | 5 | #include "QEI.h" |
Jagang | 23:228317fe0457 | 6 | #include "Map.h" |
Jagang | 39:09c04fd42c94 | 7 | #include "AX12.h" |
Jagang | 0:41149573d577 | 8 | |
Near32 | 42:fcb48e2fc426 | 9 | |
Near32 | 42:fcb48e2fc426 | 10 | #include "Odometry.h" |
Near32 | 35:54b13e154801 | 11 | #include "Asserv.h" |
Near32 | 36:54f86bc6fd80 | 12 | #include "Motor.h" |
Near32 | 17:f360e21d3307 | 13 | /*----------------------------------------------------------------------------------------------*/ |
Jagang | 39:09c04fd42c94 | 14 | /*Serial*/ |
Near32 | 48:cb3ebbc27db3 | 15 | Serial logger(OUT_TX, OUT_RX); // tx, rx |
Near32 | 48:cb3ebbc27db3 | 16 | //Serial logger(USBTX,USBRX); |
Near32 | 48:cb3ebbc27db3 | 17 | //logger.baud((int)115200); |
Near32 | 17:f360e21d3307 | 18 | /*----------------------------------------------------------------------------------------------*/ |
Jagang | 21:5443f93819db | 19 | |
Jagang | 23:228317fe0457 | 20 | /* --- Initialisation de la liste des obstable --- */ |
Jagang | 23:228317fe0457 | 21 | int Obstacle::lastId = 0; |
Jagang | 23:228317fe0457 | 22 | |
Jagang | 0:41149573d577 | 23 | int main() |
Jagang | 0:41149573d577 | 24 | { |
Jagang | 39:09c04fd42c94 | 25 | logger.printf("Initialisation...\r\n"); |
Near32 | 24:3c0422e1ebd6 | 26 | //mbed |
Near32 | 24:3c0422e1ebd6 | 27 | /* |
Near32 | 17:f360e21d3307 | 28 | PwmOut pw1(p22); |
Near32 | 17:f360e21d3307 | 29 | DigitalOut dir1(p21); |
Near32 | 17:f360e21d3307 | 30 | PwmOut pw2(p24); |
Near32 | 17:f360e21d3307 | 31 | DigitalOut dir2(p23); |
Near32 | 24:3c0422e1ebd6 | 32 | */ |
Near32 | 17:f360e21d3307 | 33 | |
Near32 | 17:f360e21d3307 | 34 | //mbuino |
Near32 | 17:f360e21d3307 | 35 | /* |
Near32 | 17:f360e21d3307 | 36 | PwmOut pw1(P0_17); |
Near32 | 17:f360e21d3307 | 37 | DigitalOut dir1(P0_18); |
Near32 | 17:f360e21d3307 | 38 | PwmOut pw2(P0_23); |
Near32 | 17:f360e21d3307 | 39 | DigitalOut dir2(P0_19); |
Near32 | 17:f360e21d3307 | 40 | */ |
Near32 | 17:f360e21d3307 | 41 | /* |
Near32 | 17:f360e21d3307 | 42 | //nucleo |
Near32 | 17:f360e21d3307 | 43 | PwmOut pw1(PB_8); |
Near32 | 17:f360e21d3307 | 44 | DigitalOut dir1(D12); |
Near32 | 17:f360e21d3307 | 45 | PwmOut pw2(PB_9); |
Near32 | 17:f360e21d3307 | 46 | DigitalOut dir2(D13); |
Near32 | 17:f360e21d3307 | 47 | */ |
Near32 | 24:3c0422e1ebd6 | 48 | //nucleo |
Near32 | 36:54f86bc6fd80 | 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 | 39:09c04fd42c94 | 58 | logger.printf("mise a jour des pwm.\r\n"); |
Jagang | 39:09c04fd42c94 | 59 | Timer t; |
Jagang | 39:09c04fd42c94 | 60 | |
Jagang | 39:09c04fd42c94 | 61 | AX12 test(PA_9,NC,0x05,250000); |
Jagang | 39:09c04fd42c94 | 62 | AnalogIn ain0(PA_0); |
Jagang | 39:09c04fd42c94 | 63 | AnalogIn ain1(PA_1); |
Jagang | 39:09c04fd42c94 | 64 | AnalogIn ain2(PA_4); |
Jagang | 39:09c04fd42c94 | 65 | AnalogIn ain3(PB_0); |
Jagang | 39:09c04fd42c94 | 66 | AnalogIn ain4(PC_1); |
Near32 | 24:3c0422e1ebd6 | 67 | |
Near32 | 3:573a0dc8383f | 68 | /*----------------------------------------------------------------------------------------------*/ |
Near32 | 3:573a0dc8383f | 69 | /*Odometry*/ |
Near32 | 48:cb3ebbc27db3 | 70 | QEI qei_right(PB_3,PA_10,NC,1024*4,QEI::X4_ENCODING); |
Near32 | 48:cb3ebbc27db3 | 71 | QEI qei_left(PB_4,PB_5,NC,1024*4,QEI::X4_ENCODING); |
Jagang | 32:148147c0755e | 72 | Odometry odometry(&qei_left,&qei_right,63/2.,63/2.,280); |
Jagang | 37:41abbd7eaec1 | 73 | DigitalOut led(LED1); |
Jagang | 39:09c04fd42c94 | 74 | |
Jagang | 39:09c04fd42c94 | 75 | test.SetMode(0); // Position |
Jagang | 39:09c04fd42c94 | 76 | |
Jagang | 40:83ce8d1072ef | 77 | /*char dataOff[] = {0}; |
Jagang | 39:09c04fd42c94 | 78 | char dataOn[] = {1}; |
Jagang | 40:83ce8d1072ef | 79 | while(1) |
Jagang | 37:41abbd7eaec1 | 80 | { |
Jagang | 40:83ce8d1072ef | 81 | logger.printf("0: %f\r\n",ain0.read()*3.3); |
Jagang | 40:83ce8d1072ef | 82 | logger.printf("1: %f\r\n",ain1.read()*3.3); |
Jagang | 40:83ce8d1072ef | 83 | logger.printf("2: %f\r\n",ain2.read()*3.3); |
Jagang | 40:83ce8d1072ef | 84 | logger.printf("3: %f\r\n",ain3.read()*3.3); |
Jagang | 40:83ce8d1072ef | 85 | logger.printf("4: %f\r\n\r\n",ain4.read()*3.3); |
Jagang | 37:41abbd7eaec1 | 86 | led = !led; |
Jagang | 40:83ce8d1072ef | 87 | wait(1); |
Jagang | 40:83ce8d1072ef | 88 | motorR.setSpeed(1.0); |
Jagang | 40:83ce8d1072ef | 89 | wait(1); |
Jagang | 40:83ce8d1072ef | 90 | motorR.setSpeed(0.0); |
Jagang | 39:09c04fd42c94 | 91 | }*/ |
Near32 | 48:cb3ebbc27db3 | 92 | bool testOdo = false; |
Near32 | 36:54f86bc6fd80 | 93 | |
Near32 | 36:54f86bc6fd80 | 94 | |
Near32 | 35:54b13e154801 | 95 | Asserv<float> instanceAsserv(&odometry); |
Near32 | 3:573a0dc8383f | 96 | |
Near32 | 3:573a0dc8383f | 97 | /*----------------------------------------------------------------------------------------------*/ |
Near32 | 52:96ccd958c80a | 98 | instanceAsserv.setGoal( (float)100,(float)100, (float)0); |
Jagang | 40:83ce8d1072ef | 99 | |
Near32 | 47:4909e97570f6 | 100 | while(!testOdo) |
Near32 | 26:54e1afed58b2 | 101 | { |
Near32 | 36:54f86bc6fd80 | 102 | //Asservissement : |
Near32 | 41:c04c2ec37aad | 103 | |
Jagang | 40:83ce8d1072ef | 104 | t.start(); |
Near32 | 41:c04c2ec37aad | 105 | |
Near32 | 43:87bdce65341f | 106 | instanceAsserv.update((float)t.read()); |
Near32 | 43:87bdce65341f | 107 | Mat<float> X( instanceAsserv.getX() ); |
Near32 | 47:4909e97570f6 | 108 | float phi_r = (float)instanceAsserv.getPhiR(); |
Near32 | 47:4909e97570f6 | 109 | float phi_l = (float)instanceAsserv.getPhiL(); |
Near32 | 47:4909e97570f6 | 110 | float phi_max = (float)instanceAsserv.getPhiMax(); |
Near32 | 44:d5f95af61243 | 111 | //logger.printf(" x : %f ;\t y : %f ;\t theta : %f ;\t VD = %f ;\t W = %f \n\r",X.get(1,1),X.get(2,1),X.get(3,1), X.get(4,1), X.get(5,1) ); |
Near32 | 44:d5f95af61243 | 112 | //logger.printf(" x : %f ;\t y : %f ;\t theta : %f ",X.get(1,1),X.get(2,1),X.get(3,1) ); |
Near32 | 45:b7617d7cedce | 113 | |
Near32 | 45:b7617d7cedce | 114 | //logger.printf("PhiR = %f ; \t PhiL = %f ; \n\r",phi_r,phi_l);//X.get(4,1),X.get(5,1)); |
Near32 | 45:b7617d7cedce | 115 | |
Near32 | 44:d5f95af61243 | 116 | //transpose(X).afficherMblue(); |
Near32 | 44:d5f95af61243 | 117 | |
Near32 | 42:fcb48e2fc426 | 118 | |
Near32 | 36:54f86bc6fd80 | 119 | //pcs.printf("%f : %f : %f\n", (double)odometry.getX()*100,(double)odometry.getY()*100,(double)odometry.getTheta()*180/3.14); |
Near32 | 36:54f86bc6fd80 | 120 | //blue.printf("State : \n\r"); |
Near32 | 36:54f86bc6fd80 | 121 | //(instanceAsserv.getX()).afficherMblue(); |
Near32 | 36:54f86bc6fd80 | 122 | //blue.printf("Odometry : \n\r"); |
Near32 | 41:c04c2ec37aad | 123 | //z.afficherMblue(); |
Near32 | 42:fcb48e2fc426 | 124 | |
Near32 | 48:cb3ebbc27db3 | 125 | motorR.setSpeed(0.08+(phi_r/phi_max) ); |
Near32 | 48:cb3ebbc27db3 | 126 | motorL.setSpeed(0.06+(phi_l/phi_max) ); |
Jagang | 40:83ce8d1072ef | 127 | |
Near32 | 36:54f86bc6fd80 | 128 | //Timer Handling : |
Near32 | 45:b7617d7cedce | 129 | t.stop(); |
Near32 | 45:b7617d7cedce | 130 | //logger.printf("%f s ecoulee.\n\r", t.read()); |
Near32 | 26:54e1afed58b2 | 131 | t.reset(); |
Near32 | 36:54f86bc6fd80 | 132 | t.start(); |
Near32 | 26:54e1afed58b2 | 133 | } |
Near32 | 3:573a0dc8383f | 134 | |
Near32 | 26:54e1afed58b2 | 135 | |
Near32 | 48:cb3ebbc27db3 | 136 | int i=0; |
Near32 | 48:cb3ebbc27db3 | 137 | int nbrech = 100; |
Near32 | 48:cb3ebbc27db3 | 138 | float tableR[nbrech], tableL[nbrech]; |
Near32 | 48:cb3ebbc27db3 | 139 | |
Near32 | 48:cb3ebbc27db3 | 140 | motorR.setSpeed(0.0); |
Near32 | 48:cb3ebbc27db3 | 141 | motorL.setSpeed(0.0); |
Near32 | 48:cb3ebbc27db3 | 142 | while(i<nbrech) |
Near32 | 48:cb3ebbc27db3 | 143 | { |
Near32 | 48:cb3ebbc27db3 | 144 | tableR[i] = odometry.getPhiright(); |
Near32 | 48:cb3ebbc27db3 | 145 | tableL[i] = odometry.getPhileft(); |
Near32 | 48:cb3ebbc27db3 | 146 | motorR.setSpeed( ((float)i)/100); |
Near32 | 48:cb3ebbc27db3 | 147 | motorL.setSpeed( ((float)i)/100); |
Near32 | 48:cb3ebbc27db3 | 148 | logger.printf("vitesse = %f \r\n", (float)i/100); |
Near32 | 48:cb3ebbc27db3 | 149 | wait(0.5); |
Near32 | 48:cb3ebbc27db3 | 150 | i++; |
Near32 | 48:cb3ebbc27db3 | 151 | } |
Near32 | 48:cb3ebbc27db3 | 152 | |
Near32 | 48:cb3ebbc27db3 | 153 | i=0; |
Near32 | 26:54e1afed58b2 | 154 | while(1) |
Near32 | 48:cb3ebbc27db3 | 155 | { |
Near32 | 48:cb3ebbc27db3 | 156 | motorR.setSpeed(0.0); |
Near32 | 48:cb3ebbc27db3 | 157 | motorL.setSpeed(0.0); |
Near32 | 48:cb3ebbc27db3 | 158 | |
Near32 | 48:cb3ebbc27db3 | 159 | if(i<nbrech) |
Near32 | 48:cb3ebbc27db3 | 160 | { |
Near32 | 48:cb3ebbc27db3 | 161 | logger.printf("%f , %f \r\n", tableL[i],tableR[i]); |
Near32 | 48:cb3ebbc27db3 | 162 | wait(1); |
Near32 | 48:cb3ebbc27db3 | 163 | i++; |
Near32 | 48:cb3ebbc27db3 | 164 | } |
Jagang | 0:41149573d577 | 165 | } |
Near32 | 43:87bdce65341f | 166 | } |