My Version of the Crealab MotorLib.

Fork of MotorLib by CreaLab

Embed: (wiki syntax)

« Back to documentation index

CreaMot Class Reference

CreaMot Class Reference

ATTENTION UNDER CONSTRUCTION, DO NOT YET USE. More...

#include <CreaMot.h>

Public Member Functions

 CreaMot (PinName _MPh[4])
 CreaMot Class Creator.
 CreaMot (PinName _MPh0, PinName _MPh1, PinName _MPh2, PinName _MPh3)
 CreaMot Class Creator.
 CreaMot (PinName _MPh0, PinName _MPh1, PinName _MPh2, PinName _MPh3, uint32_t AStepTime_us)
 CreaMot Class Creator.
void callbackSet (void(*CBfunction)(void))
 Attach a basic Callback function.
template<typename T >
void callbackSet (T *object, void(T::*CBmember)(void))
 Attach a Callback function, member of a class.
void callbackRemove ()
 Remove the Callback function that may have been attached previously.
void RunDist_cm (bool AClockWise, float Dist_cm)
 High Level: Run CreaMot for a given number of centimeters.
void RunDist_cm (float Dist_cm)
 High Level: Run CreaMot for a given number of centimeters in default direction.
void RunTurnAngle (float turn_angle_deg, float turn_radius_cm)
 High Level: Run CreaMot for a turn angle around a turn_radius_cm in default direction.
void setDiamCM (float Adiam_cm)
 Additional geometric information: set the wheel diameter, also sets perimeter and degrees per cm.
void setSpeed_cm_sec (float speed_cm_sec)
 Set CreaMot speed in centimeter/sec, based on perimeter in cm.
void RunDegrees (bool AClockWise, float angle_deg)
 High Level: Run CreaMot for a given angle.
void RunDegrees (float angle_deg)
 High Level: Run CreaMot for a given angle in default direction for details see RunDegrees (bool AClockWise, float angle_deg);.
void RunSteps (bool AClockWise, int32_t Nsteps)
 High Level: Run CreaMot for a number of Steps.
void RunInfinite (bool AClockWise)
 High Level: Run CreaMot "unlimited".
void PauseRun ()
 High Level: Pause a CreaMot Run.
void RestartRun ()
 High Level: Restart a Paused Run.
void StopRun ()
 High Level: End any Run.
uint32_t getStepsFullTurn ()
 MidLevel: Get number of Steps per Full turn.
void setStepsFullTurn (uint32_t StepsFullTurn)
 MidLevel: Set number of Steps per Full turn.
uint32_t getStepTime_us ()
 Mid Level: Get the CreaMot step time.
void setStepTime_us (uint32_t AStepTime_us)
 Set the time in microseconds between two CreaMot steps.
void setRotationPeriodSec (float Seconds_Per_Turn)
 Set the time in seconds to get one full turn, rotation of 360°.
void MotorTest ()
 Low Level: Run one full turn clockwise then anticlockwise.
void MotorOFF ()
 Low Level: turn off all CreaMot Phases No more current flows, reduces holding force.
void MotorON ()
 Low Level: turn on the CreaMot Phases in the last used phase.
void StepOnce ()
 Low Level: Advance CreaMot one step, rotates in direction of variable AClockWise.
void StepCCW ()
 Low Level: Advance CreaMot one step, rotates CounterClockwise.
void StepClkW ()
 Low Level: Advance CreaMot one step, rotates Clockwise.
void MotorZero ()
 Low Level: turn on the CreaMot Phases in Zero Position.

Data Fields

bool defaultDirection
 Default rotation direction that serves as local storage, but not as actual direction.
char StateValue
 State value that is used and managed by the class owner.
motStates CurrState
 General state that the CreaMot state machine is in.
motCmands CurrCmd
 Command asked to be executed currently by the state machine.

Detailed Description

ATTENTION UNDER CONSTRUCTION, DO NOT YET USE.

Class of a Four Phase Stepper CreaMot.

Perform Runs for number of steps, or given amount angle, but also Low-Level steps.

High-Level Run functions have 'Run' in their name. They rely on tickers and return immediately after ticker is set up. A state-machine evaluates the one ongoing command versus the CreaMot state at every tick. When End of Run is detected tickers stop, and CreaMot turns off.

To define the speed of the CreaMot set the variable StepTime_us, either by a) Using the createor CreaMot(...., uint32_t AStepTime_us); b) Calling function setStepTime_us(uint32_t AStepTime_us); any time c) or leave it to the default value MOTOR_STEP_TIME_DEFAULT_US = 5000

To be able to run the CreaMot for a given angle, set the number of steps per full turn with the function "setStepsFullTurn" That value defaults to MOTOR_STEPS_FOR_A_TURN = 4096

