kevin eccli
/
Freescale
MAJ BROSSARD
main.cpp@3:8f2c0c324296, 2014-10-22 (annotated)
- Committer:
- singularity
- Date:
- Wed Oct 22 23:26:41 2014 +0000
- Revision:
- 3:8f2c0c324296
- Parent:
- 0:d837a2e3622c
tentative convert sint16 -> char
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
singularity | 0:d837a2e3622c | 1 | #include "mbed.h" |
singularity | 0:d837a2e3622c | 2 | |
singularity | 0:d837a2e3622c | 3 | DigitalOut gpo(D0); |
singularity | 0:d837a2e3622c | 4 | DigitalOut led(LED_RED); |
singularity | 0:d837a2e3622c | 5 | Serial xbee(D1,D0); |
singularity | 0:d837a2e3622c | 6 | AnalogIn FBA(PTB10); //Feedback moteur A |
singularity | 0:d837a2e3622c | 7 | AnalogIn FBB(PTB11); //Feedback moteur B |
singularity | 0:d837a2e3622c | 8 | PwmOut moteurA1(PTD1); |
singularity | 0:d837a2e3622c | 9 | PwmOut moteurA2(PTD3); |
singularity | 0:d837a2e3622c | 10 | PwmOut moteurB1(PTD2); |
singularity | 0:d837a2e3622c | 11 | PwmOut moteurB2(PTD0); |
singularity | 0:d837a2e3622c | 12 | PwmOut direction(PTC4); |
singularity | 0:d837a2e3622c | 13 | char commande[10]; |
singularity | 0:d837a2e3622c | 14 | int curseur; |
singularity | 0:d837a2e3622c | 15 | |
singularity | 0:d837a2e3622c | 16 | |
singularity | 0:d837a2e3622c | 17 | |
singularity | 0:d837a2e3622c | 18 | void setDirection(float dir) { |
singularity | 0:d837a2e3622c | 19 | direction.write(dir); |
singularity | 0:d837a2e3622c | 20 | } |
singularity | 0:d837a2e3622c | 21 | |
singularity | 0:d837a2e3622c | 22 | |
singularity | 0:d837a2e3622c | 23 | void setMoteurs(float m1, float m2) { |
singularity | 0:d837a2e3622c | 24 | if (m1>0) { |
singularity | 0:d837a2e3622c | 25 | moteurA2.write(0); |
singularity | 0:d837a2e3622c | 26 | moteurA1.write(m1); |
singularity | 0:d837a2e3622c | 27 | } |
singularity | 0:d837a2e3622c | 28 | else { |
singularity | 0:d837a2e3622c | 29 | moteurA1.write(0); |
singularity | 0:d837a2e3622c | 30 | moteurA2.write(-m1); |
singularity | 0:d837a2e3622c | 31 | } |
singularity | 0:d837a2e3622c | 32 | |
singularity | 0:d837a2e3622c | 33 | if (m2>0) { |
singularity | 0:d837a2e3622c | 34 | moteurB2.write(0); |
singularity | 0:d837a2e3622c | 35 | moteurB1.write(m2); |
singularity | 0:d837a2e3622c | 36 | } |
singularity | 0:d837a2e3622c | 37 | else { |
singularity | 0:d837a2e3622c | 38 | moteurB1.write(0); |
singularity | 0:d837a2e3622c | 39 | moteurB2.write(-m2); |
singularity | 0:d837a2e3622c | 40 | } |
singularity | 0:d837a2e3622c | 41 | } |
singularity | 0:d837a2e3622c | 42 | |
singularity | 0:d837a2e3622c | 43 | |
singularity | 0:d837a2e3622c | 44 | |
singularity | 0:d837a2e3622c | 45 | void tourneDroite(int angle, PwmOut s) { |
singularity | 0:d837a2e3622c | 46 | s.write(s.read() + valeurServo(angle)); |
singularity | 0:d837a2e3622c | 47 | } |
singularity | 0:d837a2e3622c | 48 | |
singularity | 0:d837a2e3622c | 49 | int pourcentServo(float v) { |
singularity | 0:d837a2e3622c | 50 | return v*2000; |
singularity | 0:d837a2e3622c | 51 | } |
singularity | 0:d837a2e3622c | 52 | |
singularity | 0:d837a2e3622c | 53 | float valeurServo(int p) { |
singularity | 0:d837a2e3622c | 54 | return (float)p/2000; |
singularity | 0:d837a2e3622c | 55 | } |
singularity | 0:d837a2e3622c | 56 | |
singularity | 0:d837a2e3622c | 57 | |
singularity | 0:d837a2e3622c | 58 | |
singularity | 0:d837a2e3622c | 59 | void commande(int dir, int m1, int m2) { |
singularity | 0:d837a2e3622c | 60 | setDirection((float)dir-128.0f)/128.0f); |
singularity | 0:d837a2e3622c | 61 | setMoteurs((m-128.0f)/128.0f,(m2-128.0f)/128.0f); |
singularity | 0:d837a2e3622c | 62 | } |
singularity | 0:d837a2e3622c | 63 | |
singularity | 0:d837a2e3622c | 64 | |
singularity | 0:d837a2e3622c | 65 | |
singularity | 0:d837a2e3622c | 66 | /* |
singularity | 0:d837a2e3622c | 67 | void getVar(int idVar) { |
singularity | 0:d837a2e3622c | 68 | switch(idVar) { |
singularity | 0:d837a2e3622c | 69 | case |
singularity | 0:d837a2e3622c | 70 | } |
singularity | 0:d837a2e3622c | 71 | } |
singularity | 0:d837a2e3622c | 72 | */ |
singularity | 0:d837a2e3622c | 73 | |
singularity | 0:d837a2e3622c | 74 | void traiter(char commande[]) { |
singularity | 0:d837a2e3622c | 75 | switch(commande[0]) { |
singularity | 0:d837a2e3622c | 76 | /*case 'S': |
singularity | 0:d837a2e3622c | 77 | setVar(commande[1]); |
singularity | 0:d837a2e3622c | 78 | case 'G': |
singularity | 0:d837a2e3622c | 79 | xbee.printf(getVar(commande[1])); |
singularity | 0:d837a2e3622c | 80 | */ |
singularity | 0:d837a2e3622c | 81 | case 'C': |
singularity | 0:d837a2e3622c | 82 | commande(commande[1],commande[2],commande[3]); |
singularity | 0:d837a2e3622c | 83 | |
singularity | 0:d837a2e3622c | 84 | default: |
singularity | 0:d837a2e3622c | 85 | xbee.printf("Commande non valide"); |
singularity | 0:d837a2e3622c | 86 | } |
singularity | 0:d837a2e3622c | 87 | } |
singularity | 0:d837a2e3622c | 88 | |
singularity | 0:d837a2e3622c | 89 | void callback() { |
singularity | 0:d837a2e3622c | 90 | char donne = xbee.getc(); |
singularity | 0:d837a2e3622c | 91 | |
singularity | 0:d837a2e3622c | 92 | if(donne == '\n') { |
singularity | 0:d837a2e3622c | 93 | if(curseur!=0) { |
singularity | 0:d837a2e3622c | 94 | commande[curseur] = '\n'; |
singularity | 0:d837a2e3622c | 95 | traiter(commande); |
singularity | 0:d837a2e3622c | 96 | curseur = 0; |
singularity | 0:d837a2e3622c | 97 | } |
singularity | 0:d837a2e3622c | 98 | } else { |
singularity | 0:d837a2e3622c | 99 | commande[curseur] = donne; |
singularity | 0:d837a2e3622c | 100 | curseur++; |
singularity | 0:d837a2e3622c | 101 | } |
singularity | 0:d837a2e3622c | 102 | } |
singularity | 0:d837a2e3622c | 103 | |
singularity | 0:d837a2e3622c | 104 | void init() { |
singularity | 0:d837a2e3622c | 105 | curseur = 0; |
singularity | 0:d837a2e3622c | 106 | for(int i=0:i<10;i++) { |
singularity | 0:d837a2e3622c | 107 | commande[i] = '\n'; |
singularity | 0:d837a2e3622c | 108 | } |
singularity | 0:d837a2e3622c | 109 | moteurA1.period(0.0001f); |
singularity | 0:d837a2e3622c | 110 | moteurA2.period(0.0001f); |
singularity | 0:d837a2e3622c | 111 | moteurB1.period(0.0001f); |
singularity | 0:d837a2e3622c | 112 | moteurA2.period(0.0001f); |
singularity | 0:d837a2e3622c | 113 | direction.period(0.014286f); |
singularity | 0:d837a2e3622c | 114 | } |
singularity | 0:d837a2e3622c | 115 | |
singularity | 0:d837a2e3622c | 116 | int main() |
singularity | 0:d837a2e3622c | 117 | { |
singularity | 0:d837a2e3622c | 118 | init(); |
singularity | 0:d837a2e3622c | 119 | xbee.baud(9600); |
singularity | 0:d837a2e3622c | 120 | xbee.printf("Xbee Freescale: Hello...\n"); |
singularity | 0:d837a2e3622c | 121 | xbee.attach(&callback); |
singularity | 0:d837a2e3622c | 122 | |
singularity | 0:d837a2e3622c | 123 | while (true) { |
singularity | 0:d837a2e3622c | 124 | gpo = !gpo; // toggle pin |
singularity | 0:d837a2e3622c | 125 | led = !led; // toggle led |
singularity | 0:d837a2e3622c | 126 | wait(0.2f); |
singularity | 0:d837a2e3622c | 127 | } |
singularity | 0:d837a2e3622c | 128 | } |