Library for handle a Stepper Motor Driver Borad
Diff: StepperMotor.cpp
- Revision:
- 0:ce88091ae9fb
- Child:
- 1:b96435078d4d
diff -r 000000000000 -r ce88091ae9fb StepperMotor.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/StepperMotor.cpp Tue Nov 20 16:46:43 2012 +0000 @@ -0,0 +1,165 @@ +#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); +}