To be able to run the CreaMot for a given perimeter distance in centimeters, set the wheel diameter with the function setDiamCM( float Adiam_cm);

Callbacks can be attached to react to 'end of run' events.

Attention: the attached Callback is called within a Ticker Callback. Your code you execute in the Callback should be short, must not use waits, or any long routines. Do not call any CreaMot run commands in the callback, as it creates conflict situations. Long Callback code may impair this and any other Ticker functions that run in your application.

Low-Level functions directly talk to the hardware without ticker. Use of Low-Level functions while tickers still run may lead to unexpected behavior.

NB: all times are uint32_t, step numbers are int32_t

Definition at line 98 of file CreaMot.h.


Constructor & Destructor Documentation

CreaMot ( PinName  _MPh[4] )

CreaMot Class Creator.

Creates the class, initiallizes all fields, creates Phase Pins. Time between two steps defaults here to MOTOR_STEP_TIME_DEFAULT_US = 5000usec. Pin names are used to create digital outputs: Pin0 = new DigitalOut(_MPh0)

       PinName MotPhases[] = {PB_1, PB_15, PB_14, PB_13};
       CreaMot MotorName(MotPhases); // Call this creator for example like this:  
Parameters:
_MPhArray of Names of the 4 Digital Pins of type PinNames

Definition at line 5 of file CreaMot.cpp.

CreaMot ( PinName  _MPh0,
PinName  _MPh1,
PinName  _MPh2,
PinName  _MPh3 
)

CreaMot Class Creator.

Creates the class, initiallizes all fields, creates Phase Pins. Time between two steps defaults here to MOTOR_STEP_TIME_DEFAULT_US=5000usec. Pin names are used to create digital outputs: Pin0 = new DigitalOut(_MPh0)

      // Call this creator for example like this:
       CreaMot MotorName(PB_1, PB_15, PB_14, PB_13);
Parameters:
<_MPh0,_MPh1,_MPh2,_MPh3>List of Names of the 4 Digital Pins of type PinNames

Definition at line 9 of file CreaMot.cpp.

CreaMot ( PinName  _MPh0,
PinName  _MPh1,
PinName  _MPh2,
PinName  _MPh3,
uint32_t  AStepTime_us 
)

CreaMot Class Creator.

Creates the class, initiallizes all fields, creates Phase Pins. Time between two steps is passed as parameter. Pin names are used to create digital outputs: Pin0 = new DigitalOut(_MPh0)

      // Call this creator for example like this:
       CreaMot MotorName(PB_1, PB_15, PB_14, PB_13, 6000);
Parameters:
<_MPh0,_MPh1,_MPh2,_MPh3>List of Names of the 4 Digital Pins of type PinNames
<AStepTime_us>the time in usec between two steps, thats used initially.

Definition at line 14 of file CreaMot.cpp.


Member Function Documentation

void callbackRemove (  )

Remove the Callback function that may have been attached previously.

Definition at line 212 of file CreaMot.h.

void callbackSet ( T *  object,
void(T::*)(void)  CBmember 
)

Attach a Callback function, member of a class.

Only called when a Run Command reaches it's requested end. Not called when the CreaMot is stopped by a call of Stop Function, or any other events. For use see precautions at Class description above.

Parameters:
<*object>Class pointer which possesses callback member.
<*CBmember>Pointer to callback function, member of Class.
    // Class Creator:
     AClass::AClass(Class Creation Parameters)
    {   ...
        // Attach callback function:
        MotorInstance->setMotorCallback(this, &AClass::CallBackMemberFunction);
        ...  
    }

    // Simple callback function, state variable endMove can be polled by main thread
    void AClass::CallBackMemberFunction()
    {  endMove=true;  }

Definition at line 207 of file CreaMot.h.

void callbackSet ( void(*)(void)  CBfunction )

Attach a basic Callback function.

A callback is called when the current Command reaches it's requested end. Not called when the CreaMot is stopped by a call of Stop Function, or any other events. For use see precautions at Class description above. Formerly called setMotorCallback()

    // Simple callback function, state variable endMove can be polled elsewhere
    void CallBackFunction()
    { endMove=true; }

    // main routine
    void main()
    {   ...
        // Attach callback function:
        MotorInstance->callbackSet(CallBackFunction);
        ...
        while (true) {
            ....
        if (endMove) // poll the endMove flag
               { ... } // react to Movement End
               
            ....
        }
    }
Parameters:
<*CBfunction>Callback function, must not be member of a class.

Definition at line 182 of file CreaMot.h.

uint32_t getStepsFullTurn (  )

MidLevel: Get number of Steps per Full turn.

Defaults to MOTOR_STEPS_FOR_A_TURN = 4096. Used by RunDegrees() to translate from angle in degrees to number of steps. Old Name was: getCalibration, but that was not a good explicit name.

Returns:
uint32_t The number of motor steps needed for a full turn.

