Bologna
Dependencies: TCS3200 X_NUCLEO_IHM12A1 mbed-rtos mbed
Fork of SchedamotoriIHM12A1_copy by
Revision 7:e14976007f66, committed 2018-04-26
- Comitter:
- gandhi1
- Date:
- Thu Apr 26 11:00:39 2018 +0000
- Parent:
- 6:b5d006c1eb59
- Commit message:
- Bologna
Changed in this revision
diff -r b5d006c1eb59 -r e14976007f66 TCS3200.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/TCS3200.lib Thu Apr 26 11:00:39 2018 +0000 @@ -0,0 +1,1 @@ +https://mbed.org/users/grantphillips/code/TCS3200/#b98e768bc655
diff -r b5d006c1eb59 -r e14976007f66 X-NUCLEO-IHM12A1.lib --- a/X-NUCLEO-IHM12A1.lib Fri Feb 24 16:13:24 2017 +0000 +++ b/X-NUCLEO-IHM12A1.lib Thu Apr 26 11:00:39 2018 +0000 @@ -1,1 +1,1 @@ -https://developer.mbed.org/teams/ST/code/X_NUCLEO_IHM12A1/#232e0c730f59 +https://developer.mbed.org/teams/ST/code/X_NUCLEO_IHM12A1/#123d44b9fb9d
diff -r b5d006c1eb59 -r e14976007f66 main.cpp --- a/main.cpp Fri Feb 24 16:13:24 2017 +0000 +++ b/main.cpp Thu Apr 26 11:00:39 2018 +0000 @@ -1,73 +1,145 @@ #include "mbed.h" -#include "stspin240_250_class.h" -Serial pc(USBTX, USBRX); - Stspin240_250_Init_t initDeviceParameters = +#include "rtos.h" +#include "TCS3200.h" +#include "STSpin240_250.h" + +STSpin240_250_init_t init = { - 20000, - 20000, - 20000, - 50, - TRUE + 20000, /* Frequency of PWM of Input Bridge A in Hz up to 100000Hz */ + 20000, /* Frequency of PWM of Input Bridge B in Hz up to 100000Hz */ + 20000, /* Frequency of PWM used for Ref pin in Hz up to 100000Hz */ + 50, /* Duty cycle of PWM used for Ref pin (from 0 to 100) */ + TRUE /* Dual Bridge configuration (FALSE for mono, TRUE for dual brush dc) */ }; -STSPIN240_250 *motor; + +STSpin240_250 *motor; + +/*FUNZIONI:.*/ +void config_motor(); +void read_color(); +void controllo_motori(); +void check_color(); + + +TCS3200 color(D3,D8,D12,D13,D14); +DigitalIn ir1 (D10); //destra +DigitalIn ir2 (D11); //sinistra + +/*Variabili per leggere i colori.*/ + +long red; +long green; +long blue; + +/*Variabili per abilitare i motori sono dopo una prima lettura del sensore di colore,poichè inizialmente da sempre nessun colore come comando*/ +int start=0; + + +/*Definizione thread per lettura colori*/ +Thread thread; + + +/* Main ----------------------------------------------------------------------*/ +Serial pc(USBTX,USBRX); + +void read_color() +{ + + while(true) { + check_color(); + start=1; + + Thread::wait(100); + } +} int main() { - uint8_t demoStep = 0; - #if (defined TARGET_NUCLEO_F030R8)||(defined TARGET_NUCLEO_F334R8) - motor = new STSPIN240_250(D2, D9, D6, D7, D5, D4, A2); - #elif (defined TARGET_NUCLEO_L152RE) - motor = new STSPIN240_250(D2, D9, D6, D7, D5, D4, A3); - #else - motor = new STSPIN240_250(D2, D9, D6, D7, D5, D4, A0); - #endif - if (motor->Init(&initDeviceParameters) != COMPONENT_OK) exit(EXIT_FAILURE); - motor->SetDualFullBridgeConfig(1); - motor->SetRefPwmFreq(0, 15000); - motor->SetRefPwmDc(0, 60); - motor->SetBridgeInputPwmFreq(0,10000); - motor->SetBridgeInputPwmFreq(1,10000); - - while (1) - { - switch (demoStep) - { - case 0: - /* Va avanti */ - motor->SetSpeed(0,50); - motor->SetSpeed(1,50); - motor->Run(0, BDCMotor::FWD); - motor->Run(1, BDCMotor::FWD); - break; - case 1: - /* Va a destra */ - motor->SetSpeed(0,47); - motor->SetSpeed(1,23); - motor->Run(0, BDCMotor::FWD); - motor->Run(1, BDCMotor::FWD); + + config_motor(); + + //Set the scaling factor + color.SetMode(TCS3200::SCALE_100); + - break; - case 2: - /* Va indietro */ - motor->SetSpeed(0,50); - motor->SetSpeed(1,50); - motor->Run(0, BDCMotor::BWD); - motor->Run(1, BDCMotor::BWD); - break; - case 3: - /* Va a sinistra */ - motor->SetSpeed(0,25); - motor->SetSpeed(1,30); - motor->Run(0, BDCMotor::FWD); - motor->Run(1, BDCMotor::FWD); - break; - } - wait_ms(4500); - demoStep++; - if (demoStep > 3) - { - demoStep = 0; + thread.start(read_color); + + while(true){ + + if(start==1) { + + controllo_motori(); //se il sensore ha effettuato una prima lettura allora avvio il controllo motori + + } + + + else if(start==0) { + motor->set_speed(0,0); + motor->set_speed(1,0); + motor->run(1, BDCMotor::BWD); + motor->run(0, BDCMotor::BWD); + } } - } } +//Elenco Funzioni +void config_motor(){ + #if (defined TARGET_NUCLEO_F030R8)||(defined TARGET_NUCLEO_F334R8) + motor = new STSpin240_250(D2, D9, D6, D7, D5, D4, A2); + #elif (defined TARGET_NUCLEO_L152RE) + motor = new STSpin240_250(D2, D9, D6, D7, D5, D4, A3); + #elif (defined TARGET_NUCLEO_F429ZI) + motor = new STSpin240_250(D2, D9, D6, D7, D5, D3, A0); + #else + motor = new STSpin240_250(D2, D9, D6, D7, D5, D4, A0); + #endif + if (motor->init(&init) != COMPONENT_OK) exit(EXIT_FAILURE); + + motor->set_dual_full_bridge_config(1); + motor->set_ref_pwm_freq(0, 15000); + motor->set_ref_pwm_dc(0, 60); + motor->set_bridge_input_pwm_freq(0,10000); + motor->set_bridge_input_pwm_freq(1,10000); +} + +void check_color(){ + red = color.ReadRed(); + blue=color.ReadBlue(); + green = color.ReadGreen(); +} + +void controllo_motori(){ + + if(red<green && red<blue) { + + motor->set_speed(0,0); + motor->set_speed(1,0); + motor->run(1, BDCMotor::FWD); + motor->run(0, BDCMotor::FWD); + + } + + else { + motor->set_speed(0,40); + motor->set_speed(1,40); + motor->run(1, BDCMotor::FWD); + motor->run(0, BDCMotor::FWD); +} + + + if (ir1 && !ir2) { + motor->set_speed(0,40); + motor->set_speed(1,100); + motor->run(0, BDCMotor::BWD); + motor->run(1, BDCMotor::FWD); + } + + if (!ir1 && ir2) { + motor->set_speed(0,100); + motor->set_speed(1,40); + motor->run(0, BDCMotor::FWD); + motor->run(1, BDCMotor::BWD); + } +} + + \ No newline at end of file
diff -r b5d006c1eb59 -r e14976007f66 mbed-rtos.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-rtos.lib Thu Apr 26 11:00:39 2018 +0000 @@ -0,0 +1,1 @@ +https://mbed.org/users/mbed_official/code/mbed-rtos/#5713cbbdb706
diff -r b5d006c1eb59 -r e14976007f66 mbed.bld --- a/mbed.bld Fri Feb 24 16:13:24 2017 +0000 +++ b/mbed.bld Thu Apr 26 11:00:39 2018 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/mbed_official/code/mbed/builds/ef9c61f8c49f \ No newline at end of file +http://mbed.org/users/mbed_official/code/mbed/builds/7130f322cb7e \ No newline at end of file