controllo velocita motore con tasto user

Dependencies:   ArduinoMotorShield mbed

Fork of motore2 by Rapid PrototypingST32Nucleo

Committer:
mikteam10
Date:
Sat Oct 22 09:20:24 2016 +0000
Revision:
1:b259a13421b6
Parent:
0:a3a22a35e75c
motori A e B che girano a velocit? inversamente proporzionali

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mikteam10 0:a3a22a35e75c 1 #include "mbed.h"
mikteam10 0:a3a22a35e75c 2 #include "ArduinoMotorShield.hpp"
mikteam10 0:a3a22a35e75c 3
mikteam10 1:b259a13421b6 4 Ticker timer;
mikteam10 0:a3a22a35e75c 5 ArduinoMotorShield motore;
mikteam10 0:a3a22a35e75c 6 DigitalIn mybutton(USER_BUTTON);
mikteam10 1:b259a13421b6 7 //DigitalOut myled(LED1);
mikteam10 1:b259a13421b6 8 //Timer timer;
mikteam10 1:b259a13421b6 9 Serial pc(USBTX, USBRX); // tx, rx
mikteam10 1:b259a13421b6 10 PwmOut led(LED1);
mikteam10 1:b259a13421b6 11
mikteam10 1:b259a13421b6 12 float speed2 =-1.0; // velocita di start del motore in forward
mikteam10 1:b259a13421b6 13 float speed =-0.1; //velocita del motore in backward
mikteam10 1:b259a13421b6 14 float velocita = -0.15; // variabile velocita da incrementare o decrementare da tastiera per il motore A
mikteam10 1:b259a13421b6 15 float velocita2 = -0.95; //variabile velocita da incrementare o decrementare da tastiera per il motore A
mikteam10 1:b259a13421b6 16 float brightness = 0.0; //variabile per aumentare o decrementare la luce del led1
mikteam10 1:b259a13421b6 17 bool brake;
mikteam10 0:a3a22a35e75c 18
mikteam10 1:b259a13421b6 19
mikteam10 1:b259a13421b6 20 void attime(){
mikteam10 1:b259a13421b6 21 pc.printf("C1= %f \r\n", (motore.GetMotorCurrent( ArduinoMotorShield::MOTOR_A)));
mikteam10 1:b259a13421b6 22 pc.printf("C2= %f \r\n", (motore.GetMotorCurrent( ArduinoMotorShield::MOTOR_B)));
mikteam10 1:b259a13421b6 23 }
mikteam10 1:b259a13421b6 24
mikteam10 1:b259a13421b6 25
mikteam10 0:a3a22a35e75c 26 int main() {
mikteam10 1:b259a13421b6 27 pc.printf("Premere un tasto per accendere i motori \n\r ");
mikteam10 1:b259a13421b6 28 pc.printf("Premere 'd' per accelerare il motore A e decelerare il motore B \n\r");
mikteam10 1:b259a13421b6 29 pc.printf("Premere 'u' per accelerare il motore B e decelerare il motore A \n\r");
mikteam10 1:b259a13421b6 30
mikteam10 1:b259a13421b6 31 brake= false;
mikteam10 1:b259a13421b6 32 char s='\0';
mikteam10 1:b259a13421b6 33 char c='\0';
mikteam10 1:b259a13421b6 34 char p='\0';
mikteam10 1:b259a13421b6 35 //while(s != 's')
mikteam10 1:b259a13421b6 36 s = pc.getc();
mikteam10 1:b259a13421b6 37
mikteam10 1:b259a13421b6 38 motore.SetMotorPower( ArduinoMotorShield::MOTOR_A, speed);
mikteam10 1:b259a13421b6 39 motore.SetMotorPower( ArduinoMotorShield::MOTOR_B, speed2);
mikteam10 1:b259a13421b6 40 timer.attach(&attime, 0.5);
mikteam10 0:a3a22a35e75c 41
mikteam10 1:b259a13421b6 42 while(1) {
mikteam10 1:b259a13421b6 43 c = pc.getc();
mikteam10 1:b259a13421b6 44
mikteam10 1:b259a13421b6 45 if((c == 'u') && (velocita < 1.0)) {
mikteam10 1:b259a13421b6 46 brightness += 0.05;
mikteam10 1:b259a13421b6 47 velocita += 0.05;
mikteam10 1:b259a13421b6 48 velocita2 -= 0.05;
mikteam10 1:b259a13421b6 49 led = brightness;
mikteam10 0:a3a22a35e75c 50 motore.SetMotorPower( ArduinoMotorShield::MOTOR_A, velocita) ;
mikteam10 1:b259a13421b6 51 motore.SetMotorPower( ArduinoMotorShield::MOTOR_B, velocita2) ;
mikteam10 1:b259a13421b6 52 pc.printf("\rV= %f\n\r" , velocita);
mikteam10 1:b259a13421b6 53 pc.printf("\rV2= %f\n\r" , velocita2);
mikteam10 1:b259a13421b6 54
mikteam10 1:b259a13421b6 55
mikteam10 1:b259a13421b6 56 }
mikteam10 1:b259a13421b6 57 if((c == 'd') && (velocita>-1.0)){
mikteam10 1:b259a13421b6 58 velocita -= 0.05;
mikteam10 1:b259a13421b6 59 velocita2 += 0.05;
mikteam10 1:b259a13421b6 60 brightness -= 0.05;
mikteam10 1:b259a13421b6 61 led = brightness;
mikteam10 0:a3a22a35e75c 62 motore.SetMotorPower( ArduinoMotorShield::MOTOR_A, velocita) ;
mikteam10 1:b259a13421b6 63 motore.SetMotorPower( ArduinoMotorShield::MOTOR_B, velocita2) ;
mikteam10 1:b259a13421b6 64 pc.printf("\rV= %f\n\r" , velocita);
mikteam10 1:b259a13421b6 65 pc.printf("\rV2= %f\n\r" , velocita2);
mikteam10 1:b259a13421b6 66
mikteam10 1:b259a13421b6 67 }
mikteam10 1:b259a13421b6 68
mikteam10 1:b259a13421b6 69
mikteam10 1:b259a13421b6 70 if((c == 's') && (brake==false)){
mikteam10 1:b259a13421b6 71 motore.SetBrake( ArduinoMotorShield::MOTOR_A, 1) ;
mikteam10 1:b259a13421b6 72 motore.SetBrake( ArduinoMotorShield::MOTOR_B, 1) ;
mikteam10 1:b259a13421b6 73 brake=true;
mikteam10 1:b259a13421b6 74 p = pc.getc();
mikteam10 1:b259a13421b6 75 if(p == 's'){
mikteam10 1:b259a13421b6 76 motore.SetBrake( ArduinoMotorShield::MOTOR_A, 0) ;
mikteam10 1:b259a13421b6 77 motore.SetBrake( ArduinoMotorShield::MOTOR_B, 0) ;
mikteam10 1:b259a13421b6 78 motore.SetMotorPower( ArduinoMotorShield::MOTOR_A, velocita) ;
mikteam10 1:b259a13421b6 79 motore.SetMotorPower( ArduinoMotorShield::MOTOR_B, velocita2) ;
mikteam10 1:b259a13421b6 80 brake=false;
mikteam10 0:a3a22a35e75c 81 }
mikteam10 0:a3a22a35e75c 82 }
mikteam10 0:a3a22a35e75c 83
mikteam10 1:b259a13421b6 84
mikteam10 1:b259a13421b6 85
mikteam10 1:b259a13421b6 86 }
mikteam10 1:b259a13421b6 87
mikteam10 1:b259a13421b6 88
mikteam10 1:b259a13421b6 89 }