Definition at line 121 of file CreaMot.cpp.

uint32_t getStepTime_us (  )

Mid Level: Get the CreaMot step time.

Step time is time between two CreaMot steps, and is given in microseconds and is passed to the ticker as delay time. So the larger the value the slower the CreaMot speed. Defaults to MOTOR_STEP_TIME_DEFAULT_US = 5000.

Returns:
uint32_t The structure of CreaMot status registers.
void MotorOFF (  )

Low Level: turn off all CreaMot Phases No more current flows, reduces holding force.

Turn off all CreaMot Phases, no more current flowing.

After: State: Motor_OFF. StepPhases memorizes the last used phase. Equivalent what previously the function "void Stop();" did .

Definition at line 208 of file CreaMot.cpp.

void MotorON (  )

Low Level: turn on the CreaMot Phases in the last used phase.

Turn on the CreaMot Phase, In the last used phase, memorized in StepPhases Equivalent to what previously the function "void Start();" did.

The last used phase is held in StepPhases. After: State: Motor_ON, or Motor_ZERO if StepPhases==0 Equivalent to what previously the function "void Start();" did.

Definition at line 215 of file CreaMot.cpp.

void MotorTest (  )

Low Level: Run one full turn clockwise then anticlockwise.

After: State: Motor_OFF. Blocking function, returns back only after end of full movement.

Definition at line 191 of file CreaMot.cpp.

void MotorZero (  )

Low Level: turn on the CreaMot Phases in Zero Position.

CreaMot phases turned on and put to Zero Position.

After: State: Motor_ZERO, StepPhases==0

Definition at line 222 of file CreaMot.cpp.

void PauseRun (  )

High Level: Pause a CreaMot Run.

Put CreaMot into Pause state, Run is suspended, but only effective if Status.cmd=MOTOR_run. Retains the number of steps that remain to be run if restarting run. While paused: still uses ticker; State: Motor_RUN; cmd=MOTOR_pause. Use RestartRun(); to continue.

Definition at line 100 of file CreaMot.cpp.

void RestartRun (  )

High Level: Restart a Paused Run.

Restart the Run that was launched before calling PuaseRun. Only effective if Status.cmd=MOTOR_pause, otherwise no re/action. Status afterwards is same as afterRun commands.

Definition at line 103 of file CreaMot.cpp.

void RunDegrees ( bool  AClockWise,
float  angle_deg 
)

High Level: Run CreaMot for a given angle.

Runs CreaMot for a given angle in given direction. Angles<0 are run in opposite direction. Call Pause() or Stop() to pause or end the CreaMot run prematurely. While running: Uses ticker; State: first Motor_ON then Motor_RUN; cmd=MOTOR_run. At end: calls attached Callback, stops ticker; State: Motor_OFF; cmd=MOTOR_stop then MOTOR_nop.

Parameters:
[in]<AClockWise>Given Direction, true for CLOCKWISE, false for COUNTERCLOCKWISE.
[in]<angle_deg>Angle>0 to rotate for, in degrees, Angles<0 are run in opposite direction.

Definition at line 83 of file CreaMot.cpp.

void RunDegrees ( float  angle_deg )

High Level: Run CreaMot for a given angle in default direction for details see RunDegrees (bool AClockWise, float angle_deg);.

Definition at line 88 of file CreaMot.cpp.

void RunDist_cm ( bool  AClockWise,
float  Dist_cm 
)

High Level: Run CreaMot for a given number of centimeters.

Runs CreaMot for a given wheel circumference in cimeters in given direction. You must setup the perimeter and diameter with setDiam(Adiam_cm) in advance, otherwise no reaction. Call Pause() or Stop() to pause or end the CreaMot run prematurely. While run: Uses ticker; State: first Motor_ON then Motor_RUN; cmd=MOTOR_run. At end: calls attached Callback, stops ticker; State: Motor_OFF; cmd=MOTOR_stop then MOTOR_nop.

Parameters:
[in]<AClockWise>Given Direction, true for CLOCKWISE, false for COUNTERCLOCKWISE.
[in]<Dist_cm>Circumference to rotate for, in cm, Dist_cm<0 are run in opposite direction.

Definition at line 46 of file CreaMot.cpp.

void RunDist_cm ( float  Dist_cm )

High Level: Run CreaMot for a given number of centimeters in default direction.

Same as RunDist_cm(AClockWise,Dist_cm) but uses Default diretion.

Definition at line 50 of file CreaMot.cpp.

void RunInfinite ( bool  AClockWise )

High Level: Run CreaMot "unlimited".

Runs CreaMot with out limit in given direction, precisely runs 4Billion Steps. While run: Uses ticker; State: first Motor_ON then Motor_RUN; cmd=MOTOR_run. Call Pause() or Stop() to pause or end the CreaMot run.

