Simple program featuring a few API functions usage of the X_NUCLEO_IHM03A1 library.
Dependencies: X_NUCLEO_IHM03A1 mbed
Fork of HelloWorld_IHM03A1 by
This application provides a simple example of usage of the X-NUCLEO-IHM03A1 High Power Stepper Motor Control Expansion Board.
It shows how to use one stepper motor connected to the board by:
- moving the rotor a specific number of steps or to a specific position, with a given speed value, direction of rotation;
- monitoring the motor status;
- handling an interrupt triggered by the motor driver;
- getting and setting a motor driver parameter.
For the hardware configuration of the expansion board, please refer to the X_NUCLEO_IHM03A1 home web page.
More API usage is available in IHM03A1_ExampleFor1Motor and IHM03A1_ExampleFor3Motors programs.
Diff: main.cpp
- Revision:
- 2:f20ed233a489
- Parent:
- 0:c44075c08a18
- Child:
- 5:f5ceb23e3590
--- a/main.cpp Thu Apr 14 08:53:04 2016 +0000 +++ b/main.cpp Fri Mar 24 11:00:20 2017 +0100 @@ -45,13 +45,13 @@ #include "DevSPI.h" /* Component specific header files. */ -#include "powerstep01_class.h" +#include "PowerStep01.h" /* Variables -----------------------------------------------------------------*/ /* Initialization parameters of the motor connected to the expansion board. */ /* Current mode. */ -powerstep01_Init_u_t initDeviceParameters = +powerstep01_init_u_t init = { /* common parameters */ .cm.cp.cmVmSelection = POWERSTEP01_CM_VM_CURRENT, // enum powerstep01_CmVm_t @@ -99,7 +99,7 @@ }; /* Motor Control Component. */ -POWERSTEP01 *motor; +PowerStep01 *motor; /* Functions -----------------------------------------------------------------*/ @@ -108,51 +108,43 @@ * @param None * @retval None * @note If needed, implement it, and then attach and enable it: - * + motor->AttachFlagIRQ(&myFlagIRQHandler); - * + motor->EnableFlagIRQ(); + * + motor->attach_flag_irq(&my_flag_irq_handler); + * + motor->enable_flag_irq(); * To disable it: * + motor->DisbleFlagIRQ(); */ -void myFlagIRQHandler(void) +void my_flag_irq_handler(void) { /* Set ISR flag. */ motor->isrFlag = TRUE; /* Get the value of the status register. */ - unsigned int statusRegister = motor->GetStatus(); + unsigned int statusRegister = motor->get_status(); printf(" WARNING: \"FLAG\" interrupt triggered.\r\n"); /* Check SW_F flag: if not set, the SW input is opened */ - if ((statusRegister & POWERSTEP01_STATUS_SW_F )!=0) - { - printf(" SW closed (connected to ground).\r\n"); + if ((statusRegister & POWERSTEP01_STATUS_SW_F ) != 0) { + printf(" SW closed (connected to ground).\r\n"); } /* Check SW_EN bit */ - if ((statusRegister & POWERSTEP01_STATUS_SW_EVN)== - POWERSTEP01_STATUS_SW_EVN) - { - printf(" SW turn_on event.\r\n"); + if ((statusRegister & POWERSTEP01_STATUS_SW_EVN) == POWERSTEP01_STATUS_SW_EVN) { + printf(" SW turn_on event.\r\n"); } /* Check Command Error flag: if set, the command received by SPI can't be */ /* performed. This occurs for instance when a move command is sent to the */ /* Powerstep01 while it is already running */ - if ((statusRegister & POWERSTEP01_STATUS_CMD_ERROR)== - POWERSTEP01_STATUS_CMD_ERROR) - { + if ((statusRegister & POWERSTEP01_STATUS_CMD_ERROR) == POWERSTEP01_STATUS_CMD_ERROR) { printf(" Non-performable command detected.\r\n"); } /* Check UVLO flag: if not set, there is an undervoltage lock-out */ - if ((statusRegister & POWERSTEP01_STATUS_UVLO)==0) - { - printf(" undervoltage lock-out.\r\n"); + if ((statusRegister & POWERSTEP01_STATUS_UVLO)==0) { + printf(" undervoltage lock-out.\r\n"); } /* Check thermal STATUS flags: if set, the thermal status is not normal */ - if ((statusRegister & POWERSTEP01_STATUS_TH_STATUS)!=0) - { + if ((statusRegister & POWERSTEP01_STATUS_TH_STATUS)!=0) { //thermal status: 1: Warning, 2: Bridge shutdown, 3: Device shutdown printf(" Thermal status: %d.\r\n", (statusRegister & POWERSTEP01_STATUS_TH_STATUS)>>11); } /* Check OCD flag: if not set, there is an overcurrent detection */ - if ((statusRegister & POWERSTEP01_STATUS_OCD)==0) - { + if ((statusRegister & POWERSTEP01_STATUS_OCD)==0) { printf(" Overcurrent detection.\r\n"); } /* Reset ISR flag. */ @@ -164,16 +156,15 @@ * @param[in] error Number of the error * @retval None * @note If needed, implement it, and then attach it: - * + motor->AttachErrorHandler(&myErrorHandler); + * + motor->attach_error_handler(&my_error_handler); */ -void myErrorHandler(uint16_t error) +void my_error_handler(uint16_t error) { /* Printing to the console. */ printf("Error %d detected\r\n\n", error); /* Infinite loop */ - while(1) - { + while (true) { } } @@ -193,25 +184,27 @@ DevSPI dev_spi(D11, D12, D13); /* Initializing Motor Control Component. */ - motor = new POWERSTEP01(D2, D4, D8, D9, D10, dev_spi); - if (motor->Init(&initDeviceParameters) != COMPONENT_OK) exit(EXIT_FAILURE); + motor = new PowerStep01(D2, D4, D8, D9, D10, dev_spi); + if (motor->init(&init) != COMPONENT_OK) { + exit(EXIT_FAILURE); + } /* Attaching and enabling an interrupt handler. */ - motor->AttachFlagIRQ(&myFlagIRQHandler); - motor->EnableFlagIRQ(); + motor->attach_flag_irq(&my_flag_irq_handler); + motor->enable_flag_irq(); /* Attaching an error handler */ - motor->AttachErrorHandler(&myErrorHandler); + motor->attach_error_handler(&my_error_handler); /* Printing to the console. */ printf("Motor Control Application Example for 1 Motor\r\n"); -//----- Move of 16000 steps in the FW direction +//----- move of 16000 steps in the FW direction printf("--> Moving forward 16000 steps.\r\n"); - motor->Move(StepperMotor::FWD, 16000); + motor->move(StepperMotor::FWD, 16000); /* Waiting while the motor is active. */ - motor->WaitWhileActive(); + motor->wait_while_active(); /* Wait for 2 seconds */ wait_ms(2000); @@ -220,13 +213,13 @@ printf("--> Go to position -6400 steps.\r\n"); /* Request device to go to position -6400 */ - motor->GoTo(-6400); + motor->go_to(-6400); /* Wait for the motor ends moving */ - motor->WaitWhileActive(); + motor->wait_while_active(); /* Get current position of device and print to the console */ - printf(" Position: %d.\r\n", motor->GetPosition()); + printf(" Position: %d.\r\n", motor->get_position()); /* Wait for 2 seconds */ wait_ms(2000); @@ -236,34 +229,33 @@ printf("--> Go to home position.\r\n"); /* Request device to go to Home */ - motor->GoHome(); + motor->go_home(); /* Wait for the motor ends moving */ - motor->WaitWhileActive(); + motor->wait_while_active(); /* Wait for 2 seconds */ wait_ms(2000); -//----- Run the motor BACKWARD at 400 step/s - printf("--> Run the motor backward at 400 step/s.\r\n"); - motor->Run(StepperMotor::BWD,400); +//----- run the motor BACKWARD at 400 step/s + printf("--> run the motor backward at 400 step/s.\r\n"); + motor->run(StepperMotor::BWD,400); //----- Get parameter example /* Wait for device reaches the targeted speed */ - while((motor->ReadStatusRegister() & POWERSTEP01_STATUS_MOT_STATUS)!= - POWERSTEP01_STATUS_MOT_STATUS_CONST_SPD); + while ((motor->read_status_register() & POWERSTEP01_STATUS_MOT_STATUS) != POWERSTEP01_STATUS_MOT_STATUS_CONST_SPD); /* Record the reached speed in step/s and print to the console */ - printf(" Reached Speed: %f step/s.\r\n", motor->GetAnalogValue(POWERSTEP01_SPEED)); + printf(" Reached Speed: %f step/s.\r\n", motor->get_analog_value(POWERSTEP01_SPEED)); //----- Soft stopped required while running printf("--> Soft stop requested.\r\n"); /* Request a soft stop of device and keep the power bridges enabled */ - motor->SoftHiZ(); + motor->soft_hiz(); /* Wait for the motor of device ends moving */ - motor->WaitWhileActive(); + motor->wait_while_active(); /* Wait for 2 seconds */ wait_ms(2000); @@ -271,33 +263,32 @@ //----- Read inexistent register printf("--> Try to read an inexistent register.\r\n"); /* Trying to read an inexistent register */ - /* triggers the flag interrupt and call the myFlagIRQHandler */ - motor->GetRawParameter(0x1F); + /* triggers the flag interrupt and call the my_flag_irq_handler */ + motor->get_raw_parameter(0x1F); //----- Changing a motor setting printf("--> Set the reference voltage assigned to the torque regulation during acceleration to 500mV.\r\n"); - motor->SetRawParameter(POWERSTEP01_TVAL_ACC, POWERSTEP01::Tval_RefVoltage_to_RegVal(500)); + motor->set_raw_parameter(POWERSTEP01_TVAL_ACC, PowerStep01::t_val_ref_voltage_to_reg_val(500)); //Alternatively, the register value can be written directly as shown below: - //motor->SetRawParameter(POWERSTEP01_TVAL_ACC, 63); + //motor->set_raw_parameter(POWERSTEP01_TVAL_ACC, 63); //Print the new parameter value to the console - printf("--> New TVAL_ACC register value is 0x%x .\r\n",motor->GetRawParameter(POWERSTEP01_TVAL_ACC)); + printf("--> New TVAL_ACC register value is 0x%x .\r\n",motor->get_raw_parameter(POWERSTEP01_TVAL_ACC)); /* Infinite Loop. */ printf("--> Infinite Loop...\r\n"); - while (1) - { + while (true) { /* Request device to go position -6400 */ - motor->GoTo(-6400); + motor->go_to(-6400); /* Waiting while the motor is active. */ - motor->WaitWhileActive(); + motor->wait_while_active(); /* Request device to go position 6400 */ - motor->GoTo(6400); + motor->go_to(6400); /* Waiting while the motor is active. */ - motor->WaitWhileActive(); + motor->wait_while_active(); } } /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/