Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed X-NUCLEO-IHM05A1
Exercise_IHM05A1.cpp
00001 /** 00002 ****************************************************************************** 00003 * @file main.cpp 00004 * @author IPC Rennes 00005 * @version V1.0.0 00006 * @date April 13th, 2016 00007 * @brief mbed simple application for the STMicroelectronics X-NUCLEO-IHM05A1 00008 * Motor Control Expansion Board: control of 1 motor. 00009 ****************************************************************************** 00010 * @attention 00011 * 00012 * <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2> 00013 * 00014 * Redistribution and use in source and binary forms, with or without modification, 00015 * are permitted provided that the following conditions are met: 00016 * 1. Redistributions of source code must retain the above copyright notice, 00017 * this list of conditions and the following disclaimer. 00018 * 2. Redistributions in binary form must reproduce the above copyright notice, 00019 * this list of conditions and the following disclaimer in the documentation 00020 * and/or other materials provided with the distribution. 00021 * 3. Neither the name of STMicroelectronics nor the names of its contributors 00022 * may be used to endorse or promote products derived from this software 00023 * without specific prior written permission. 00024 * 00025 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00026 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00027 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00028 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 00029 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00030 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 00031 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00032 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 00033 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00034 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00035 * 00036 ****************************************************************************** 00037 */ 00038 00039 /* Includes ------------------------------------------------------------------*/ 00040 00041 /* mbed specific header files. */ 00042 #include "mbed.h" 00043 00044 /* Component specific header files. */ 00045 #include "L6208.h" 00046 #include "SampledSound.h" 00047 #include "Clacson.h" 00048 DigitalIn myButton(USER_BUTTON); 00049 DigitalIn Input (PC_0); 00050 AnalogIn InWave(PA_0); 00051 Serial pc(USBTX, USBRX); 00052 //DigitalOut myRele(PC_3); 00053 DigitalOut LedAD (PC_2) ;//bianche telecamere 00054 DigitalOut LedAS (PC_3) ;//bianche telecamere 00055 DigitalOut LedPD (PC_10) ;//rosse 00056 DigitalOut LedPS (PC_12) ;//rosse 00057 AnalogOut OutWave(PA_4); // pin di output per la forma d'onda analogica 00058 DigitalOut myD2(PA_10);//pin di output proveniente dal raspberry 00059 DigitalOut led2(LED2); // LED su scheda 00060 /* Definitions ---------------------------------------------------------------*/ 00061 #ifdef TARGET_NUCLEO_F334R8 00062 #define VREFA_PWM_PIN D11 00063 #define VREFB_PWM_PIN D9 00064 #elif TARGET_NUCLEO_F302R8 00065 #define VREFA_PWM_PIN D11 00066 #define VREFB_PWM_PIN D15 /* HW mandatory patch: bridge manually D9 with D15 */ 00067 #else 00068 #define VREFA_PWM_PIN D3 00069 #define VREFB_PWM_PIN D9 00070 #endif 00071 #define NUMSAMPLE 300 00072 // ticker per la generazione dell'onda con DAC 00073 Ticker SampleOutTicker; 00074 00075 void my_error_handler(uint16_t error) 00076 { 00077 /* Printing to the console. */ 00078 pc.printf("Error %d detected\r\n\n", error); 00079 00080 /* Infinite loop */ 00081 while (true) { 00082 } 00083 } 00084 00085 00086 00087 // ticker per l'acquisizione dell'onda con ADC 00088 Ticker SamplingTicker; 00089 00090 // carattere in arrivo dal PC ed equivalente numerico 00091 volatile char cReadChar; 00092 volatile int nReadChar; 00093 00094 // flag che diventa true quando si vuole fermare l'acquisizione 00095 volatile bool bStop; 00096 00097 // valore letto dall'ADC e corrispondente in tensione 00098 volatile unsigned short usReadADCLux; 00099 volatile float fReadVoltage; 00100 00101 // valore di temperatura letto dall'ADC 00102 volatile float fLux; 00103 00104 // prototipo di funzione che genera i campioni della sinusoide da utilizzare per la generazione tramite DAC 00105 void CalculateSinewave(void); 00106 // funzione di generazione suono della frequenza e ampiezza selezionate 00107 void SoundGenerate(double fFrequency); 00108 00109 // carattere in arrivo dal PC 00110 00111 volatile char CReadMusic; 00112 00113 uint32_t nProva; 00114 00115 // indice nell'array dei campioni da porre in output da DAC per Marimba 00116 volatile uint32_t nSampleSoundIndex; 00117 00118 // Periodo di generazione campioni in output DeltaT = T/NumSample 00119 double fDeltaT; 00120 // amplificazione per il dato da spedire sull'ADC 00121 volatile double fAmp; 00122 //volatile double fAmpNew; 00123 // flag per bloccare la generazione del segnale 00124 volatile int bGenerate = false; 00125 00126 // frequenza segnale da generare 00127 volatile double fFreq; 00128 //flag che diventa true se bisogna fermare la generazione di suoni 00129 00130 // periodo della sinusoide da generare 00131 double fPeriod; 00132 // tipo di suono da generare: 0=Sine, 1= Square 00133 char cSoundWave; 00134 00135 // nota corrispondente al tasto premuto 00136 volatile char cKeyToPlay= '\0'; 00137 00138 00139 //********************************************************************** 00140 // generazione suoni con i sample da file di campioni in SoundSample.h 00141 //********************************************************************** 00142 void SampleOut() 00143 { 00144 00145 // genera campioni fino al raggiungimento del numero di campioni nel file Sound.h 00146 if((nSampleSoundIndex < nSampleNum) && (bGenerate == true)) 00147 { 00148 // mette in output un campione della forma d'onda moltiplicato per l'amplificazione fAmp 00149 OutWave.write_u16(naInputSoundWave[nSampleSoundIndex]*fAmp); 00150 // incrementa l'indice del campione in output, nSampleNum è il numero dei campioni nle file Sound.h 00151 nSampleSoundIndex++; 00152 } 00153 else 00154 { 00155 // se è stato generato il numero di campioni del file oppure bGenerate = false, ferma la generazione di suoni 00156 bGenerate = false; 00157 } 00158 } 00159 00160 // prototipo di funzione che genera i campioni della sinusoide da utilizzare per la generazione tramite DAC 00161 void CalculateSinewave(void); 00162 // funzione di generazione suono della frequenza e ampiezza selezionate 00163 void SoundGenerate(double fFrequency); 00164 00165 /* Variables -----------------------------------------------------------------*/ 00166 00167 /* Initialization parameters of the motor connected to the expansion board. */ 00168 l6208_init_t init = 00169 { 00170 00171 1500, //Acceleration rate in step/s^2 or (1/16)th step/s^2 for microstep modes 00172 20, //Acceleration current torque in % (from 0 to 100) 00173 1500, //Deceleration rate in step/s^2 or (1/16)th step/s^2 for microstep modes 00174 30, //Deceleration current torque in % (from 0 to 100) 00175 1500, //Running speed in step/s or (1/16)th step/s for microstep modes 00176 50, //Running current torque in % (from 0 to 100) 00177 20, //Holding current torque in % (from 0 to 100) 00178 STEP_MODE_1_16, //Step mode via enum motorStepMode_t 00179 FAST_DECAY, //Decay mode via enum motorDecayMode_t 00180 0, //Dwelling time in ms 00181 FALSE, //Automatic HIZ STOP 00182 100000 //VREFA and VREFB PWM frequency (Hz) 00183 }; 00184 00185 /* Motor Control Component. */ 00186 L6208 *motor; 00187 00188 /* Functions -----------------------------------------------------------------*/ 00189 00190 /** 00191 * @brief This is an example of user handler for the flag interrupt. 00192 * @param None 00193 * @retval None 00194 * @note If needed, implement it, and then attach and enable it: 00195 * + motor->attach_flag_irq(&my_flag_irq_handler); 00196 * + motor->enable_flag_irq(); 00197 * To disable it: 00198 * + motor->DisbleFlagIRQ(); 00199 */ 00200 void my_flag_irq_handler(void) 00201 { 00202 pc.printf(" WARNING: \"FLAG\" interrupt triggered:\r\n"); 00203 motor->disable(); 00204 pc.printf(" Motor disabled.\r\n\n"); 00205 } 00206 00207 /** 00208 * @brief This is an example of error handler. 00209 * @param[in] error Number of the error 00210 * @retval None 00211 * @note If needed, implement it, and then attach it: 00212 * + motor->attach_error_handler(&my_error_handler); 00213 */ 00214 00215 /* Main ----------------------------------------------------------------------*/ 00216 00217 int main() 00218 { 00219 //inizializza variabili 00220 bGenerate= false; 00221 bStop= true; 00222 cReadChar= 0; 00223 //nSampleOutIndex= 0; 00224 //nSampleOutCount= 0; 00225 nSampleSoundIndex= 0; 00226 00227 //imposta il funzionamento del pulsante come "PullDown": Aperto = '0'. L'altra modalità di funzionamento è PullUp 00228 myButton.mode(PullUp); 00229 00230 // configura velocità della comunicazione seriale su USB-VirtualCom e invia messaggio di benvenuto 00231 pc.baud(921600); //921600 bps 00232 00233 // test dei LED 00234 led2=1; // Blu 00235 wait_ms(500); 00236 led2=0; 00237 00238 pc.printf("\r\nHallo Amaldi Students - Exercise 12 \r\n"); 00239 pc.printf("\n\r*** Sampled Sound Generator ***\n\r"); 00240 00241 // fissa l'amplificazione da 0 a 1 00242 fAmp = 1.0; 00243 00244 // attende la pressione di tasti sulla marimba 00245 while(true) 00246 { 00247 if(myButton == 0) 00248 { 00249 while(myButton != 1){}; // attendi che il tasto sia rilasciato 00250 fFreq=nSamplePerSec/nUnderSampleFactor;// campioni per secondo da generare = nSamplePerSec/nUnderSampleFactor 00251 pc.printf("--- Sound Generation ---\n\r"); 00252 pc.printf("--- Clacson ---\n\r"); 00253 if (myD2 == 1) 00254 { 00255 bGenerate = true; // flag true quando è attiva la generazione di suoni 00256 nSampleSoundIndex =0; //inizializza indice dell'array 00257 fDeltaT = (1.0/fFreq); // fFreq dipende dal periodo di campionamento e dal fattore di sottocampionamento 00258 SampleOutTicker.attach(&SampleOut,fDeltaT); // avvia generazione 00259 } 00260 else 00261 { 00262 bGenerate = false; // arresta la generazione di suoni 00263 } 00264 } 00265 } // while 00266 // periodo di campionamento 00267 int nDeltaT; 00268 00269 00270 // configura velocità della comunicazione seriale su USB-VirtualCom e invia messaggio di benvenuto 00271 pc.baud(921600); //921600 bps 00272 // messaggio di benvenuto 00273 pc.printf("\r\nHey Bro! Say Goodbye to your Sister !\r\n"); 00274 pc.printf("Enter Acquisition Time, DeltaT[sec]= [1 - 9]: \r\n"); 00275 // pc.printf("\r\n*** Bluetooth Temp Acquisition ***\r\n"); 00276 00277 // inizializza variabili 00278 bStop=true; 00279 //myRele = 0x00; // spegni il relè 00280 00281 00282 while (true){ 00283 if (myButton == 1) { 00284 LedAD =0x00 ; 00285 LedAS =0x00 ; 00286 LedPD =0x00 ; 00287 LedPS =0x00 ; 00288 wait(1); 00289 LedAD =0x01 ; 00290 LedAS =0x01 ; 00291 LedPD =0x01 ; 00292 LedPS =0x01 ; 00293 wait(1); 00294 LedAD =0x00 ; 00295 LedAS =0x00 ; 00296 LedPD =0x00 ; 00297 LedPS =0x00 ; 00298 wait(1); 00299 LedAD =0x01; 00300 wait_ms(500); 00301 LedAS =0x01 ; 00302 wait_ms(500); 00303 LedPD =0x01 ; 00304 wait_ms(500); 00305 LedPS =0x01 ; 00306 wait(1);}}; 00307 // configura velocità della comunicazione seriale su USB-VirtualCom e invia messaggio di benvenuto 00308 pc.baud(921600); //921600 bps 00309 //pc.baud(9600); //256000 bps 00310 pc.printf("*** Test Motor ***\n\r"); 00311 00312 /* Printing to the console. */ 00313 pc.printf("STARTING MAIN PROGRAM\r\n"); 00314 pc.printf(" Reminder:\r\n"); 00315 pc.printf(" The position unit is in agreement to the step mode.\r\n"); 00316 pc.printf(" The speed, acceleration or deceleration unit depend on the step mode:\r\n"); 00317 pc.printf(" - For normal mode and half step mode, the unit is steps/s or /s^2.\r\n"); 00318 pc.printf(" - For microstep modes, the unit is (1/16)steps/s or /s^2.\r\n"); 00319 00320 //----- Initialization 00321 /* Initializing Motor Control Component. */ 00322 motor = new L6208(D2, D8, D7, D4, D5, D6, VREFA_PWM_PIN, VREFB_PWM_PIN); 00323 if (motor->init(&init) != COMPONENT_OK) { 00324 exit(EXIT_FAILURE); 00325 } 00326 00327 /* Attaching and enabling an interrupt handler. */ 00328 motor->attach_flag_irq(&my_flag_irq_handler); 00329 motor->enable_flag_irq(); 00330 00331 /* Attaching an error handler */ 00332 motor->attach_error_handler(&my_error_handler); 00333 00334 /* Printing to the console. */ 00335 pc.printf("Motor Control Application Example for 1 Motor\r\n"); 00336 00337 //----- run the motor BACKWARD 00338 pc.printf("--> Running the motor backward.\r\n"); 00339 motor->run(StepperMotor::BWD); 00340 00341 while (motor->get_status()!=STEADY) { 00342 /* Print reached speed to the console in step/s or microsteps/s */ 00343 pc.printf(" Reached Speed: %d microstep/s.\r\n", motor->get_speed()); 00344 wait_ms(50); 00345 } 00346 pc.printf(" Reached Speed: %d microstep/s.\r\n", motor->get_speed()); 00347 00348 /* Wait for 1 second */ 00349 wait_ms(1000); 00350 00351 00352 00353 //----- Soft stop required while running 00354 pc.printf("--> Soft stop requested.\r\n"); 00355 motor->soft_stop(); 00356 00357 00358 //----- Change step mode to full step mode 00359 motor->set_step_mode(StepperMotor::STEP_MODE_FULL); 00360 pc.printf(" Motor step mode: %d (0:FS, 1:1/2, 2:1/4, 3:1/8, 4:1/16).\r\n", motor->get_step_mode()); 00361 00362 /* Get current position of device and print to the console */ 00363 pc.printf(" Position: %d.\r\n", motor->get_position()); 00364 00365 /* Set speed, acceleration and deceleration to scale with normal mode */ 00366 motor->set_max_speed(init.maxSpeedSps>>4); 00367 motor->set_acceleration(motor->get_acceleration()>>4); 00368 motor->set_deceleration(motor->get_deceleration()>>4); 00369 /* Print parameters to the console */ 00370 pc.printf(" Motor Max Speed: %d step/s.\r\n", motor->get_max_speed()); 00371 pc.printf(" Motor Min Speed: %d step/s.\r\n", motor->get_min_speed()); 00372 pc.printf(" Motor Acceleration: %d step/s.\r\n", motor->get_acceleration()); 00373 pc.printf(" Motor Deceleration: %d step/s.\r\n", motor->get_deceleration()); 00374 00375 //----- move of 200 steps in the FW direction 00376 pc.printf("--> Moving forward 200 steps.\r\n"); 00377 motor->move(StepperMotor::FWD, 200); 00378 int i,a=0; 00379 00380 while (true) { 00381 00382 if ( Input == 0 ) 00383 { 00384 a=a+1; 00385 } 00386 if(a>1) 00387 { 00388 a=0; 00389 00390 } 00391 00392 if(a==0) 00393 { 00394 /* Request device to go position -3200 */ 00395 motor->go_to(150); 00396 /* Waiting while the motor is active. */ 00397 motor->wait_while_active(); 00398 } 00399 else 00400 { 00401 /* Request device to go position -3200 */ 00402 motor->go_to(-150); 00403 /* Waiting while the motor is active. */ 00404 motor->wait_while_active(); 00405 00406 } 00407 00408 00409 } 00410 } 00411 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Generated on Thu Jul 14 2022 00:23:39 by
