.

Dependencies:   mbed

Committer:
gerardo_carmona
Date:
Sat Oct 11 00:56:33 2014 +0000
Revision:
0:c9d69c701daf
Prueba puente H

Who changed what in which revision?

UserRevisionLine numberNew contents of line
gerardo_carmona 0:c9d69c701daf 1 // For the KL25Z
gerardo_carmona 0:c9d69c701daf 2 #include "mbed.h"
gerardo_carmona 0:c9d69c701daf 3
gerardo_carmona 0:c9d69c701daf 4 #define MOVE_FWD 'w'
gerardo_carmona 0:c9d69c701daf 5 #define MOVE_REV 's'
gerardo_carmona 0:c9d69c701daf 6 #define MOVE_LEF 'a'
gerardo_carmona 0:c9d69c701daf 7 #define MOVE_RIG 'd'
gerardo_carmona 0:c9d69c701daf 8 #define MOVE_STO 'q'
gerardo_carmona 0:c9d69c701daf 9
gerardo_carmona 0:c9d69c701daf 10 DigitalOut myled(LED2);
gerardo_carmona 0:c9d69c701daf 11 DigitalOut M1DIR(D7);
gerardo_carmona 0:c9d69c701daf 12 DigitalOut M2DIR(D8);
gerardo_carmona 0:c9d69c701daf 13 PwmOut M1PWM(D9);
gerardo_carmona 0:c9d69c701daf 14 PwmOut M2PWM(D10);
gerardo_carmona 0:c9d69c701daf 15 DigitalOut M_ON(D2);
gerardo_carmona 0:c9d69c701daf 16 Serial bt(D14, D15);
gerardo_carmona 0:c9d69c701daf 17 Serial pc(USBTX, USBRX);
gerardo_carmona 0:c9d69c701daf 18
gerardo_carmona 0:c9d69c701daf 19 bool move_motors(char c, int _power);
gerardo_carmona 0:c9d69c701daf 20 void motor_fwd(int _power);
gerardo_carmona 0:c9d69c701daf 21 void motor_rev(int _power);
gerardo_carmona 0:c9d69c701daf 22 void motor_left(int _power);
gerardo_carmona 0:c9d69c701daf 23 void motor_right(int _power);
gerardo_carmona 0:c9d69c701daf 24 void motor_stop();
gerardo_carmona 0:c9d69c701daf 25
gerardo_carmona 0:c9d69c701daf 26
gerardo_carmona 0:c9d69c701daf 27 int main() {
gerardo_carmona 0:c9d69c701daf 28 myled = 0;
gerardo_carmona 0:c9d69c701daf 29 wait(3);
gerardo_carmona 0:c9d69c701daf 30 bt.baud(9600);
gerardo_carmona 0:c9d69c701daf 31 M_ON = 1;
gerardo_carmona 0:c9d69c701daf 32 while(1) {
gerardo_carmona 0:c9d69c701daf 33 if (bt.readable()){
gerardo_carmona 0:c9d69c701daf 34 char c = bt.getc();
gerardo_carmona 0:c9d69c701daf 35 bt.printf("%c\n\r", c);
gerardo_carmona 0:c9d69c701daf 36 move_motors(c, 100);
gerardo_carmona 0:c9d69c701daf 37 }
gerardo_carmona 0:c9d69c701daf 38 }
gerardo_carmona 0:c9d69c701daf 39 }
gerardo_carmona 0:c9d69c701daf 40
gerardo_carmona 0:c9d69c701daf 41 bool move_motors(char c, int _power){
gerardo_carmona 0:c9d69c701daf 42 switch (c){
gerardo_carmona 0:c9d69c701daf 43 case MOVE_FWD:
gerardo_carmona 0:c9d69c701daf 44 motor_fwd(_power);
gerardo_carmona 0:c9d69c701daf 45 break;
gerardo_carmona 0:c9d69c701daf 46 case MOVE_REV:
gerardo_carmona 0:c9d69c701daf 47 motor_rev(_power);
gerardo_carmona 0:c9d69c701daf 48 break;
gerardo_carmona 0:c9d69c701daf 49 case MOVE_LEF:
gerardo_carmona 0:c9d69c701daf 50 motor_left(_power);
gerardo_carmona 0:c9d69c701daf 51 break;
gerardo_carmona 0:c9d69c701daf 52 case MOVE_RIG:
gerardo_carmona 0:c9d69c701daf 53 motor_right(_power);
gerardo_carmona 0:c9d69c701daf 54 break;
gerardo_carmona 0:c9d69c701daf 55 case MOVE_STO:
gerardo_carmona 0:c9d69c701daf 56 motor_stop();
gerardo_carmona 0:c9d69c701daf 57 break;
gerardo_carmona 0:c9d69c701daf 58 default:
gerardo_carmona 0:c9d69c701daf 59 motor_stop();
gerardo_carmona 0:c9d69c701daf 60 //bt.printf("%f\r\n", get_mag_angle());
gerardo_carmona 0:c9d69c701daf 61 }
gerardo_carmona 0:c9d69c701daf 62 return 1;
gerardo_carmona 0:c9d69c701daf 63 }
gerardo_carmona 0:c9d69c701daf 64
gerardo_carmona 0:c9d69c701daf 65 void motor_fwd(int _power){
gerardo_carmona 0:c9d69c701daf 66 M1DIR = 1;
gerardo_carmona 0:c9d69c701daf 67 M1PWM = 1;
gerardo_carmona 0:c9d69c701daf 68 M2DIR = 1;
gerardo_carmona 0:c9d69c701daf 69 M2PWM = 1;
gerardo_carmona 0:c9d69c701daf 70 pc.printf("FWD\n");
gerardo_carmona 0:c9d69c701daf 71 }
gerardo_carmona 0:c9d69c701daf 72
gerardo_carmona 0:c9d69c701daf 73 void motor_rev(int _power){
gerardo_carmona 0:c9d69c701daf 74 M1DIR = 0;
gerardo_carmona 0:c9d69c701daf 75 M1PWM = 1;
gerardo_carmona 0:c9d69c701daf 76 M2DIR = 0;
gerardo_carmona 0:c9d69c701daf 77 M2PWM = 1;
gerardo_carmona 0:c9d69c701daf 78 pc.printf("REV\n");
gerardo_carmona 0:c9d69c701daf 79 }
gerardo_carmona 0:c9d69c701daf 80
gerardo_carmona 0:c9d69c701daf 81 void motor_left(int _power){
gerardo_carmona 0:c9d69c701daf 82 M1DIR = 0;
gerardo_carmona 0:c9d69c701daf 83 M1PWM = 1;
gerardo_carmona 0:c9d69c701daf 84 M2DIR = 1;
gerardo_carmona 0:c9d69c701daf 85 M2PWM = 1;
gerardo_carmona 0:c9d69c701daf 86 pc.printf("LEFT\n");
gerardo_carmona 0:c9d69c701daf 87 }
gerardo_carmona 0:c9d69c701daf 88
gerardo_carmona 0:c9d69c701daf 89 void motor_right(int _power){
gerardo_carmona 0:c9d69c701daf 90 M1DIR = 1;
gerardo_carmona 0:c9d69c701daf 91 M1PWM = 1;
gerardo_carmona 0:c9d69c701daf 92 M2DIR = 0;
gerardo_carmona 0:c9d69c701daf 93 M2PWM = 1;
gerardo_carmona 0:c9d69c701daf 94 pc.printf("RIGHT\n");
gerardo_carmona 0:c9d69c701daf 95 }
gerardo_carmona 0:c9d69c701daf 96
gerardo_carmona 0:c9d69c701daf 97 void motor_stop(){
gerardo_carmona 0:c9d69c701daf 98 M1DIR = 1;
gerardo_carmona 0:c9d69c701daf 99 M1PWM = 0;
gerardo_carmona 0:c9d69c701daf 100 M2DIR = 1;
gerardo_carmona 0:c9d69c701daf 101 M2PWM = 0;
gerardo_carmona 0:c9d69c701daf 102 pc.printf("STOP\n");
gerardo_carmona 0:c9d69c701daf 103 }