Parameters:
[in]<AClockWise>Given Direction, true for CLOCKWISE, false for COUNTERCLOCKWISE.

Definition at line 77 of file CreaMot.cpp.

void RunSteps ( bool  AClockWise,
int32_t  Nsteps 
)

High Level: Run CreaMot for a number of Steps.

During Run: Uses ticker; State: first Motor_ON then Motor_RUN; cmd=MOTOR_run. Call Pause() or Stop() to pause or end the run prematurely. At the end: calls the Callback, stops ticker; State: Motor_OFF.

Parameters:
[in]<AClockWise>Given Direction, true for CLOCKWISE, false for COUNTERCLOCKWISE.
[in]<Nsteps>Number of steps to run for; Nsteps<0 are run in opposite direction!

Definition at line 93 of file CreaMot.cpp.

void RunTurnAngle ( float  turn_angle_deg,
float  turn_radius_cm 
)

High Level: Run CreaMot for a turn angle around a turn_radius_cm in default direction.

Calculate the needed wheel angle from a turn angle and a turn_radius_cm.

Runs CreaMot for a given turn angle in degrees with a given turn radius. in default direction. Negative angles are rotated in opposite direction. turn-radius must be positive. Zero or negative radius are not executed. You must setup the perimeter and diameter with setDiam(Adiam_cm) in advance, otherwise no reaction. Call Pause() or Stop() to pause or end the CreaMot run prematurely. While run: Uses ticker; State: first Motor_ON then Motor_RUN; cmd=MOTOR_run. At end: calls attached Callback, stops ticker; State: Motor_OFF; cmd=MOTOR_stop then MOTOR_nop.

Parameters:
[in]<turn_angle_deg>Given turn angle in degrees that should be run
[in]<turn_radius_cm>Given Trun radius that should be run

Definition at line 61 of file CreaMot.cpp.

void setDiamCM ( float  Adiam_cm )

Additional geometric information: set the wheel diameter, also sets perimeter and degrees per cm.

Definition at line 54 of file CreaMot.cpp.

void setRotationPeriodSec ( float  Seconds_Per_Turn )

Set the time in seconds to get one full turn, rotation of 360°.

was previously called setSpeed().

Parameters:
<Seconds_Per_Turn>Period of Rotation, e.g. if =20.0 then CreaMot will do 360° in 20 seconds.

Definition at line 127 of file CreaMot.cpp.

void setSpeed_cm_sec ( float  speed_cm_sec )

Set CreaMot speed in centimeter/sec, based on perimeter in cm.

Definition at line 67 of file CreaMot.cpp.

void setStepsFullTurn ( uint32_t  StepsFullTurn )

MidLevel: Set number of Steps per Full turn.

Defaults is MOTOR_STEPS_FOR_A_TURN = 4096. Used by RunDegrees() to translate from degrees to number of steps. Old Name was: setCalibration, but not good explicit name.

Parameters:
<StepsFullTurn>Number of steps needed to complete a full CreaMot turn

Definition at line 124 of file CreaMot.cpp.

void setStepTime_us ( uint32_t  AStepTime_us )

Set the time in microseconds between two CreaMot steps.

Defaults to MOTOR_STEP_TIME_DEFAULT_US = 5000usec. Filters values below Minimum Value = 700. Passed to the ticker as delay time. Can be called while ticker is running, and takes immediate effect. Was previously called setStepTime(), but was not clear which units.

Parameters:
<AStepTime_us>the time in microseconds between two CreaMot steps

Definition at line 131 of file CreaMot.cpp.

void StepCCW (  )

Low Level: Advance CreaMot one step, rotates CounterClockwise.

Definition at line 236 of file CreaMot.cpp.

void StepClkW (  )

Low Level: Advance CreaMot one step, rotates Clockwise.

Definition at line 232 of file CreaMot.cpp.

void StepOnce (  )

Low Level: Advance CreaMot one step, rotates in direction of variable AClockWise.

Definition at line 228 of file CreaMot.cpp.

void StopRun (  )

High Level: End any Run.

Force stop of any ongoing run, but does not call the Callback function. Only effective if Status.cmd=MOTOR_run, otherwise no re/action. Emits first cmd=MOTOR_stop then cmd=MOTOR_nop. Aftewards: ticker is detached; State: Motor_OFF;

Definition at line 107 of file CreaMot.cpp.


Field Documentation

Command asked to be executed currently by the state machine.

Definition at line 380 of file CreaMot.h.

General state that the CreaMot state machine is in.

Definition at line 379 of file CreaMot.h.

Default rotation direction that serves as local storage, but not as actual direction.

Definition at line 255 of file CreaMot.h.

char StateValue

State value that is used and managed by the class owner.

Used for example by Creabot library to indicate if this is the left or right CreaMot.

Definition at line 259 of file CreaMot.h.