|
OpenMoCo Engine Libraries
|
Motor Manager. More...
#include <OMMotor.h>
Public Member Functions | |
| OMMotor () | |
| void | ms (uint8_t) |
| uint8_t | ms () |
| void | backlash (uint8_t) |
| uint8_t | backlash () |
| void | setHandler (void(*)(uint8_t)) |
| void | enable (bool) |
| bool | enable () |
| void | continuous (bool) |
| bool | continuous () |
| void | contSpeed (unsigned int) |
| unsigned int | contSpeed () |
| void | stepDelayMin (unsigned int) |
| unsigned int | stepDelayMin () |
| void | stepDelayMax (unsigned int) |
| unsigned int | stepDelayMax () |
| void | move () |
| void | move (bool, unsigned long) |
| void | home () |
| void | homeSet () |
| long | homeDistance () |
Static Public Member Functions | |
| static void | dir (bool) |
| static bool | dir () |
| static bool | running () |
| static void | sleep (bool) |
| static bool | sleep () |
| static void | steps (unsigned long) |
| static unsigned long | steps () |
| static void | maxSteps (unsigned long) |
| static unsigned long | maxSteps () |
| static unsigned long | stepsMoved () |
| static void | stop () |
| static void | clear () |
Motor Manager.
The Motor Manager class provides the ability to run a stepper motor on the nanoMoCo platform in either continuous motion or on-demand motion using asynchronous (non-blocking) control.
All actions performed by the Motor Manager are non-blocking, and this library uses the Timer1 library to achieve this. For this reason, you may only have one such object instantiated in your program, and under no circumstances should you attempt to use Timer1 in another part of your program while a motor action is being performed.
Status reporting from the Motor Manager is handled via a callback function, which you may specify. This function will be called at specified times with a special code to indicate the status. For more information on this, see setHandler().
It is not possible to specify the motor control lines at this time, and the library will only work for other Arduino-compatible devices when they use the same pins for the stepper driver.
Example:
// note: You -always- have to include the TimerOne library in an arduino // sketch before including the OMMotor header #include "TimerOne.h" #include "OMMotor.h" OMMotor Motor = OMMotor(); void setup() { Motor.enable(true); Motor.continuous(false); Motor.setHandler(motorCallback); Serial.begin(19200); } void loop() { if( ! Motor.running() ) { delay(1000); Serial.println("Moving!"); Motor.move(true, 1000); } } void motorCallback( byte code ) { if( code == OM_MOT_DONE ) { Serial.println("Got Done!"); Serial.print("Moved: "); unsigned long smoved = Motor.stepsMoved(); Serial.println(smoved, DEC); } else { Serial.println("Got Begin!"); } }
(c) 2008-2011 C. A. Church / Dynamic Perception
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.
| OMMotor | ( | ) |
Constructor
Creates a new insstance of the OMMotor class. Sets all control outputs to the output state.
Definition at line 66 of file OMMotor.cpp.
| void backlash | ( | uint8_t | p_Back | ) |
Set Backlash Compensation Amount
Sets the current backlash compensation amount.
The backlash compensation allows for adjustments to moves when the gear train attached to your motor exhibits backlash. If the direction of the motor is changed between moves, the specified amount of steps will be added to the next movement, to take up the slack created by the backlash.
| p_Back | The backlash compensation amount, in steps |
Definition at line 294 of file OMMotor.cpp.
| uint8_t backlash | ( | ) |
Get Backlash Compensation Amount
Returns the currently set backlash compensation amount.
Definition at line 276 of file OMMotor.cpp.
| void clear | ( | ) | [static] |
Clear Steps Moved
Clears the count of steps moved. Additionally, if the motor is currently running, any movement will be immediately stopped, and the callback will be executed with the OM_MOT_DONE argument.
This method does NOT reset the home location for the motor.
Definition at line 773 of file OMMotor.cpp.
| void continuous | ( | bool | p_En | ) |
Set Continuous Motion Mode
Enables or disables continuous motion mode.
In continuous motion mode, the motor will run continuously at the specified speed until either a stop() request is received, or maxSteps() are reached. When continuous mode is disabled, the motor only moves the distance specified by steps() or passed via the two-argument form of move().
Note: you may adjust the currently moving speed at any time, using contSpeed().
Of additional note: when in continous mode, the callback will be executed immediately and passed the OM_MOT_DONE value when calling move(), indicating that you are free to do other activities while the motor is moving.
Below is an example of performing aa continuous motion move, with a maxSteps() value set. The motor will move at a speed of 1,000 steps/second, and will automatically stop its self after 5,000 steps have been taken. Once the motor is stopped, the steps moved value is cleared out, and we're ready to move again.
#include "TimerOne.h" #include "OMMotor.h" OMMotor Motor = OMMotor(); unsigned int max_steps = 5000; void setup() { Motor.enable(true); Motor.sleep(true); Motor.continuous(true); Motor.contSpeed(1000); Motor.maxSteps(5000); Motor.setHandler(motorCallback); Serial.begin(19200); } void loop() { if( ! Motor.running() ) { delay(1000); Serial.println("Clearing Motor"); Motor.clear(); Serial.println("Starting Continuous Motion!"); Motor.move(); } else { Serial.print("Moved: "); unsigned long smoved = Motor.stepsMoved(); Serial.println(smoved, DEC); delay(1000); } } void motorCallback( byte code ) { if( code == OM_MOT_DONE ) { Serial.println("Got Done!"); Serial.print("Moved: "); unsigned long smoved = Motor.stepsMoved(); Serial.println(smoved, DEC); } else { Serial.println("Got Begin!"); } }
| p_En | Enable (true) or Disable (false() continuous motion |
Definition at line 453 of file OMMotor.cpp.
| bool continuous | ( | ) |
Get Continuous Motion Value
Returns the current continuous motion value.
Definition at line 466 of file OMMotor.cpp.
| void contSpeed | ( | unsigned int | p_Speed | ) |
Set Continuous Motion Speed
Sets the current continuous motion speed, in steps per second.
You may set the speed even while a continuous move is running, and the speed will be immediately changed.
| p_Speed | Steps per second |
update skip settings, if needed
Definition at line 481 of file OMMotor.cpp.
| unsigned int contSpeed | ( | ) |
Get Continuous Motion Speed
Returns the current continuous motion speed.
Definition at line 496 of file OMMotor.cpp.
| bool dir | ( | ) | [static] |
Get Current Direction
Returns the current direction value.
Note: if called in the middle of a move, the value returned may be different than the value after the move is completed, if you called move() in its two-argument form.
Definition at line 149 of file OMMotor.cpp.
| void dir | ( | bool | p_Dir | ) | [static] |
Set Current Direction
Sets the current direction value.
Note: if called while a move is underway, the direction will be changed immediately, but backlash compensation will not be applied. Additionally, if this method is called during a move executed via the two-argument form of move(), then the direction value may not be retained after the move is completed.
| p_Dir | Direction value (true or false) |
Definition at line 168 of file OMMotor.cpp.
| void enable | ( | bool | p_En | ) |
Enable Motor
Sets the current Enable/Disable flag value.
If the motor is not enabled, calls to move() will return immediately and trigger a callback with the OM_MOT_DONE value. It will not be possible to move the motor in any fashion while the motor is disabled.
| p_En | Enable (true) or Disable (false) the motor |
Definition at line 312 of file OMMotor.cpp.
| bool enable | ( | ) |
Get Enable Flag Value
Returns the current enable flag value.
Definition at line 325 of file OMMotor.cpp.
| void home | ( | ) |
Send Motor Home
Send motor home immediately.
This method will not stop any ongoing move, but it will immediately send the motor back to its home position, and will result in an aborted move.
Like other moves, this move is non-blocking and will trigger the callback to be executed with the OM_MOT_DONE argument when the send to home move is completed.
Definition at line 1070 of file OMMotor.cpp.
| long homeDistance | ( | ) |
Distance from Home
Returns the number of steps and direction back to the home position.
Definition at line 1054 of file OMMotor.cpp.
| void homeSet | ( | ) |
Set Home
Sets the current position of the motor as the home position.
Definition at line 1040 of file OMMotor.cpp.
| void maxSteps | ( | unsigned long | p_Steps | ) | [static] |
Set Maximum Steps
Sets the maximum steps to be taken, for both continuous and non- continuous moves.
Once this steps count is achieved, the motor will be stopped immediately and the callback will be called with the OM_MOT_DONE paarameter. It will not be possible to move again until the clear() method has been called.
Set the maximum steps to 0 to disable this feature.
| p_Steps | Maximum steps to move |
Definition at line 554 of file OMMotor.cpp.
| unsigned long maxSteps | ( | ) | [static] |
Get Maximum Steps
Returns the current maximum step count.
Definition at line 566 of file OMMotor.cpp.
| void move | ( | ) |
Move Now
Immediately executes a move using saved parameters.
This method will execute either a continuous or non-continuous move using the values currently saved in dir() and steps() or contSpeed(), depending on your current motion type.
If a move can be executed, asynchronous movement is initiated and the callback is called. When in continuous motion, the OM_MOT_DONE argument will be passed to the callback, otherwise OM_MOT_BEGIN will be passed.
If the motor is disabled, or the maximum # of steps has been reached, the callback will be executed with the OM_MOT_DONE argument immediately, and no move will take place.
Definition at line 672 of file OMMotor.cpp.
| void move | ( | bool | p_Dir, |
| unsigned long | p_Steps | ||
| ) |
Move Now
Immediately executes a move using specified parameters.
This method will execute either a continuous or non-continuous move using the values passed as arguments. If the direction of the motor set by dir() is different than the value passed as an argument, dir() will return the value passed here as an argument until the move is completed or stopped.
For continuous motion, you must specify 0 as the number of steps. To limit continuous motion, use maxSteps() instead.
If a move can be executed, asynchronous movement is initiated and the callback is called. When in continuous motion, the OM_MOT_DONE argument will be passed to the callback, otherwise OM_MOT_BEGIN will be passed.
If the motor is disabled, or the maximum # of steps has been reached, the callback will be executed with the OM_MOT_DONE argument immediately, and no move will take place.
Definition at line 699 of file OMMotor.cpp.
| uint8_t ms | ( | ) |
Get MicroSteps
Returns the current microstep setting.
Definition at line 86 of file OMMotor.cpp.
| void ms | ( | uint8_t | p_Div | ) |
Set MicroSteps
Sets the current microstep setting. You may choose from the following values:
1, 2, 4, 8, or 16
| p_Div | Microstep setting value |
Definition at line 103 of file OMMotor.cpp.
| bool running | ( | ) | [static] |
Motor Running?
Returns whether or not the motor is currently moving.
Definition at line 508 of file OMMotor.cpp.
| void setHandler | ( | void(*)(uint8_t) | p_Func | ) |
Set Callback Handler
As actions start and complete, a callback is used to signal what is happening, without having to query the method to see if a given action is still occuring.
By passing a function pointer to this method, that function will be executed at each one of these moments, and passed a single, one byte argument that matches the signal which is to be sent. The template for this function is:
void function(byte val) { }
The following codes are defined:
OM_MOT_DONE
- The motor has stopped moving
OM_MOT_MOVING
- The motor has begun moving
An example of an appropriate callback handler:
#include "TimerOne.h" #include "OMMotor.h" OMMotor Motor = OMMotor(); void setup() { Motor.enable(true); Motor.continuous(false); Motor.setHandler(motorCallback); Serial.begin(19200); } void loop() { if( ! Motor.running() ) { delay(1000); Serial.println("Moving!"); Motor.move(true, 1000); } } void motorCallback( byte code ) { if( code == OM_MOT_DONE ) { Serial.println("Got Done!"); Serial.print("Moved: "); unsigned long smoved = Motor.stepsMoved(); Serial.println(smoved, DEC); } else { Serial.println("Got Begin!"); } }
| p_Func | A pointer to a function taking a single byte argument |
Definition at line 256 of file OMMotor.cpp.
| void sleep | ( | bool | p_En | ) | [static] |
Enable Sleep
Sets the current sleep between move flag value and immediately changes the status off the motor driver sleep line.
When sleep is enabled, the motor driver will be put to sleep between moves to conserve energy and reduce the heat of both the motor and the driver.
| p_En | Enable (true) or Disable (false) sleeps between moves |
Definition at line 343 of file OMMotor.cpp.
| bool sleep | ( | ) | [static] |
Get Sleep Flag Value
Returns the current value of the sleep between moves flag.
Definition at line 363 of file OMMotor.cpp.
| unsigned int stepDelayMax | ( | ) |
Get Maximum Step Delay
Returns the current maximum step delay.
Definition at line 638 of file OMMotor.cpp.
| void stepDelayMax | ( | unsigned int | p_Steps | ) |
Set Maximum Step Delay
Sets the maximum delay between steps, in microseconds.
This setting controls how fast steps will be taken during a non-continuous motion move. Maximum step delay controls the initial delay between steps as a non-continuous motion begins. The wider the difference between the minimum and maximum step delay, the longer and smoother the acceleration into an individual move will be.
For continuous motion moves, use the contSpeed() method instead.
The default value is 1400
| p_Steps | Maximum delay between steps, in microseconds |
Definition at line 625 of file OMMotor.cpp.
| unsigned int stepDelayMin | ( | ) |
Get Minimum Step Delay
Returns the current minimum step delay.
Definition at line 602 of file OMMotor.cpp.
| void stepDelayMin | ( | unsigned int | p_Steps | ) |
Set Minimum Step Delay
Sets the minimum delay between steps, in microseconds.
This setting controls how fast steps will be taken during a non-continuous motion move. The lower this setting, the faster the motor will step. The wider the difference between the minim and maximum step delay, the longer and smoother the acceleration into an individual move will be.
For continuous motion moves, use the contSpeed() method instead.
The default value is 1,000
| p_Steps | Minimum delay between steps, in microseconds |
Definition at line 588 of file OMMotor.cpp.
| unsigned long steps | ( | ) | [static] |
Get Steps Per Move
Returns the current steps per move for non-continuous motion moves.
Definition at line 535 of file OMMotor.cpp.
| void steps | ( | unsigned long | p_Steps | ) | [static] |
Set Steps per Move
Sets the current steps per move for non-continuous motion moves.
This method sets a default steps per move so that one may use move() with no arguments.
| p_Steps | Steps Per Move |
Definition at line 523 of file OMMotor.cpp.
| unsigned long stepsMoved | ( | ) | [static] |
Get Steps Moved
Returns the count of steps moved since the last clear() call.
Definition at line 650 of file OMMotor.cpp.
| void stop | ( | ) | [static] |
Stop Immediately
Immediately stops any movement in progress.
Triggers the callback with the OM_MOT_DONE argument.
Definition at line 743 of file OMMotor.cpp.