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: X_NUCLEO_IHM03A1 mbed
Fork of IHM03A1_ExampleFor1Motor by
main.cpp
00001 /** 00002 ****************************************************************************** 00003 * @file main.cpp 00004 * @author IPC Rennes 00005 * @version V1.0.0 00006 * @date March 18th, 2016 00007 * @brief mbed test application for the STMicroelectronics X-NUCLEO-IHM03A1 00008 * Motor Control Expansion Board: control of 1 motor. 00009 ****************************************************************************** 00010 * @attention 00011 * 00012 * <h2><center>© COPYRIGHT(c) 2015 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 00040 /* Includes ------------------------------------------------------------------*/ 00041 00042 /* mbed specific header files. */ 00043 #include "mbed.h" 00044 00045 /* Helper header files. */ 00046 #include "DevSPI.h" 00047 00048 /* Component specific header files. */ 00049 #include "powerstep01_class.h" 00050 00051 /* Variables -----------------------------------------------------------------*/ 00052 00053 /* Initialization parameters of the motor connected to the expansion board. */ 00054 /* Current mode. */ 00055 powerstep01_Init_u_t initDeviceParameters = 00056 { 00057 /* common parameters */ 00058 .cm.cp.cmVmSelection = POWERSTEP01_CM_VM_CURRENT, // enum powerstep01_CmVm_t 00059 582, // Acceleration rate in step/s2, range 14.55 to 59590 steps/s^2 00060 582, // Deceleration rate in step/s2, range 14.55 to 59590 steps/s^2 00061 488, // Maximum speed in step/s, range 15.25 to 15610 steps/s 00062 0, // Minimum speed in step/s, range 0 to 976.3 steps/s 00063 POWERSTEP01_LSPD_OPT_OFF, // Low speed optimization bit, enum powerstep01_LspdOpt_t 00064 244.16, // Full step speed in step/s, range 7.63 to 15625 steps/s 00065 POWERSTEP01_BOOST_MODE_OFF, // Boost of the amplitude square wave, enum powerstep01_BoostMode_t 00066 281.25, // Overcurrent threshold settings via enum powerstep01_OcdTh_t 00067 STEP_MODE_1_16, // Step mode settings via enum motorStepMode_t 00068 POWERSTEP01_SYNC_SEL_DISABLED, // Synch. Mode settings via enum powerstep01_SyncSel_t 00069 (POWERSTEP01_ALARM_EN_OVERCURRENT| 00070 POWERSTEP01_ALARM_EN_THERMAL_SHUTDOWN| 00071 POWERSTEP01_ALARM_EN_THERMAL_WARNING| 00072 POWERSTEP01_ALARM_EN_UVLO| 00073 POWERSTEP01_ALARM_EN_STALL_DETECTION| 00074 POWERSTEP01_ALARM_EN_SW_TURN_ON| 00075 POWERSTEP01_ALARM_EN_WRONG_NPERF_CMD), // Alarm settings via bitmap enum powerstep01_AlarmEn_t 00076 POWERSTEP01_IGATE_64mA, // Gate sink/source current via enum powerstep01_Igate_t 00077 POWERSTEP01_TBOOST_0ns, // Duration of the overboost phase during gate turn-off via enum powerstep01_Tboost_t 00078 POWERSTEP01_TCC_500ns, // Controlled current time via enum powerstep01_Tcc_t 00079 POWERSTEP01_WD_EN_DISABLE, // External clock watchdog, enum powerstep01_WdEn_t 00080 POWERSTEP01_TBLANK_375ns, // Duration of the blanking time via enum powerstep01_TBlank_t 00081 POWERSTEP01_TDT_125ns, // Duration of the dead time via enum powerstep01_Tdt_t 00082 /* current mode parameters */ 00083 328.12, // Hold torque in mV, range from 7.8mV to 1000 mV 00084 328.12, // Running torque in mV, range from 7.8mV to 1000 mV 00085 328.12, // Acceleration torque in mV, range from 7.8mV to 1000 mV 00086 328.12, // Deceleration torque in mV, range from 7.8mV to 1000 mV 00087 POWERSTEP01_TOFF_FAST_8us, //Maximum fast decay time , enum powerstep01_ToffFast_t 00088 POWERSTEP01_FAST_STEP_12us, //Maximum fall step time , enum powerstep01_FastStep_t 00089 3.0, // Minimum on-time in us, range 0.5us to 64us 00090 21.0, // Minimum off-time in us, range 0.5us to 64us 00091 POWERSTEP01_CONFIG_INT_16MHZ_OSCOUT_2MHZ, // Clock setting , enum powerstep01_ConfigOscMgmt_t 00092 POWERSTEP01_CONFIG_SW_HARD_STOP, // External switch hard stop interrupt mode, enum powerstep01_ConfigSwMode_t 00093 POWERSTEP01_CONFIG_TQ_REG_TVAL_USED, // External torque regulation enabling , enum powerstep01_ConfigEnTqReg_t 00094 POWERSTEP01_CONFIG_VS_COMP_DISABLE, // Motor Supply Voltage Compensation enabling , enum powerstep01_ConfigEnVscomp_t 00095 POWERSTEP01_CONFIG_OC_SD_DISABLE, // Over current shutwdown enabling, enum powerstep01_ConfigOcSd_t 00096 POWERSTEP01_CONFIG_UVLOVAL_LOW, // UVLO Threshold via powerstep01_ConfigUvLoVal_t 00097 POWERSTEP01_CONFIG_VCCVAL_15V, // VCC Val, enum powerstep01_ConfigVccVal_t 00098 POWERSTEP01_CONFIG_TSW_048us, // Switching period, enum powerstep01_ConfigTsw_t 00099 POWERSTEP01_CONFIG_PRED_DISABLE // Predictive current enabling , enum powerstep01_ConfigPredEn_t 00100 }; 00101 00102 /* Motor Control Component. */ 00103 POWERSTEP01 *motor; 00104 00105 00106 /* Functions -----------------------------------------------------------------*/ 00107 00108 /** 00109 * @brief This is an example of user handler for the flag interrupt. 00110 * @param None 00111 * @retval None 00112 * @note If needed, implement it, and then attach and enable it: 00113 * + motor->AttachFlagIRQ(&myFlagIRQHandler); 00114 * + motor->EnableFlagIRQ(); 00115 * To disable it: 00116 * + motor->DisbleFlagIRQ(); 00117 */ 00118 void myFlagIRQHandler(void) 00119 { 00120 /* Set ISR flag. */ 00121 motor->isrFlag = TRUE; 00122 00123 /* Get the value of the status register. */ 00124 unsigned int statusRegister = motor->GetStatus(); 00125 00126 printf(" WARNING: \"FLAG\" interrupt triggered.\r\n"); 00127 /* Check HIZ flag: if set, power brigdes are disabled */ 00128 if ((statusRegister & POWERSTEP01_STATUS_HIZ)==POWERSTEP01_STATUS_HIZ) 00129 { 00130 // HIZ state 00131 printf(" HiZ state.\r\n"); 00132 } 00133 /* Check BUSY flag: if not set, a command is under execution */ 00134 if ((statusRegister & POWERSTEP01_STATUS_BUSY)==0) 00135 { 00136 // BUSY 00137 printf(" Busy.\r\n"); 00138 } 00139 /* Check SW_F flag: if not set, the SW input is opened */ 00140 if ((statusRegister & POWERSTEP01_STATUS_SW_F )!=0) 00141 { 00142 // SW closed (connected to ground) 00143 printf(" SW closed (connected to ground).\r\n"); 00144 } 00145 /* Check SW_EN bit */ 00146 if ((statusRegister & POWERSTEP01_STATUS_SW_EVN)== 00147 POWERSTEP01_STATUS_SW_EVN) 00148 { 00149 // SW turn_on event 00150 printf(" SW turn_on event.\r\n"); 00151 } 00152 if ((statusRegister & POWERSTEP01_STATUS_MOT_STATUS)== 00153 POWERSTEP01_STATUS_MOT_STATUS_STOPPED) 00154 { 00155 // MOTOR STOPPED 00156 printf(" Stopped.\r\n"); 00157 } 00158 else 00159 { 00160 if ((statusRegister & POWERSTEP01_STATUS_MOT_STATUS)== 00161 POWERSTEP01_STATUS_MOT_STATUS_ACCELERATION) 00162 { 00163 // MOTOR ACCELERATION 00164 printf(" Accelerating "); 00165 } 00166 else if ((statusRegister & POWERSTEP01_STATUS_MOT_STATUS)== 00167 POWERSTEP01_STATUS_MOT_STATUS_DECELERATION) 00168 { 00169 // MOTOR DECELERATION 00170 printf(" Decelerating "); 00171 } 00172 else if ((statusRegister & POWERSTEP01_STATUS_MOT_STATUS)== 00173 POWERSTEP01_STATUS_MOT_STATUS_CONST_SPD) 00174 { 00175 // MOTOR RUNNING AT CONSTANT SPEED 00176 printf(" Steady running "); 00177 } 00178 /* Check direction bit */ 00179 if ((statusRegister & POWERSTEP01_STATUS_DIR)==0) 00180 { 00181 // StepperMotor::BWD 00182 printf(" in backward direction.\r\n"); 00183 } 00184 else 00185 { 00186 // StepperMotor::FWD 00187 printf(" in forward direction.\r\n"); 00188 } 00189 } 00190 /* Check Command Error flag: if set, the command received by SPI can't be */ 00191 /* performed. This occurs for instance when a move command is sent to the */ 00192 /* Powerstep01 while it is already running */ 00193 if ((statusRegister & POWERSTEP01_STATUS_CMD_ERROR)== 00194 POWERSTEP01_STATUS_CMD_ERROR) 00195 { 00196 // Command Error 00197 printf(" Non-performable command detected.\r\n"); 00198 } 00199 /* Check Step mode clock flag: if set, the device is working in step clock mode */ 00200 if ((statusRegister & POWERSTEP01_STATUS_STCK_MOD)== 00201 POWERSTEP01_STATUS_STCK_MOD) 00202 { 00203 //Step clock mode enabled 00204 printf(" Step clock mode enabled.\r\n"); 00205 } 00206 /* Check UVLO flag: if not set, there is an undervoltage lock-out */ 00207 if ((statusRegister & POWERSTEP01_STATUS_UVLO)==0) 00208 { 00209 //Undervoltage lock-out 00210 printf(" undervoltage lock-out.\r\n"); 00211 } 00212 /* Check UVLO ADC flag: if not set, there is an ADC undervoltage lock-out */ 00213 if ((statusRegister & POWERSTEP01_STATUS_UVLO_ADC)==0) 00214 { 00215 //ADC undervoltage lock-out 00216 printf(" ADC undervoltage lock-out:\r\n"); 00217 printf(" Expected with default IHM03A1 HW configuration.\r\n"); 00218 } 00219 /* Check thermal STATUS flags: if set, the thermal status is not normal */ 00220 if ((statusRegister & POWERSTEP01_STATUS_TH_STATUS)!=0) 00221 { 00222 //thermal status: 1: Warning, 2: Bridge shutdown, 3: Device shutdown 00223 if (((statusRegister & POWERSTEP01_STATUS_TH_STATUS)>>11)==1) 00224 { 00225 printf(" Thermal status - Warning.\r\n"); 00226 } 00227 else if (((statusRegister & POWERSTEP01_STATUS_TH_STATUS)>>11)==2) 00228 { 00229 printf(" Thermal status - Bridge shutdown.\r\n"); 00230 } 00231 else if (((statusRegister & POWERSTEP01_STATUS_TH_STATUS)>>11)==3) 00232 { 00233 printf(" Thermal status - Device shutdown.\r\n"); 00234 } 00235 } 00236 /* Check OCD flag: if not set, there is an overcurrent detection */ 00237 if ((statusRegister & POWERSTEP01_STATUS_OCD)==0) 00238 { 00239 //Overcurrent detection 00240 printf(" Overcurrent detection.\r\n"); 00241 } 00242 /* Check STALL_A flag: if not set, there is a Stall condition on bridge A */ 00243 if ((statusRegister & POWERSTEP01_STATUS_STALL_A)==0) 00244 { 00245 //Bridge A stalled 00246 printf(" Bridge A stalled.\r\n"); 00247 } 00248 /* Check STALL_B flag: if not set, there is a Stall condition on bridge B */ 00249 if ((statusRegister & POWERSTEP01_STATUS_STALL_B)==0) 00250 { 00251 //Bridge B stalled 00252 printf(" Bridge B stalled.\r\n"); 00253 } 00254 00255 /* Reset ISR flag. */ 00256 motor->isrFlag = FALSE; 00257 } 00258 00259 /** 00260 * @brief This is an example of user handler for the busy interrupt. 00261 * @param None 00262 * @retval None 00263 * @note If needed, implement it, and then attach and enable it: 00264 * + motor->AttachBusyIRQ(&myBusyIRQHandler); 00265 * + motor->EnableBusyIRQ(); 00266 * To disable it: 00267 * + motor->DisbleBusyIRQ(); 00268 */ 00269 void myBusyIRQHandler(void) 00270 { 00271 /* Set ISR flag. */ 00272 motor->isrFlag = TRUE; 00273 00274 if (motor->CheckBusyHw()) 00275 { 00276 /* Busy pin is low, so at list one Powerstep01 chip is busy */ 00277 /* To be customized (for example Switch on a LED) */ 00278 } 00279 else 00280 { 00281 /* To be customized (for example Switch off a LED) */ 00282 } 00283 00284 /* Reset ISR flag. */ 00285 motor->isrFlag = FALSE; 00286 } 00287 00288 /** 00289 * @brief This is an example of error handler. 00290 * @param[in] error Number of the error 00291 * @retval None 00292 * @note If needed, implement it, and then attach it: 00293 * + motor->AttachErrorHandler(&myErrorHandler); 00294 */ 00295 void myErrorHandler(uint16_t error) 00296 { 00297 /* Printing to the console. */ 00298 printf("Error %d detected\r\n\n", error); 00299 00300 /* Infinite loop */ 00301 while(1) 00302 { 00303 } 00304 } 00305 00306 /* Main ----------------------------------------------------------------------*/ 00307 00308 int main() 00309 { 00310 int32_t pos; 00311 uint32_t unsignedIntegerValue; 00312 float floatValue; 00313 00314 /* Printing to the console. */ 00315 printf("STARTING MAIN PROGRAM\r\n"); 00316 printf(" Reminder:\r\n"); 00317 printf(" The position unit is in agreement to the step mode.\r\n"); 00318 printf(" The speed, acceleration or deceleration unit\r\n"); 00319 printf(" do not depend on the step mode and the step unit is a full step.\r\n"); 00320 00321 //----- Initialization 00322 /* Initializing SPI bus. */ 00323 DevSPI dev_spi(D11, D12, D13); 00324 00325 /* Initializing Motor Control Component. */ 00326 motor = new POWERSTEP01(D2, D4, D8, D9, D10, dev_spi); 00327 if (motor->Init(&initDeviceParameters) != COMPONENT_OK) exit(EXIT_FAILURE); 00328 00329 /* Attaching and enabling interrupt handlers. */ 00330 motor->AttachFlagIRQ(&myFlagIRQHandler); 00331 motor->EnableFlagIRQ(); 00332 motor->AttachBusyIRQ(&myBusyIRQHandler); 00333 motor->EnableBusyIRQ(); 00334 00335 /* Attaching an error handler */ 00336 motor->AttachErrorHandler(&myErrorHandler); 00337 00338 /* Printing to the console. */ 00339 printf("Motor Control Application Example for 1 Motor\r\n"); 00340 00341 //----- Move of 16000 steps in the FW direction 00342 /* Printing to the console. */ 00343 printf("--> Moving forward 16000 steps.\r\n"); 00344 00345 /* Move 16000 steps in the FORWARD direction*/ 00346 motor->Move(StepperMotor::FWD, 16000); 00347 00348 /* Waiting while the motor is active. */ 00349 motor->WaitWhileActive(); 00350 00351 /* Wait for 2 seconds */ 00352 wait_ms(2000); 00353 00354 //----- Move of 16000 steps in the BW direction 00355 /* Printing to the console. */ 00356 printf("--> Moving backward 16000 steps.\r\n"); 00357 00358 /* Move 16000 steps in the BACKWARD direction*/ 00359 motor->Move(StepperMotor::BWD, 16000); 00360 00361 /* Waiting while the motor is active. */ 00362 motor->WaitWhileActive(); 00363 00364 /* Wait for 2 seconds */ 00365 wait_ms(2000); 00366 00367 //----- Go to position -6400 00368 /* Printing to the console. */ 00369 printf("--> Go to position -6400 steps.\r\n"); 00370 00371 /* Request device to go to position -6400 */ 00372 motor->GoTo(-6400); 00373 00374 /* Wait for the motor ends moving */ 00375 motor->WaitWhileActive(); 00376 00377 /* Get current position of device and print to the console */ 00378 pos = motor->GetPosition(); 00379 printf(" Position: %d.\r\n", pos); 00380 00381 /* Mark the current position and print to the console */ 00382 motor->SetMark(); 00383 printf(" Position marked: -6400.\r\n"); 00384 00385 /* Wait for 2 seconds */ 00386 wait_ms(2000); 00387 00388 //----- Go Home 00389 /* Printing to the console. */ 00390 printf("--> Go to home position.\r\n"); 00391 00392 /* Request device to go to Home */ 00393 motor->GoHome(); 00394 motor->WaitWhileActive(); 00395 00396 /* Get current position of device and print to the console */ 00397 pos = motor->GetPosition(); 00398 printf(" Position: %d.\r\n", pos); 00399 00400 /* Wait for 2 seconds */ 00401 wait_ms(2000); 00402 00403 //----- Go to position 6400 00404 /* Printing to the console. */ 00405 printf("--> Go to position 6400 steps.\r\n"); 00406 00407 /* Request device to go to position 6400 */ 00408 motor->GoTo(6400); 00409 00410 /* Wait for the motor of device ends moving */ 00411 motor->WaitWhileActive(); 00412 00413 /* Get current position of device and print to the console */ 00414 pos = motor->GetPosition(); 00415 printf(" Position: %d.\r\n", pos); 00416 00417 /* Wait for 2 seconds */ 00418 wait_ms(2000); 00419 00420 //----- Go Mark which was set previously after go to -6400 00421 /* Printing to the console. */ 00422 printf("--> Go to the marked position.\r\n"); 00423 00424 /* Request device to go to Mark position */ 00425 motor->GoMark(); 00426 00427 /* Wait for the motor of device ends moving */ 00428 motor->WaitWhileActive(); 00429 00430 /* Get current position of device and print to the console */ 00431 pos = motor->GetPosition(); 00432 printf(" Position: %d.\r\n", pos); 00433 00434 /* Wait for 2 seconds */ 00435 wait_ms(2000); 00436 00437 //----- Run the motor BACKWARD 00438 /* Printing to the console. */ 00439 printf("--> Run the motor backward at 400 step/s.\r\n"); 00440 00441 /* Request device to run BACKWARD at 400 step/s */ 00442 motor->Run(StepperMotor::BWD,400); 00443 00444 //----- Get parameter example 00445 /* Wait for device reaches the targeted speed */ 00446 while((motor->ReadStatusRegister() & POWERSTEP01_STATUS_MOT_STATUS)!= 00447 POWERSTEP01_STATUS_MOT_STATUS_CONST_SPD); 00448 00449 /* Record the reached speed in step/s */ 00450 floatValue = motor->GetAnalogValue(POWERSTEP01_SPEED); 00451 /* Print reached speed to the console in step/s */ 00452 printf(" Reached Speed: %f step/s.\r\n", floatValue); 00453 00454 /* Record the reached speed in step/s rounded to integer */ 00455 unsignedIntegerValue = motor->GetSpeed(); 00456 /* Print reached speed to the console in step/s */ 00457 printf(" Reached Speed: %d step/s.\r\n", unsignedIntegerValue); 00458 00459 /* Record the reached speed in step/tick */ 00460 unsignedIntegerValue = motor->GetRawParameter(POWERSTEP01_SPEED); 00461 /* Print reached speed to the console in step/tick */ 00462 printf(" Reached Speed: %d step/tick.\r\n", unsignedIntegerValue); 00463 00464 //----- Soft stopped required while running 00465 /* Printing to the console. */ 00466 printf("--> Soft stop requested.\r\n"); 00467 00468 /* Request a soft stop of device and keep the power bridges enabled */ 00469 motor->SoftHiZ(); 00470 00471 /* Wait for the motor of device ends moving */ 00472 motor->WaitWhileActive(); 00473 00474 /* Wait for 2 seconds */ 00475 wait_ms(2000); 00476 00477 //----- Run stopped by hardstop 00478 /* Printing to the console. */ 00479 printf("--> Run the motor forward at 300 step/s.\r\n"); 00480 00481 /* Request device to run in FORWARD direction at 300 step/s */ 00482 motor->Run(StepperMotor::FWD,300); 00483 wait_ms(5000); 00484 00485 /* Printing to the console. */ 00486 printf("--> Hard stop requested.\r\n"); 00487 00488 /* Request device to immediatly stop */ 00489 motor->HardStop(); 00490 motor->WaitWhileActive(); 00491 00492 /* Wait for 2 seconds */ 00493 wait_ms(2000); 00494 00495 //----- GOTO stopped by soft stop 00496 /* Printing to the console. */ 00497 printf("--> Go to position 200000 steps.\r\n"); 00498 00499 /* Request device to go to position 200000 */ 00500 motor->GoTo(200000); 00501 wait_ms(2000); 00502 00503 /* Printing to the console. */ 00504 printf("--> Soft stop requested.\r\n"); 00505 00506 /* Request device to perform a soft stop */ 00507 motor->SoftStop(); 00508 motor->WaitWhileActive(); 00509 00510 /* Get current position of device and print to the console */ 00511 pos = motor->GetPosition(); 00512 printf(" Position: %d.\r\n", pos); 00513 00514 /* Wait for 2 seconds */ 00515 wait_ms(2000); 00516 00517 //----- Read inexistent register to test MyFlagInterruptHandler 00518 /* Printing to the console. */ 00519 printf("--> Try to read an inexistent register.\r\n"); 00520 00521 /* Try to read an inexistent register */ 00522 /* the flag interrupt should be raised */ 00523 /* and the MyFlagInterruptHandler function called */ 00524 motor->GetRawParameter(0x1F); 00525 00526 //----- Put the bridges in high impedance 00527 /* Printing to the console. */ 00528 printf("--> Request disabling of device power bridges.\r\n"); 00529 00530 /* Request disabling of device power bridges */ 00531 motor->HardHiZ(); 00532 wait_ms(5000); 00533 00534 //----- Step clock mode example 00535 /* Printing to the console. */ 00536 printf("--> Enable Step Clock Mode of the Powerstep01, enabling power bridges.\r\n"); 00537 00538 /* Enable Step Clock Mode of the Powerstep01, enabling power bridges */ 00539 motor->StepClockModeEnable(StepperMotor::FWD); 00540 00541 /* Printing to the console. */ 00542 printf("--> Start step clock.\r\n"); 00543 00544 /* Enable the step clock at 333 Hz */ 00545 motor->StepClockStart(333); 00546 00547 /* Let the motor runs for 5 second at 333 step/s */ 00548 wait_ms(5000); 00549 00550 /* Printing to the console. */ 00551 printf("--> Stop step clock.\r\n"); 00552 00553 /* Stop the step clock */ 00554 motor->StepClockStop(); 00555 00556 /* Wait for 2 seconds */ 00557 wait_ms(2000); 00558 00559 //----- Set parameter example 00560 /* Printing to the console. */ 00561 printf("--> Set POWERSTEP01_MAX_SPEED parameter to 3.\r\n"); 00562 printf(" This is corresponding to 45.75 full step/s.\r\n"); 00563 00564 /* Change the maximum speed to 3*2^18 step/tick */ 00565 motor->SetRawParameter(POWERSTEP01_MAX_SPEED, 3); 00566 00567 /* Printing to the console. */ 00568 printf("--> Move the motor 3200 steps in forward direction.\r\n"); 00569 00570 /* Request device to move 3200 microsteps */ 00571 motor->Move(StepperMotor::FWD,3200); 00572 00573 /* Wait for the motor ends moving */ 00574 motor->WaitWhileActive(); 00575 00576 /* Wait for 2 seconds */ 00577 wait_ms(2000); 00578 00579 //----- Change step mode to full step mode 00580 /* Printing to the console. */ 00581 printf("--> Select full step mode.\r\n"); 00582 00583 /* Select full step mode for device */ 00584 motor->SetStepMode(StepperMotor::STEP_MODE_FULL); 00585 00586 /* Printing to the console. */ 00587 printf("--> Set the device position to POWERSTEP01_MIN_POSITION + 199 (-2^21+199).\r\n"); 00588 00589 /* Set the device position to POWERSTEP01_MIN_POSITION + 199 */ 00590 motor->SetRawParameter(POWERSTEP01_ABS_POS,(uint32_t)(POWERSTEP01_MIN_POSITION+199)); 00591 00592 /* Get current position of device and print to the console */ 00593 pos = motor->GetPosition(); 00594 printf(" Position: %d.\r\n", pos); 00595 00596 /* Printing to the console. */ 00597 printf("--> Go to POWERSTEP01_MAX_POSITION (2^21-1) using the shortest path.\r\n"); 00598 00599 /* Request device to go to the POWERSTEP01_MAX_POSITION using the shortest path */ 00600 motor->GoTo(POWERSTEP01_MAX_POSITION); 00601 00602 /* Wait for the motor of device ends moving */ 00603 motor->WaitWhileActive(); 00604 00605 /* Get current position of device and print to the console */ 00606 pos = motor->GetPosition(); 00607 printf(" Position: %d.\r\n", pos); 00608 00609 /* Wait for 2 seconds */ 00610 wait_ms(2000); 00611 00612 /* Printing to the console. */ 00613 printf("--> Set the device position to POWERSTEP01_MIN_POSITION + 199 (-2^21+199).\r\n"); 00614 00615 /* Set the device position to POWERSTEP01_MIN_POSITION + 199 */ 00616 motor->SetRawParameter(POWERSTEP01_ABS_POS,(uint32_t)(POWERSTEP01_MIN_POSITION+199)); 00617 00618 /* Get current position of device and print to the console */ 00619 pos = motor->GetPosition(); 00620 printf(" Position: %d.\r\n", pos); 00621 00622 //----- GoTo_DIR example 00623 /* Printing to the console. */ 00624 printf("--> Go to the POWERSTEP01_MAX_POSITION using backward direction.\r\n"); 00625 00626 /* Request device to go to the POWERSTEP01_MAX_POSITION using backward direction */ 00627 motor->GoTo(StepperMotor::BWD,POWERSTEP01_MAX_POSITION); 00628 00629 /* Wait for the motor of device ends moving */ 00630 motor->WaitWhileActive(); 00631 00632 /* Get current position of device and print to the console */ 00633 pos = motor->GetPosition(); 00634 printf(" Position: %d.\r\n", pos); 00635 00636 /* Printing to the console. */ 00637 printf("--> Restore device initial max speed.\r\n"); 00638 00639 /* Restore device initial max speed */ 00640 motor->SetAnalogValue(POWERSTEP01_MAX_SPEED, POWERSTEP01_CONF_PARAM_MAX_SPEED_DEVICE_0); 00641 00642 /* Get the maximum speed in step/s */ 00643 floatValue = motor->GetAnalogValue(POWERSTEP01_MAX_SPEED); 00644 /* Print maximum speed to the console in step/s */ 00645 printf(" Maximum Speed: %f step/s.\r\n", floatValue); 00646 00647 //----- Restore initial microstepping mode 00648 /* Printing to the console. */ 00649 printf("--> Restore device initial step mode.\r\n"); 00650 00651 /* Reset device to its initial microstepping mode */ 00652 motor->SetStepMode((StepperMotor::step_mode_t)POWERSTEP01_CONF_PARAM_STEP_MODE_DEVICE_0); 00653 00654 /* Wait for 2 seconds */ 00655 wait_ms(2000); 00656 00657 /* Printing to the console. */ 00658 printf("--> Infinite Loop...\r\n"); 00659 /* Infinite Loop. */ 00660 while (1) 00661 { 00662 /* Request device to go position -6400 */ 00663 motor->GoTo(-6400); 00664 00665 /* Waiting while the motor is active. */ 00666 motor->WaitWhileActive(); 00667 00668 /* Request device to go position 6400 */ 00669 motor->GoTo(6400); 00670 00671 /* Waiting while the motor is active. */ 00672 motor->WaitWhileActive(); 00673 } 00674 } 00675 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Generated on Sat Jul 16 2022 09:12:55 by
1.7.2
