Library for handle a Stepper Motor Driver Borad
Revision 1:b96435078d4d, committed 2012-12-16
- Comitter:
- dury
- Date:
- Sun Dec 16 16:39:40 2012 +0000
- Parent:
- 0:ce88091ae9fb
- Commit message:
- Library for move Stepper Motor using DIR and CLK pin of a control board.;
Changed in this revision
StepperMotor.cpp | Show annotated file Show diff for this revision Revisions of this file |
StepperMotor.h | Show annotated file Show diff for this revision Revisions of this file |
--- a/StepperMotor.cpp Tue Nov 20 16:46:43 2012 +0000 +++ b/StepperMotor.cpp Sun Dec 16 16:39:40 2012 +0000 @@ -1,165 +1,259 @@ -#include "StepperMotor.h" - -StepperMotorDrv::StepperMotorDrv(PinName PinDir, PinName PinClk):Dir(PinDir),Clock(PinClk) -{ - MaxSpeed=100; - Speed=0; - Step=1; - DutyCycle=50; - Direction=CW; - SetRawSpeedHz(0); - SetDutyCyclePerc(DutyCycle); - SetDir(CW); -} - -StepperMotorDrv::~StepperMotorDrv() -{ -} - -// Set duty cycle of pwm in percentual -float StepperMotorDrv::SetDutyCyclePerc(float PercentualValue) -{ - if (PercentualValue > 100) PercentualValue=100; - DutyCycle=PercentualValue; - Clock.write(DutyCycle/100); - return (DutyCycle); -} - -// Set duty cycle of pwm as float value from 0.0 to 1.0 -// Values grater than 1.0 were used as 0.n -float StepperMotorDrv::SetDutyCycleValue(float DecimalValue) -{ - if (DecimalValue > 1){ - DecimalValue=DecimalValue - int(DecimalValue); - } - Clock.write(DecimalValue); - return(DecimalValue * 100); -} - -// Set Raw Speed in Hz -float StepperMotorDrv::SetRawSpeedHz(float SpeedHz) -{ - Speed=SpeedHz; - Clock.period(1/Speed); - return(Speed); -} - -// Set Max Speed to Reach in Hz -void StepperMotorDrv::SetMaxSpeedHz(float MaxSpeedHz) -{ - MaxSpeed=MaxSpeedHz; -} - -// Set Speed in Percentoal Value (the result depends on MaxSpeed) -// Return: Calculated Speed in Hz -float StepperMotorDrv::SetSpeedPerc(float PercValue) -{ - Speed=(MaxSpeed/100)*PercValue; - Clock.period(1/Speed); - return(Speed); -} - -// Set Speed in Percentoal Value (the result depends on MaxSpeed) -// Return: new Speed in Hz -float StepperMotorDrv::IncSpeedHz(float StepValueHz) -{ - Speed+=StepValueHz; - Clock.period(1/Speed); - return(Speed); -} - -// Increment Speed by <IncStep> Value -// Return: new Speed in Hz -float StepperMotorDrv::SpeedUp(void) -{ - return(IncSpeedHz(Step)); -} - -// Set Speed in Percentoal Value (the result depends on MaxSpeed) -// Return: new Speed in Hz -float StepperMotorDrv::DecSpeedHz(float StepValueHz) -{ - Speed-=StepValueHz; - Clock.period(1/Speed); - return Speed; -} - -// Decrement Speed by <Step> Value -// Return: new Speed in Hz -float StepperMotorDrv::SpeedDown(void) -{ - return(DecSpeedHz(Step)); -} - -// Stop the Motor -void StepperMotorDrv::Stop(void) -{ - SetRawSpeedHz(0); -} - -// Get the current speed in Hz -float StepperMotorDrv::GetSpeedHz(void) -{ - return Speed; -} - -// Get the current speed in Perc Value -float StepperMotorDrv::GetSpeedPerc(void) -{ - return (Speed*100)/MaxSpeed; -} - -// Get the current MAX Speed in Hz -float StepperMotorDrv::GetMaxSpeed(void) -{ - return (MaxSpeed); -} - -// Get the current Duty Cycle in Perc Value -float StepperMotorDrv::GetDuryCyclePerc(void) -{ - return (DutyCycle); -} - -// Get the current Duty Cycle decimal value -float StepperMotorDrv::GetDuryCycleValue(void) -{ - return (DutyCycle/100); -} - -// Get the current Step Value -float StepperMotorDrv::GetStepHz(void) -{ - return (Step); -} - -// Set Rotation Direction: CW=1 CC=0 -void StepperMotorDrv::SetDir(bool RotationDir) -{ - Dir=RotationDir; -} - -// Set Rotation Direction CW -void StepperMotorDrv::SetDirCW(void) -{ - Dir=CW; -} - -// Set Rotation Direction CC -void StepperMotorDrv::SetDirCC(void) -{ - Dir=CC; -} - -// Set Rotation Direction CW -void StepperMotorDrv::RevertDir(void) -{ - Dir=!Dir; -} - -// Get Rotation Direction Direction -// Return: CW=1 or CC=0 -bool StepperMotorDrv::GetDir(void) -{ - return(Dir); -} +#include "StepperMotor.h" + +/* TODO: +_RunDegrees(float degrees) +_SetDegreeJump(float degree) +_DegreeUp() +_DegreeDown() +*/ +Serial debug(USBTX,USBRX); + +StepperMotorDrv::StepperMotorDrv(PinName PinDir, PinName PinClk):Dir(PinDir),Clock(PinClk) +{ + MaxSpeed=100; // default 100 Hz + Speed=50; // default 50 Hz + SpeedJump=1; // default 1 Hz + DutyCycle=50; // default 50 % + SetDir(CW); // default ClockWise + Clock=0; // Set to 0 for sure + CurrPos=0; // Set 0 when power on + CurrDegree=0; // default 0 degrees + DegreesPerStep=0; // 0 -> NOT SET +} + +StepperMotorDrv::~StepperMotorDrv() +{ +} + +// Set duty cycle of pwm in percentual +void StepperMotorDrv::SetDutyCyclePerc(float PercentualValue) +{ + if (PercentualValue > 100) PercentualValue=100; + DutyCycle=PercentualValue; + tUp=(tPeriod/100)*DutyCycle; + tDown=tPeriod-tUp; +} + +// Set duty cycle of pwm as float value from 0.0 to 1.0 +// Values grater than 1.0 were used as 0.n +void StepperMotorDrv::SetDutyCycle(float DecimalValue) +{ + if (DecimalValue > 1){ + DecimalValue=DecimalValue - int(DecimalValue); + } + DutyCycle=DecimalValue*100; + tUp=tPeriod*DecimalValue; + tDown=tPeriod-tUp; +} + +// Set Raw Speed in Hz +void StepperMotorDrv::SetSpeed(float SpeedHz) +{ + Speed=SpeedHz; + tPeriod=1/Speed; + // Recalculate also DutyCycle t for new period + tUp=tPeriod*(DutyCycle/100); + tDown=tPeriod-tUp; +} + +void StepperMotorDrv::SetMaxSpeed(float MaxSpeedHz) +{ + MaxSpeed=MaxSpeedHz; +} + +void StepperMotorDrv::SetSpeedPerc(float PercValue) +{ + Speed=(MaxSpeed/100)*PercValue; + tPeriod=1/Speed; + // Recalculate also DutyCycle t for new period + tUp=tPeriod*(DutyCycle/100); + tDown=tPeriod-tUp; +} + +float StepperMotorDrv::SpeedUp(float ValueHz) +{ + Speed+=ValueHz; + // Recalculate also DutyCycle t for new period + tUp=tPeriod*(DutyCycle/100); + tDown=tPeriod-tUp; + + return(Speed); +} + +float StepperMotorDrv::SpeedDown(float ValueHz) +{ + Speed-=ValueHz; + // Recalculate also DutyCycle t for new period + tUp=tPeriod*(DutyCycle/100); + tDown=tPeriod-tUp; + + return Speed; +} + +void StepperMotorDrv::SetSpeedJump(float ValueHz) +{ + SpeedJump=ValueHz; +} + +float StepperMotorDrv::SpeedUp(void) +{ + return(SpeedUp(SpeedJump)); +} + +// Decrement Speed by <Step> Value +// Return: new Speed in Hz +float StepperMotorDrv::SpeedDown(void) +{ + return(SpeedDown(SpeedJump)); +} + +// Get the current speed in Hz +float StepperMotorDrv::GetSpeed(void) +{ + return Speed; +} + +// Get the current speed in Perc Value +float StepperMotorDrv::GetSpeedPerc(void) +{ + return (Speed*100)/MaxSpeed; +} + +// Get the current MAX Speed in Hz +float StepperMotorDrv::GetMaxSpeed(void) +{ + return (MaxSpeed); +} + +// Get the current Duty Cycle in Perc Value +float StepperMotorDrv::GetDutyCyclePerc(void) +{ + return (DutyCycle); +} + +// Get the current Duty Cycle decimal value +float StepperMotorDrv::GetDutyCycle(void) +{ + return (DutyCycle/100); +} + +// Get the current Step Value +float StepperMotorDrv::GetSpeedJump(void) +{ + return (SpeedJump); +} + +// Set Rotation Direction: CW=1 CC=0 +void StepperMotorDrv::SetDir(bool RotationDir) +{ + Dir=RotationDir; +} + +// Set Rotation Direction CW +void StepperMotorDrv::SetDirCW(void) +{ + Dir=CW; +} + +// Set Rotation Direction CC +void StepperMotorDrv::SetDirCC(void) +{ + Dir=CC; +} + +// Set Rotation Direction CW +void StepperMotorDrv::RevertDir(void) +{ + Dir=!Dir; +} + +// Get Rotation Direction Direction +// Return: CW=1 or CC=0 +bool StepperMotorDrv::GetDir(void) +{ + return(Dir); +} + +void StepperMotorDrv::Run(void) +{ + Clock.period(tPeriod); + Clock.write(DutyCycle/100); +} + +// Stop the Motor +void StepperMotorDrv::Stop(void) +{ + Clock=0; +} + +void StepperMotorDrv::RunStep(int nrSteps, bool RotationDir) +{ + Dir=RotationDir; + int StepAdd=(0-1)+(RotationDir*2); + float DegreeAdd=DegreesPerStep*(float(StepAdd)); + + for (int step=0; step<nrSteps; step++){ + Clock=1; + wait(tUp); + Clock=0; + wait(tDown); + CurrPos=CurrPos+StepAdd; // Update Current Step Position + CurrDegree=CurrDegree+DegreeAdd; // Update Current Degree Position + //debug.printf("Current Pos: %d Degree: %f\r\n",CurrPos,CurrDegree); + } +} + +void StepperMotorDrv::RunStep(int nrSteps) +{ + RunStep(nrSteps,Dir); +} + +void StepperMotorDrv::RunStep(void) +{ + RunStep(1,Dir); +} + +void StepperMotorDrv::RunStepCW(void) +{ + RunStep(1,CW); +} + +void StepperMotorDrv::RunStepCC(void) +{ + RunStep(1,CC); +} + +void StepperMotorDrv::SetZero(void) +{ + CurrPos=0; + CurrDegree=0; +} + +void StepperMotorDrv::SetCurrPos(int Pos) +{ + CurrPos=Pos; +} + +int StepperMotorDrv::GetCurrPos(void) +{ + return(CurrPos); +} + +float StepperMotorDrv::GetPeriod(void) +{ + return(tPeriod); +} + +void StepperMotorDrv::SetDegreePerStep(float Degrees){ + DegreesPerStep=Degrees; +} + +float StepperMotorDrv::GetDegreePerStep(void){ + return(DegreesPerStep); +} + +float StepperMotorDrv::GetCurrDegree(void){ + return(CurrDegree); +} + + \ No newline at end of file
--- a/StepperMotor.h Tue Nov 20 16:46:43 2012 +0000 +++ b/StepperMotor.h Sun Dec 16 16:39:40 2012 +0000 @@ -1,85 +1,259 @@ -/* mbed Stepper Library - * Copyright (c) 2012 Fabio Durigon - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -#ifndef STEPPERMOTOR_H -#define STEPPERMOTOR_H - -#include "mbed.h" - -//!Library for handle Stepper Motor with a Power Module with Clock and Dir input - -// Rotation direction -#define CW 1 -#define CC 0 - -class StepperMotorDrv -{ - public: - /** Create a StepperMotorDrv Object - * - * @param PinDir Digital Output Pin to set Direction of motor - * @param PinClk Pwm Output Pin for Clock signal - */ - StepperMotorDrv(PinName PinDir, PinName PinClk); - - /** Destroys an instance of SRD02 - */ - ~StepperMotorDrv(); - - /** Set duty cicle of clock pulse - * - * @param PercentualValue: Value 0-100% - */ - float SetDutyCyclePerc(float PercentualValue); - float SetDutyCycleValue(float DecimalValue); - float SetRawSpeedHz(float SpeedHz); - void SetMaxSpeedHz(float MaxSpeedHz); - float SetSpeedPerc(float PercValue); - float IncSpeedHz(float StepValueHz); - float SpeedUp(void); - float DecSpeedHz(float StepValueHz); - float SpeedDown(void); - void Stop(void); - float GetSpeedHz(void); - float GetSpeedPerc(void); - float GetMaxSpeed(void); - float GetDuryCyclePerc(void); - float GetDuryCycleValue(void); - float GetStepHz(void); - void SetDir(bool RotationDir); - void SetDirCW(void); - void SetDirCC(void); - void RevertDir(void); - bool GetDir(void); - - private: - DigitalOut Dir; - PwmOut Clock; - - float MaxSpeed; - float Speed; - float Step; - float DutyCycle; - bool Direction; - -}; +/* mbed Stepper Motor Library + * Copyright (c) 2012 Fabio Durigon + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#ifndef STEPPERMOTOR_H +#define STEPPERMOTOR_H + +#include "mbed.h" + +//!Library for handle Stepper Motor with a Power Module with Clock and Dir input + +// Rotation direction +#define CW 1 +#define CC 0 + +/** Library to control a Stepper Motor Driver Board with Clock and Dir input*/ +class StepperMotorDrv +{ + public: + /** Create a StepperMotorDrv Object + * + * @param PinDir Digital Output Pin to set Direction of motor + * @param PinClk Pwm Output Pin for Clock signal + */ + StepperMotorDrv(PinName PinDir, PinName PinClk); + + /** Destroys an instance of StepperMotorDrv Object*/ + ~StepperMotorDrv(); + + /** Set duty cycle of clock pulse in Percentual + * + * @param PercentualValue Value 0-100% + */ + void SetDutyCyclePerc(float PercentualValue); + + /** Set duty cycle of clock pulse in decimal value + * + * @param DecimalValue 0.0 to 1.0 (0.5 = 50%) + */ + void SetDutyCycle(float DecimalValue); + + /** Set Motor Speed: frequency of clock pulse to apply on PinClk + * + * @param SpeedHz Frequency value in Hz + */ + void SetSpeed(float SpeedHz); + + /** Set MaxSpeed parameter used only by "Perc functions" + * to calculate raw frequency to apply. + * + * @param MaxSpeedHz Frequency in Hz + * + * + * Example: + * @code + * SetMaxSpeed(1000); // Max Speed is 1000 Hz + * SetSpeedPerc(50); // Set Speed to 50% that means 500 Hz + * @endcode + * + * + */ + void SetMaxSpeed(float MaxSpeedHz); + + /** Set Speed in Perc of MaxSpeed + * @param PercValue 0 to 100 (in %) + * + * + * Example: + * @code + * SetMaxSpeed(1000); // Max Speed is 1000 Hz + * SetSpeedPerc(100); // Set Speed to 100% that means 1000 Hz + * @endcode + * + * + */ + void SetSpeedPerc(float PercValue); + + /** Increment Speed by Hz value + * @param StepValueHz: amount of Hz to add to velocity + * + * @return new Speed in Hz + */ + float SpeedUp(float ValueHz); + + /** Decrement Speed by Hz value + * @param StepValueHz: amount of Hz to substract to velocity + * + * @return new Speed in Hz + */ + float SpeedDown(float ValueHz); + + /** Set Step value to apply in SpeedUp() SpeedDown() like functions + * @param StepValueHz: value in Hz + * + * + * Example: + * @code + * SetStep(10); // Set Step to 10 Hz + * ... + * SpeedUp(); // Increase Speed by 10 Hz + * ... + * @endcode + * + * + */ + void SetSpeedJump(float ValueHz); + + /** Increase velocity by a Step set with SetStep() function + * + * @return new Speed in Hz + * + */ + float SpeedUp(void); + + /** Decrease velocity by a Step set with SetStep() function + * + * @return new Speed in Hz + * + */ + float SpeedDown(void); + + /** Get Current Speed in Hz*/ + float GetSpeed(void); + + /** Get Current Speed in Percentual*/ + float GetSpeedPerc(void); + + /** Get MaxSpeed parameter set (in Hz)*/ + float GetMaxSpeed(void); + + /** Get duty cycle value in Percentual*/ + float GetDutyCyclePerc(void); + + /** Get duty cycle in decimal value (Ex: 0.5 means 50%)*/ + float GetDutyCycle(void); + + /** Get step value for SpedUp/Down (in Hz)*/ + float GetSpeedJump(void); + + /** Set Rotation Direction + * @param RotationDir 1 or CW for ClockWise rotation, 0 or CC for Counterclockwise rotation + * + * N.B. CW and CC depends from MotorBoard Specs + * + */ + void SetDir(bool RotationDir); + + /** Set Rotation Direction to CW*/ + void SetDirCW(void); + + /** Set Rotation Direction to CC*/ + void SetDirCC(void); + + /** Revert Direction*/ + void RevertDir(void); + + /** Get current Direction + * + * @return 1 for CW, o for CC + */ + bool GetDir(void); + + /** Run Motor*/ + void Run(void); + + /** Stop Motor*/ + void Stop(void); + + /** Run <nrSteps> step in <RotationDir> direction*/ + void RunStep(int nrSteps, bool RotationDir); + + /** Run <nrSteps> step in current direction*/ + void RunStep(int nrSteps); + + /** Run one step in current direction*/ + void RunStep(void); + + /** Run one step in clockwise direction*/ + void RunStepCW(void); + + /** Run one step in counterclockwise direction*/ + void RunStepCC(void); + + /** Reset current position to 0 + * currPos=0 + * currDegree=0 + */ + void SetZero(void); + + /** Set internal <currPos> to Pos + * be carefull if you use degrees funcions + * this don't affect the degrees variables + */ + void SetCurrPos(int Pos); + + /* Get current position in steps positive or negative relative to zero*/ + int GetCurrPos(void); + + /** Get current Period of duty cycle in seconds*/ + float GetPeriod(void); + + /** Set degrees of rotation by one step of motor + * Set it if You want to use Degrees functions + * + * + * Example: + * @code + * SetDegreePerStep(1.6); // 1.6 degrees for each motor step + * SetZero(); + * RunStep(10); // move 1.6 * 10 = 16 degrees + * printf("%f",GetCurrentDegree()); // prints "16" + * ... + * @endcode + * + * + */ + void SetDegreePerStep(float Degrees); + + /** Get current "Degrees per one step" value*/ + float GetDegreePerStep(void); + + /** Get current position in Degrees*/ + float GetCurrDegree(void); + + + private: + DigitalOut Dir; + PwmOut Clock; + float MaxSpeed; + float Speed; + float SpeedJump; + float DutyCycle; + int CurrPos; + float tPeriod; + float tUp; + float tDown; + float DegreesPerStep; + float CurrDegree; + +}; #endif \ No newline at end of file