Library to handle the X_NUCLEO_IHM02A1 Motor Control Expansion Board based on the L6470 component.
Fork of X_NUCLEO_IHM02A1 by
Diff: Components/l6470/l6470_class.h
- Revision:
- 1:b78dab6d2c58
- Parent:
- 0:92706998571a
- Child:
- 2:3ed992ffeada
--- a/Components/l6470/l6470_class.h Fri Nov 20 18:07:45 2015 +0000 +++ b/Components/l6470/l6470_class.h Wed Nov 25 11:59:37 2015 +0000 @@ -81,16 +81,37 @@ { public: + /*** Public Component Related Types ***/ + + /** + * @brief Prepared Actions. + */ + typedef enum + { + PREPARED_NO_ACTION = 0, + PREPARED_GET_POSITION, + PREPARED_GET_MARK, + PREPARED_GET_SPEED, + PREPARED_GET_MAX_SPEED, + PREPARED_GET_MIN_SPEED, + PREPARED_GET_ACCELERATION, + PREPARED_GET_DECELERATION, + PREPARED_GET_DIRECTION, + PREPARED_SET_MARK + } prepared_action_t; + + /*** Constructor and Destructor Methods ***/ /** * @brief Constructor. * @param flag_irq pin name of the FLAG pin of the component. + * @param busy_irq pin name of the BUSY pin of the component. * @param standby_reset pin name of the STBY\RST pin of the component. * @param ssel pin name of the SSEL pin of the SPI device to be used for communication. * @param spi SPI device to be used for communication. */ - L6470(PinName flag_irq, PinName standby_reset, PinName ssel, DevSPI &spi) : StepperMotor(), flag_irq(flag_irq), standby_reset(standby_reset), ssel(ssel), dev_spi(spi) + L6470(PinName flag_irq, PinName busy_irq, PinName standby_reset, PinName ssel, DevSPI &spi) : StepperMotor(), flag_irq(flag_irq), busy_irq(busy_irq), standby_reset(standby_reset), ssel(ssel), dev_spi(spi) { /* ACTION 4 ----------------------------------------------------------* * Initialize here the component's member variables, one variable per * @@ -107,6 +128,7 @@ L6470_Direction = &_L6470_Direction[0]; L6470_ACT = &_L6470_ACT[0]; pL6470_StatusRegister = &L6470_StatusRegister; + prepared_action = PREPARED_NO_ACTION; L6470_Id = number_of_devices++; L6470_DaisyChain_HalfPrepared = ZERO_F; memset(L6470_AppCmdPkg, 0, L6470DAISYCHAINSIZE * sizeof(sL6470_AppCmdPkg_t)); @@ -142,348 +164,888 @@ * return COMPONENT_EnableFeature(); * * } * *------------------------------------------------------------------------*/ + /** + * @brief Initializing the component. + * @param init Pointer to device specific initalization structure. + * @retval "0" in case of success, an error code otherwise. + */ virtual int Init(void *init) { return (int) L6470_Config((MICROSTEPPING_MOTOR_InitTypeDef *) init); } + /** + * @brief Getting the ID of the component. + * @param id Pointer to an allocated variable to store the ID into. + * @retval "0" in case of success, an error code otherwise. + */ virtual int ReadID(uint8_t *id) { return (int) 0; } + /** + * @brief Getting the status. + * @param None. + * @retval The status. + */ virtual unsigned int GetStatus(void) { return (unsigned int) L6470_GetStatus(); } - + + /** + * @brief Getting a parameter. + * @param parameter The parameter's identifier (or its register address). + * @retval The parameter's value. + */ virtual unsigned int GetParameter(unsigned int parameter) { return (unsigned int) L6470_GetParam((eL6470_RegId_t) parameter); } + /** + * @brief Getting the position. + * @param None. + * @retval The position. + */ virtual signed int GetPosition(void) { - return (signed int) L6470_GetParam((eL6470_RegId_t) L6470_ABS_POS_ID); + return (signed int) L6470_AbsPos_2_Position((uint32_t) L6470_GetParam((eL6470_RegId_t) L6470_ABS_POS_ID)); } + /** + * @brief Getting the marked position. + * @param None. + * @retval The marked position. + */ virtual signed int GetMark(void) { - return (signed int) L6470_GetParam((eL6470_RegId_t) L6470_MARK_ID); + return (signed int) L6470_AbsPos_2_Position((uint32_t) L6470_GetParam((eL6470_RegId_t) L6470_MARK_ID)); } + /** + * @brief Getting the current speed in pps. + * @param None. + * @retval The current speed in pps. + */ virtual unsigned int GetSpeed(void) { - return (unsigned int) L6470_GetParam((eL6470_RegId_t) L6470_SPEED_ID); + return round(L6470_Speed_2_Step_s((unsigned int) L6470_GetParam((eL6470_RegId_t) L6470_SPEED_ID))); } + /** + * @brief Getting the maximum speed in pps. + * @param None. + * @retval The maximum speed in pps. + */ virtual unsigned int GetMaxSpeed(void) { - return (unsigned int) L6470_GetParam((eL6470_RegId_t) L6470_MAX_SPEED_ID); + return round(L6470_MaxSpeed_2_Step_s((unsigned int) L6470_GetParam((eL6470_RegId_t) L6470_MAX_SPEED_ID))); } + /** + * @brief Getting the minimum speed in pps. + * @param None. + * @retval The minimum speed in pps. + */ virtual unsigned int GetMinSpeed(void) { - return (unsigned int) L6470_GetParam((eL6470_RegId_t) L6470_MIN_SPEED_ID); + return round(L6470_MinSpeed_2_Step_s((unsigned int) L6470_GetParam((eL6470_RegId_t) L6470_MIN_SPEED_ID))); } + /** + * @brief Getting the acceleration in pps^2. + * @param None. + * @retval The acceleration in pps^2. + */ virtual unsigned int GetAcceleration(void) { - return (unsigned int) L6470_GetParam((eL6470_RegId_t) L6470_ACC_ID); + return round(L6470_Acc_2_Step_s2((unsigned int) L6470_GetParam((eL6470_RegId_t) L6470_ACC_ID))); } + /** + * @brief Getting the deceleration in pps^2. + * @param None. + * @retval The deceleration in pps^2. + */ virtual unsigned int GetDeceleration(void) { - return (unsigned int) L6470_GetParam((eL6470_RegId_t) L6470_DEC_ID); + return round(L6470_Dec_2_Step_s2((unsigned int) L6470_GetParam((eL6470_RegId_t) L6470_DEC_ID))); } + /** + * @brief Getting the direction of rotation. + * @param None. + * @retval The direction of rotation. + */ virtual direction_t GetDirection(void) { return (direction_t) (L6470_CheckStatusRegisterFlag((eL6470_StatusRegisterFlagId_t) DIR_ID) == 1 ? StepperMotor::FWD : StepperMotor::BWD); } + /** + * @brief Setting a parameter. + * @param parameter The parameter's identifier (or its register address). + * @param value The parameter's value. + * @retval None. + */ virtual void SetParameter(unsigned int parameter, unsigned int value) { L6470_SetParam((eL6470_RegId_t) parameter, (uint32_t) value); } + /** + * @brief Setting the current position to be the home position. + * @param None. + * @retval None. + */ virtual void SetHome(void) { L6470_ResetPos(); - //L6470_SetParam((eL6470_RegId_t) L6470_ABS_POS_ID, (uint32_t) 0); } + /** + * @brief Setting the current position to be the marked position. + * @param None. + * @retval None. + */ virtual void SetMark(void) { L6470_SetParam((eL6470_RegId_t) L6470_MARK_ID, (uint32_t) L6470_GetParam((eL6470_RegId_t) L6470_ABS_POS_ID)); - //L6470_SetParam((eL6470_RegId_t) L6470_MARK_ID, (uint32_t) 0); } + /** + * @brief Setting the current position to be the marked position. + * @param position The given position. + * @retval None. + */ + virtual void SetMark(signed int position) + { + L6470_SetParam((eL6470_RegId_t) L6470_MARK_ID, (uint32_t) L6470_Position_2_AbsPos((int32_t) position)); + } + + /** + * @brief Setting the maximum speed in pps. + * @param speed The maximum speed in pps. + * @retval None. + */ virtual void SetMaxSpeed(unsigned int speed) { - L6470_SetParam((eL6470_RegId_t) L6470_MAX_SPEED_ID, (uint32_t) speed); + L6470_SetParam((eL6470_RegId_t) L6470_MAX_SPEED_ID, (uint32_t) L6470_Step_s_2_MaxSpeed((float) speed)); } + /** + * @brief Setting the minimum speed in pps. + * @param speed The minimum speed in pps. + * @retval None. + */ virtual void SetMinSpeed(unsigned int speed) { - L6470_SetParam((eL6470_RegId_t) L6470_MIN_SPEED_ID, (uint32_t) speed); + L6470_SetParam((eL6470_RegId_t) L6470_MIN_SPEED_ID, (uint32_t) L6470_Step_s_2_MinSpeed((float) speed)); } + /** + * @brief Setting the acceleration in pps^2. + * @param acceleration The acceleration in pps^2. + * @retval None. + */ virtual void SetAcceleration(unsigned int acceleration) { - L6470_SetParam((eL6470_RegId_t) L6470_ACC_ID, (uint32_t) acceleration); + L6470_SetParam((eL6470_RegId_t) L6470_ACC_ID, (uint32_t) L6470_Step_s2_2_Acc((float) acceleration)); } + /** + * @brief Setting the deceleration in pps^2. + * @param deceleration The deceleration in pps^2. + * @retval None. + */ virtual void SetDeceleration(unsigned int deceleration) { - L6470_SetParam((eL6470_RegId_t) L6470_DEC_ID, (uint32_t) deceleration); + L6470_SetParam((eL6470_RegId_t) L6470_DEC_ID, (uint32_t) L6470_Step_s2_2_Dec((float) deceleration)); } + /** + * @brief Going to a specified position through the shortest path. + * @param position The desired position. + * @retval None. + */ virtual void GoTo(signed int position) { - L6470_GoTo((uint32_t) position); + L6470_GoTo((uint32_t) L6470_Position_2_AbsPos((int32_t) position)); } + /** + * @brief Going to a specified position imposing the desired direction. + * @param position The desired position. + * @param direction The direction of rotation. + * @retval None. + */ virtual void GoTo(signed int position, direction_t direction) { - L6470_GoToDir((eL6470_DirId_t) (direction == StepperMotor::FWD ? L6470_DIR_FWD_ID : L6470_DIR_REV_ID), (signed int) position); + L6470_GoToDir((eL6470_DirId_t) (direction == StepperMotor::FWD ? L6470_DIR_FWD_ID : L6470_DIR_REV_ID), (uint32_t) L6470_Position_2_AbsPos((int32_t) position)); } + /** + * @brief Going to the home position. + * @param None. + * @retval None. + */ virtual void GoHome(void) { L6470_GoHome(); } + /** + * @brief Going to the marked position. + * @param None. + * @retval None. + */ virtual void GoMark(void) { L6470_GoMark(); } - virtual void GoUntil(eL6470_ActId_t L6470_ActId, direction_t direction, unsigned int speed) + /** + * @brief Running at the given speed imposing the desired direction until + * an external switch turn-on event occurs. + * @param action The identifier of the action about the absolute position. + * @param position The desired position. + * @param speed The speed value in pps. + * @retval None. + * @note The identifier of the action about the absolute position can be + * one of the following: + * + L6470_ACT_RST_ID: the absolute position is reset; + * + L6470_ACT_CPY_ID: the absolute position is set as the marked position. + */ + virtual void GoUntil(eL6470_ActId_t action, direction_t direction, unsigned int speed) { - L6470_GoUntil((eL6470_ActId_t) L6470_ActId, (eL6470_DirId_t) (direction == StepperMotor::FWD ? L6470_DIR_FWD_ID : L6470_DIR_REV_ID), (unsigned int) speed); + L6470_GoUntil((eL6470_ActId_t) action, (eL6470_DirId_t) (direction == StepperMotor::FWD ? L6470_DIR_FWD_ID : L6470_DIR_REV_ID), (unsigned int) L6470_Step_s_2_Speed((float) speed)); } + /** + * @brief Running towards a specified direction. + * @param direction The direction of rotation. + * @retval None. + */ virtual void Run(direction_t direction) { L6470_Run((eL6470_DirId_t) (direction == StepperMotor::FWD ? L6470_DIR_FWD_ID : L6470_DIR_REV_ID), (unsigned int) L6470_GetParam((eL6470_RegId_t) L6470_MAX_SPEED_ID)); } + /** + * @brief Running towards a specified direction at the given speed. + * @param direction The direction of rotation. + * @param speed The speed value in pps. + * @retval None. + */ virtual void Run(direction_t direction, unsigned int speed) { - L6470_Run((eL6470_DirId_t) (direction == StepperMotor::FWD ? L6470_DIR_FWD_ID : L6470_DIR_REV_ID), (unsigned int) speed); + L6470_Run((eL6470_DirId_t) (direction == StepperMotor::FWD ? L6470_DIR_FWD_ID : L6470_DIR_REV_ID), (unsigned int) L6470_Step_s_2_Speed((float) speed)); } + /** + * @brief Moving towards a specified direction for a certain number of steps. + * @param direction The direction of rotation. + * @param steps The desired number of steps. + * @retval None. + */ virtual void Move(direction_t direction, unsigned int steps) { L6470_Move((eL6470_DirId_t) (direction == StepperMotor::FWD ? L6470_DIR_FWD_ID : L6470_DIR_REV_ID), (unsigned int) steps); } + /** + * @brief Stopping the motor through an immediate deceleration up to zero speed. + * The used deceleration value is the one stored in the "DEC" register. + * @param None. + * @retval None. + */ virtual void SoftStop(void) { L6470_SoftStop(); } + /** + * @brief Stopping the motor through an immediate infinite deceleration. + * @param None. + * @retval None. + */ virtual void HardStop(void) { L6470_HardStop(); } + /** + * @brief Disabling the power bridge after performing a deceleration to zero. + * The used deceleration value is the one stored in the "DEC" register. + * @param None. + * @retval None. + */ + virtual void SoftHiZ(void) + { + L6470_SoftHiZ(); + } + + /** + * @brief Disabling the power bridge immediately. + * @param None. + * @retval None. + */ + virtual void HardHiZ(void) + { + L6470_HardHiZ(); + } + + /** + * @brief Waiting while the motor is active. + * @param None. + * @retval None. + */ virtual void WaitWhileActive(void) { while (L6470_CheckStatusRegisterFlag(BUSY_ID) == 0); } - virtual void SoftHiZ(void) - { - L6470_SoftHiZ(); - } - - virtual void HardHiZ(void) - { - L6470_HardHiZ(); - } - + /** + * @brief Switching to step-clock mode. + * @param direction The direction of rotation. + * @retval None. + */ virtual void StepClock(direction_t direction) { L6470_StepClock((eL6470_DirId_t) (direction == StepperMotor::FWD ? L6470_DIR_FWD_ID : L6470_DIR_REV_ID)); } - virtual void ReleaseSW(eL6470_ActId_t L6470_ActId, direction_t direction) + /** + * @brief Doing a motion at minimum speed imposing a specified direction + * until the SW is released. + * @param action The identifier of the action about the absolute position. + * @param direction The direction of rotation. + * @note The identifier of the action about the absolute position can be + * one of the following: + * + L6470_ACT_RST_ID: the absolute position is reset; + * + L6470_ACT_CPY_ID: the absolute position is set as the marked position. + */ + virtual void ReleaseSW(eL6470_ActId_t action, direction_t direction) { - L6470_ReleaseSW((eL6470_ActId_t) L6470_ActId, (eL6470_DirId_t) (direction == StepperMotor::FWD ? L6470_DIR_FWD_ID : L6470_DIR_REV_ID)); + L6470_ReleaseSW((eL6470_ActId_t) action, (eL6470_DirId_t) (direction == StepperMotor::FWD ? L6470_DIR_FWD_ID : L6470_DIR_REV_ID)); } + /** + * @brief Resetting the device to power-up conditions. + * @param None. + * @retval None. + */ virtual void ResetDevice(void) { L6470_ResetDevice(); } + /** + * @brief Preparing the command to get the status. + * @param None. + * @retval None. + * @note The command will be sent by issuing "PerformAction()". + */ virtual void PrepareGetStatus(void) { L6470_PrepareGetStatus(); + prepared_action = PREPARED_NO_ACTION; } + /** + * @brief Preparing the command to get a parameter. + * @param parameter The parameter's identifier (or its register address). + * @retval None. + * @note The command will be sent by issuing "PerformAction()". + */ virtual void PrepareGetParameter(unsigned int parameter) { L6470_PrepareGetParam((eL6470_RegId_t) parameter); + prepared_action = PREPARED_NO_ACTION; } + /** + * @brief Preparing the command to get the position. + * @param None. + * @retval None. + * @note The command will be sent by issuing "PerformAction()". + */ virtual void PrepareGetPosition(void) { L6470_PrepareGetParam((eL6470_RegId_t) L6470_ABS_POS_ID); + prepared_action = PREPARED_GET_POSITION; } + /** + * @brief Preparing the command to get the marked position. + * @param None. + * @retval None. + * @note The command will be sent by issuing "PerformAction()". + */ virtual void PrepareGetMark(void) { L6470_PrepareGetParam((eL6470_RegId_t) L6470_MARK_ID); + prepared_action = PREPARED_GET_MARK; } + /** + * @brief Preparing the command to get the current speed in pps. + * @param None. + * @retval None. + * @note The command will be sent by issuing "PerformAction()". + */ virtual void PrepareGetSpeed(void) { - L6470_GetParam((eL6470_RegId_t) L6470_SPEED_ID); + L6470_PrepareGetParam((eL6470_RegId_t) L6470_SPEED_ID); + prepared_action = PREPARED_GET_SPEED; } + /** + * @brief Preparing the command to get the maximum speed in pps. + * @param None. + * @retval None. + * @note The command will be sent by issuing "PerformAction()". + */ virtual void PrepareGetMaxSpeed(void) { L6470_PrepareGetParam((eL6470_RegId_t) L6470_MAX_SPEED_ID); + prepared_action = PREPARED_GET_MAX_SPEED; } + /** + * @brief Preparing the command to get the minimum speed in pps. + * @param None. + * @retval None. + * @note The command will be sent by issuing "PerformAction()". + */ virtual void PrepareGetMinSpeed(void) { L6470_PrepareGetParam((eL6470_RegId_t) L6470_MIN_SPEED_ID); + prepared_action = PREPARED_GET_MIN_SPEED; } + /** + * @brief Preparing the command to get the acceleration in pps^2. + * @param None. + * @retval None. + * @note The command will be sent by issuing "PerformAction()". + */ virtual void PrepareGetAcceleration(void) { L6470_PrepareGetParam((eL6470_RegId_t) L6470_ACC_ID); + prepared_action = PREPARED_GET_ACCELERATION; } + /** + * @brief Preparing the command to get the deceleration in pps^2. + * @param None. + * @retval None. + * @note The command will be sent by issuing "PerformAction()". + */ virtual void PrepareGetDeceleration(void) { L6470_PrepareGetParam((eL6470_RegId_t) L6470_DEC_ID); + prepared_action = PREPARED_GET_DECELERATION; } - virtual direction_t PrepareGetDirection(void) + /** + * @brief Preparing the command to get the direction of rotation. + * @param None. + * @retval None. + * @note The command will be sent by issuing "PerformAction()". + */ + virtual void PrepareGetDirection(void) { - return (direction_t) (L6470_CheckStatusRegisterFlag((eL6470_StatusRegisterFlagId_t) DIR_ID) == 1 ? StepperMotor::FWD : StepperMotor::BWD); + if (!L6470_DaisyChain_HalfPrepared) + { + /* To avoid deleting the previous entered command. */ + L6470_DaisyChain_HalfPrepared = ONE_F; + /* Resetting commands. */ + L6470_ResetAppCmdPkg(L6470_AppCmdPkg); + } + prepared_action = PREPARED_GET_DIRECTION; } + /** + * @brief Preparing the command to set a parameter. + * @param parameter The parameter's identifier (or its register address). + * @param value The parameter's value. + * @retval None. + * @note The command will be sent by issuing "PerformAction()". + */ virtual void PrepareSetParameter(unsigned int parameter, unsigned int value) { L6470_PrepareSetParam((eL6470_RegId_t) parameter, (uint32_t) value); + prepared_action = PREPARED_NO_ACTION; } + /** + * @brief Preparing the command to set the current position to be + * the home position. + * @param None. + * @retval None. + * @note The command will be sent by issuing "PerformAction()". + */ virtual void PrepareSetHome(void) { L6470_PrepareResetPos(); - //L6470_SetParam((eL6470_RegId_t) L6470_ABS_POS_ID, (uint32_t) 0); + prepared_action = PREPARED_NO_ACTION; } + /** + * @brief Preparing the command to set the current position to be + * the marked position. + * @param None. + * @retval None. + * @note The command will be sent by issuing "PerformAction()". + */ virtual void PrepareSetMark(void) { - uint32_t mark = L6470_GetParam((eL6470_RegId_t) L6470_ABS_POS_ID); - L6470_PrepareSetParam((eL6470_RegId_t) L6470_MARK_ID, mark); + /* + Set "0" now as marked position. This value will be replaced by the + actual position read at the time when the prepared actions will be + performed. + */ + L6470_PrepareSetParam((eL6470_RegId_t) L6470_MARK_ID, 0); + prepared_action = PREPARED_SET_MARK; } + /** + * @brief Preparing the command to set the given position to be + * the marked position. + * @param position The given position. + * @retval None. + * @note The command will be sent by issuing "PerformAction()". + */ + virtual void PrepareSetMark(signed int position) + { + L6470_PrepareSetParam((eL6470_RegId_t) L6470_MARK_ID, (uint32_t) L6470_Position_2_AbsPos((int32_t) position)); + prepared_action = PREPARED_NO_ACTION; + } + + /** + * @brief Preparing the command to set the current speed in pps. + * @param speed The current speed in pps. + * @retval None. + * @note The command will be sent by issuing "PerformAction()". + */ + virtual void PrepareSetSpeed(unsigned int speed) + { + L6470_PrepareSetParam((eL6470_RegId_t) L6470_SPEED_ID, (uint32_t) L6470_Step_s_2_Speed((float) speed)); + prepared_action = PREPARED_NO_ACTION; + } + + /** + * @brief Preparing the command to set the maximum speed in pps. + * @param speed The maximum speed in pps. + * @retval None. + * @note The command will be sent by issuing "PerformAction()". + */ virtual void PrepareSetMaxSpeed(unsigned int speed) { - L6470_PrepareSetParam((eL6470_RegId_t) L6470_MAX_SPEED_ID, (uint32_t) speed); - } - - virtual void PrepareSetMinSpeed(unsigned int speed) - { - L6470_PrepareSetParam((eL6470_RegId_t) L6470_MIN_SPEED_ID, (uint32_t) speed); + L6470_PrepareSetParam((eL6470_RegId_t) L6470_MAX_SPEED_ID, (uint32_t) L6470_Step_s_2_MaxSpeed((float) speed)); + prepared_action = PREPARED_NO_ACTION; } + /** + * @brief Preparing the command to set the minimum speed in pps. + * @param speed The minimum speed in pps. + * @retval None. + * @note The command will be sent by issuing "PerformAction()". + */ + virtual void PrepareSetMinSpeed(unsigned int speed) + { + L6470_PrepareSetParam((eL6470_RegId_t) L6470_MIN_SPEED_ID, (uint32_t) L6470_Step_s_2_MinSpeed((float) speed)); + prepared_action = PREPARED_NO_ACTION; + } + + /** + * @brief Preparing the command to set the acceleration in pps^2. + * @param acceleration The acceleration in pps^2. + * @retval None. + * @note The command will be sent by issuing "PerformAction()". + */ virtual void PrepareSetAcceleration(unsigned int acceleration) { - L6470_PrepareSetParam((eL6470_RegId_t) L6470_ACC_ID, (uint32_t) acceleration); - } - - virtual void PrepareSetDeceleration(unsigned int deceleration) - { - L6470_PrepareSetParam((eL6470_RegId_t) L6470_DEC_ID, (uint32_t) deceleration); + L6470_PrepareSetParam((eL6470_RegId_t) L6470_ACC_ID, (uint32_t) L6470_Step_s2_2_Acc((float) acceleration)); + prepared_action = PREPARED_NO_ACTION; } - virtual void PrepareGoTo(signed int position) + /** + * @brief Preparing the command to set the deceleration in pps^2. + * @param deceleration The deceleration in pps^2. + * @retval None. + * @note The command will be sent by issuing "PerformAction()". + */ + virtual void PrepareSetDeceleration(unsigned int deceleration) { - L6470_PrepareGoTo((uint32_t) position); + L6470_PrepareSetParam((eL6470_RegId_t) L6470_DEC_ID, (uint32_t) L6470_Step_s2_2_Dec((float) deceleration)); + prepared_action = PREPARED_NO_ACTION; } + /** + * @brief Preparing the command to go to a specified position. + * @param position The desired position. + * @retval None. + * @note The command will be sent by issuing "PerformAction()". + */ + virtual void PrepareGoTo(signed int position) + { + L6470_PrepareGoTo((uint32_t) L6470_Position_2_AbsPos((int32_t) position)); + prepared_action = PREPARED_NO_ACTION; + } + + /** + * @brief Preparing the command to go to a specified position + * imposing the desired direction. + * @param position The desired position. + * @param direction The direction of rotation. + * @retval None. + * @note The command will be sent by issuing "PerformAction()". + */ virtual void PrepareGoTo(signed int position, direction_t direction) { - L6470_PrepareGoToDir((eL6470_DirId_t) (direction == StepperMotor::FWD ? L6470_DIR_FWD_ID : L6470_DIR_REV_ID), (signed int) position); + L6470_PrepareGoToDir((eL6470_DirId_t) (direction == StepperMotor::FWD ? L6470_DIR_FWD_ID : L6470_DIR_REV_ID), (uint32_t) L6470_Position_2_AbsPos((int32_t) position)); + prepared_action = PREPARED_NO_ACTION; } + /** + * @brief Preparing the command to go to the home position. + * @param None. + * @retval None. + * @note The command will be sent by issuing "PerformAction()". + */ virtual void PrepareGoHome(void) { L6470_PrepareGoHome(); + prepared_action = PREPARED_NO_ACTION; } + /** + * @brief Preparing the command to go to the marked position. + * @param None. + * @retval None. + * @note The command will be sent by issuing "PerformAction()". + */ virtual void PrepareGoMark(void) { L6470_PrepareGoMark(); + prepared_action = PREPARED_NO_ACTION; } + /** + * @brief Preparing the command to run at the given speed imposing the desired + * direction until an external switch turn-on event occurs. + * @param action The identifier of the action about the absolute position. + * @param position The desired position. + * @param speed The speed value in pps. + * @retval None. + * @note The command will be sent by issuing "PerformAction()". + * The identifier of the action about the absolute position can be + * one of the following: + * + L6470_ACT_RST_ID: the absolute position is reset; + * + L6470_ACT_CPY_ID: the absolute position is set as the marked position. + */ virtual void PrepareGoUntil(eL6470_ActId_t L6470_ActId, direction_t direction, unsigned int speed) { - L6470_PrepareGoUntil((eL6470_ActId_t) L6470_ActId, (eL6470_DirId_t) (direction == StepperMotor::FWD ? L6470_DIR_FWD_ID : L6470_DIR_REV_ID), (unsigned int) speed); + L6470_PrepareGoUntil((eL6470_ActId_t) L6470_ActId, (eL6470_DirId_t) (direction == StepperMotor::FWD ? L6470_DIR_FWD_ID : L6470_DIR_REV_ID), (unsigned int) L6470_Step_s_2_Speed((float) speed)); + prepared_action = PREPARED_NO_ACTION; } + /** + * @brief Preparing the command to run towards a specified direction + * at the maximum speed. + * @param direction The direction of rotation. + * @retval None. + * @note The command will be sent by issuing "PerformAction()". + */ virtual void PrepareRun(direction_t direction) { L6470_PrepareRun((eL6470_DirId_t) (direction == StepperMotor::FWD ? L6470_DIR_FWD_ID : L6470_DIR_REV_ID), (unsigned int) L6470_GetParam((eL6470_RegId_t) L6470_MAX_SPEED_ID)); + prepared_action = PREPARED_NO_ACTION; } + /** + * @brief Preparing the command to run towards a specified direction + * at the given speed. + * @param direction The direction of rotation. + * @param speed The speed value in pps. + * @retval None. + * @note The command will be sent by issuing "PerformAction()". + */ virtual void PrepareRun(direction_t direction, unsigned int speed) { - L6470_PrepareRun((eL6470_DirId_t) (direction == StepperMotor::FWD ? L6470_DIR_FWD_ID : L6470_DIR_REV_ID), (unsigned int) speed); + L6470_PrepareRun((eL6470_DirId_t) (direction == StepperMotor::FWD ? L6470_DIR_FWD_ID : L6470_DIR_REV_ID), (unsigned int) L6470_Step_s_2_Speed((float) speed)); + prepared_action = PREPARED_NO_ACTION; } + /** + * @brief Preparing the command to move towards a specified direction + * for a certain number of steps. + * @param direction The direction of rotation. + * @param steps The desired number of steps. + * @retval None. + * @note The command will be sent by issuing "PerformAction()". + */ virtual void PrepareMove(direction_t direction, unsigned int steps) { L6470_PrepareMove((eL6470_DirId_t) (direction == StepperMotor::FWD ? L6470_DIR_FWD_ID : L6470_DIR_REV_ID), (unsigned int) steps); + prepared_action = PREPARED_NO_ACTION; } + /** + * @brief Preparing the command to stop the motor. + * @param None. + * @retval None. + * @note The command will be sent by issuing "PerformAction()". + */ virtual void PrepareSoftStop(void) { L6470_PrepareSoftStop(); + prepared_action = PREPARED_NO_ACTION; } + /** + * @brief Preparing the command to stop the motor and disabling the power bridge. + * @param None. + * @retval None. + * @note The command will be sent by issuing "PerformAction()". + */ virtual void PrepareHardStop(void) { L6470_PrepareHardStop(); + prepared_action = PREPARED_NO_ACTION; } + /** + * @brief Preparing the command to disable the power bridge after performing + * a deceleration to zero. + * The used deceleration value is the one stored in the "DEC" register. + * @param None. + * @retval None. + * @note The command will be sent by issuing "PerformAction()". + */ virtual void PrepareSoftHiZ(void) { L6470_PrepareSoftHiZ(); + prepared_action = PREPARED_NO_ACTION; } + /** + * @brief Preparing the command to disable the power bridge immediately. + * @param None. + * @retval None. + * @note The command will be sent by issuing "PerformAction()". + */ virtual void PrepareHardHiZ(void) { L6470_PrepareHardHiZ(); + prepared_action = PREPARED_NO_ACTION; + } + + /** + * @brief Preparing the command to switch to step-clock mode. + * @param direction The direction of rotation. + * @retval None. + * @note The command will be sent by issuing "PerformAction()". + */ + virtual void PrepareStepClock(direction_t direction) + { + L6470_PrepareStepClock((eL6470_DirId_t) (direction == StepperMotor::FWD ? L6470_DIR_FWD_ID : L6470_DIR_REV_ID)); + prepared_action = PREPARED_NO_ACTION; } - virtual void PrepareStepClock(direction_t direction) + /** + * @brief Preparing the command to do a motion at minimum speed + * imposing a specified direction until the SW is released. + * @param action The identifier of the action about the absolute position. + * @param direction The direction of rotation. + * @retval None. + * @note The command will be sent by issuing "PerformAction()". + * The identifier of the action about the absolute position can be + * one of the following: + * + L6470_ACT_RST_ID: the absolute position is reset; + * + L6470_ACT_CPY_ID: the absolute position is set as the marked position. + */ + virtual void PrepareReleaseSW(eL6470_ActId_t action, direction_t direction) { - L6470_StepClock((eL6470_DirId_t) (direction == StepperMotor::FWD ? L6470_DIR_FWD_ID : L6470_DIR_REV_ID)); + L6470_PrepareReleaseSW((eL6470_ActId_t) action, (eL6470_DirId_t) (direction == StepperMotor::FWD ? L6470_DIR_FWD_ID : L6470_DIR_REV_ID)); + prepared_action = PREPARED_NO_ACTION; + } + + /** + * @brief Preparing the command to reset the device to power-up conditions. + * @param None. + * @retval None. + * @note The command will be sent by issuing "PerformAction()". + */ + virtual void PrepareResetDevice(void) + { + L6470_PrepareResetDevice(); + prepared_action = PREPARED_NO_ACTION; } - virtual void PrepareReleaseSW(eL6470_ActId_t L6470_ActId, direction_t direction) + /** + * @brief Performing all the actions prepared on the components + * of the daisy-chain. + * @param None. + * @retval The raw data returned by the components. + */ + uint8_t* PerformPreparedActions(void) { - L6470_ReleaseSW((eL6470_ActId_t) L6470_ActId, (eL6470_DirId_t) (direction == StepperMotor::FWD ? L6470_DIR_FWD_ID : L6470_DIR_REV_ID)); + return L6470_PerformPreparedApplicationCommand(); + } + + /** + * @brief Getting the prepared action. + * @param None + * @retval The prepared action. + */ + prepared_action_t GetPreparedAction(void) + { + return prepared_action; } - virtual void PrepareResetDevice(void) + /** + * @brief Converting the raw data received by the component according to + * the action performed on it. + * @param raw_data The received raw data. + * @retval The result of the action performed. + */ + int32_t GetResult(uint8_t *raw_data) { - L6470_ResetDevice(); - } + switch (prepared_action) + { + case PREPARED_GET_POSITION: + return L6470_AbsPos_2_Position(L6470_ExtractReturnedData(raw_data, L6470_Register[L6470_ABS_POS_ID].LengthByte)); + + case PREPARED_GET_MARK: + return L6470_AbsPos_2_Position(L6470_ExtractReturnedData(raw_data, L6470_Register[L6470_MARK_ID].LengthByte)); - virtual uint8_t* PerformAction(void) - { - return (uint8_t*) L6470_PerformPreparedApplicationCommand(); + case PREPARED_GET_SPEED: + return round(L6470_Speed_2_Step_s(L6470_ExtractReturnedData(raw_data, L6470_Register[L6470_SPEED_ID].LengthByte))); + + case PREPARED_GET_MAX_SPEED: + return round(L6470_MaxSpeed_2_Step_s(L6470_ExtractReturnedData(raw_data, L6470_Register[L6470_MAX_SPEED_ID].LengthByte))); + + case PREPARED_GET_MIN_SPEED: + return round(L6470_MinSpeed_2_Step_s(L6470_ExtractReturnedData(raw_data, L6470_Register[L6470_MIN_SPEED_ID].LengthByte))); + + case PREPARED_GET_ACCELERATION: + return round(L6470_Acc_2_Step_s2(L6470_ExtractReturnedData(raw_data, L6470_Register[L6470_ACC_ID].LengthByte))); + + case PREPARED_GET_DECELERATION: + return round(L6470_Dec_2_Step_s2(L6470_ExtractReturnedData(raw_data, L6470_Register[L6470_DEC_ID].LengthByte))); + + case PREPARED_GET_DIRECTION: + return (int32_t) (direction_t) (L6470_CheckStatusRegisterFlag((eL6470_StatusRegisterFlagId_t) DIR_ID) == 1 ? StepperMotor::FWD : StepperMotor::BWD); + + default: + case PREPARED_NO_ACTION: + return 0; + } } @@ -519,8 +1081,8 @@ * } * *------------------------------------------------------------------------*/ /** - * @brief Attaching a FLAG interrupt handler. - * @param fptr an interrupt handler. + * @brief Attaching an interrupt handler to the FLAG interrupt. + * @param fptr An interrupt handler. * @retval None. */ void AttachFlagIRQ(void (*fptr)(void)) @@ -529,7 +1091,7 @@ } /** - * @brief Enabling FLAG interrupt handling. + * @brief Enabling the FLAG interrupt handling. * @param None. * @retval None. */ @@ -539,7 +1101,7 @@ } /** - * @brief Disabling FLAG interrupt handling. + * @brief Disabling the FLAG interrupt handling. * @param None. * @retval None. */ @@ -548,6 +1110,36 @@ flag_irq.disable_irq(); } + /** + * @brief Attaching an interrupt handler to the BUSY interrupt. + * @param fptr An interrupt handler. + * @retval None. + */ + void AttachBusyIRQ(void (*fptr)(void)) + { + busy_irq.rise(fptr); + } + + /** + * @brief Enabling the BUSY interrupt handling. + * @param None. + * @retval None. + */ + void EnableBusyIRQ(void) + { + busy_irq.enable_irq(); + } + + /** + * @brief Disabling the BUSY interrupt handling. + * @param None. + * @retval None. + */ + void DisableBusyIRQ(void) + { + busy_irq.disable_irq(); + } + protected: @@ -641,6 +1233,19 @@ void L6470_PrepareAppCmdPkg(sL6470_AppCmdPkg_t* pL6470_AppCmdPkg, eL6470_AppCmdId_t L6470_AppCmdId, uint32_t p1, uint32_t p2, uint32_t p3); void L6470_PrepareDaisyChainCommand(sL6470_AppCmdPkg_t* pL6470_AppCmdPkg, uint8_t* pL6470_DaisyChainSpiTxStruct); + /** + * @brief Rounding a floating point number to the nearest unsigned integer number. + * @param f The floating point number. + * @retval The nearest unsigned integer number. + */ + int round(float f) + { + if (f >= 0) + return (int) f + (f - (int) f < 0.5f ? 0 : 1); + else + return (int) f - (f - (int) f < -0.5f ? 1 : 0); + } + /*** Component's I/O Methods ***/ @@ -718,49 +1323,63 @@ /*** Component's Instance Variables ***/ - /* Identity */ - uint8_t who_am_i; - /* ACTION 9 --------------------------------------------------------------* - * There should be only a unique identifier for each component, which * - * should be the "who_am_i" parameter, hence this parameter is optional. * + * Declare here interrupt related variables, if needed. * + * Note that interrupt handling is platform dependent, see * + * "Interrupt Related Methods" above. * + * * + * Example: * + * + mbed: * + * InterruptIn feature_irq; * *------------------------------------------------------------------------*/ - /* Type. */ - uint8_t type; - /* Flag Interrupt. */ InterruptIn flag_irq; + /* Busy Interrupt. */ + InterruptIn busy_irq; + + /* ACTION 10 -------------------------------------------------------------* + * Declare here other pin related variables, if needed. * + * * + * Example: * + * + mbed: * + * DigitalOut standby_reset; * + *------------------------------------------------------------------------*/ /* Standby/reset pin. */ DigitalOut standby_reset; + /* ACTION 11 -------------------------------------------------------------* + * Declare here communication related variables, if needed. * + * * + * Example: * + * + mbed: * + * DigitalOut ssel; * + * DevSPI &dev_spi; * + *------------------------------------------------------------------------*/ /* Configuration. */ DigitalOut ssel; /* IO Device. */ DevSPI &dev_spi; - /* Interrupts. */ - /* ACTION 10 -------------------------------------------------------------* - * Put here interrupt related objects, if needed. * - * Note that interrupt handling is platform dependent, see * - * "Interrupt Related Methods" above. * + /* ACTION 12 -------------------------------------------------------------* + * Declare here identity related variables, if needed. * + * Note that there should be only a unique identifier for each component, * + * which should be the "who_am_i" parameter. * + *------------------------------------------------------------------------*/ + /* Identity */ + uint8_t who_am_i; + + /* ACTION 13 -------------------------------------------------------------* + * Declare here the component's static and non-static data, one variable * + * per line. * * * * Example: * - * + mbed: * - * InterruptIn feature_int; * + * float measure; * + * int instance_id; * + * static int number_of_instances; * *------------------------------------------------------------------------*/ - /* Data. */ - /* ACTION 11 -------------------------------------------------------------* - * Declare here the component's data, one variable per line. * - * * - * Example: * - * int T0_out; * - * int T1_out; * - * float T0_degC; * - * float T1_degC; * - *------------------------------------------------------------------------*/ uint8_t L6470_Id; const sL6470_Register_t *L6470_Register; const sL6470_ApplicationCommand_t *L6470_ApplicationCommand; @@ -769,6 +1388,7 @@ sL6470_StatusRegister_t L6470_StatusRegister; sL6470_StatusRegister_t *pL6470_StatusRegister; StepperMotorRegister_t StepperMotorRegister; + prepared_action_t prepared_action; /* Static data. */ static uint8_t number_of_devices;