Skip to content

Class reference

akira215 edited this page Apr 17, 2016 · 3 revisions

Public Methods

linearStepper(const uint8_t stepPin, const uint8_t dirPin, const uint8_t foreLSPin, const uint8_t aftLSPin);

Constructor : you simply have to provide the 4 Arduino pins, as described in getting started. Take a look at the hardware paragraph before choosing your pins. The constructor is written so that if you try to create an instance as the MAX_INSTANCES is already reach, the instance will not be created. Decreasing MAX_INSTANCES is not required, as the memory of unused instance will not be allocated.

~linearStepper();

Destructor : you never have to call it. It remove the object of the interrupt routines and free the memory.

static uint8_t getNumOfInstances()

This fuction return the number of motor already created. Could be compare to MAX_INSTANCES macro before creating a new one. If there is too much instances, the new instance will not be created (no compiler warning or error, the error will occure during execution phase).

void setAutoCorrect(bool enable = true);

When enable, each time the device trigger the fore limit switch, its current position will be set to the value set up in setMaxPosition(const uint16_t maxPosition). Should be disable only for calibration purpose. This function only affect the fore limit switch. In any case, when the device will reach the aft limit switch, its position will be reset to 0.

void setMaxPosition(const uint16_t maxPosition);

Use this fuction to set up the fore position of the linear device. Take care that the fore position is not equal to the distance between the limit switch, as the device has its own width. Is auto correct is enable, each time the device reach the fore limit switch, its position will be set to this value. maxPosition is in mm, min position is always 0. Due to code optimization, small integer are used during interrupt routine. Its possible that the position set by a call to this method does not perfectly match to the value given (depending on ratio step / mm). You could check the position that has been taken in account by calling uint16_t getMaxPosition();. maxPosition should be provided in mm.

void setPosition(const uint16_t position);

This function will not move the motor. It just tell the library the actual position of the device, so it can be initialized. It useful when starting the device, if you know the correct position (take care that if the device was moved during shutdown, the position will be altered). The same remark as for setMaxPosition(const uint16_t maxPosition); and the rounding to small integer apply to this function. position should be provided in mm.

bool setRatioStepPerMm(const float stepPerMm);

This function is used to initialize the mechanical ratio step per mm of the device. If you don't know it, read & run the calibration example. This ratio never change if the mechanical parts are not changed. If you plan to change microstepping rate during execution phase, consider to recompute this ratio (for half step, the ratio should be multiply by 2, 4 for 4th steps,....). return false only if stepPerMm = 0.stepPerMm should be provided in step per mm.

void setSpeed(const uint8_t speed);

This function will not move the motor. It is used to set up the speed of the motor. Take care that if the mechanical ratio is wrong, the value of the speed is wrong. A call to setRatioStepPerMm(const float stepPerMm); automaically recompute the speed. That's why the speed could vary when setting up the ratio. Note that you can change the speed even during a move. The same remark as for setMaxPosition(const uint16_t maxPosition); and the rounding to small integer apply to this function. speed should be provide in mm/s.

void setAccelerationDistance(const uint8_t distOfAccel);

This function is used to set up the acceleration rate. To avoid heavy square root calculcation, acceleration has been simplified and is not constant. Instead of providing an acceleration in mm/s², a linear distance of acceleration in mm should be provided. Note that the acceleration, once setup will be apply for running AND for stopping the motor. There is two cases for stopping the motor without smooth braking once acceleration is set up : when the motor reach a limit switch, or a call of stop(false);. Position are computing regarding the acceleration (i.e. the motor will always stop to the correct position). This imply that if you plan to travel to short distance with high acceleration distance, the motor will never reach the speed set in : it will start to accelerate and then start to break at the middle distance of the travel. To cancel acceleration and run the device directly (default comportment), call setAccelerationDistance(0); The same remark as for setMaxPosition(const uint16_t maxPosition); and the rounding to small integer apply to this function.

void gotoPosition(const uint16_t position);

This function will make the motor move. The device will go to position, the absolute position in mm of the device, assuming that 0 is the aft position. If acceleration is set, it will be taken in account for the movement to be done(i.e. the device will always stop to the correct position). There is two particular cases : if you want to go to one end, you have to call gotoPosition(MAX_POS); or gotoPosition(0);. The device will be drive without any braking phase until it reach the limit switch. If you want to go smoothly to the aft stopper, you have to write gotoPosition(1); & gotoPosition(0); after an acceleration rate has been set up. The 1mm movement will occur during acceleration phase, it will be done at a small speed. The same remark as for setMaxPosition(const uint16_t maxPosition); and the rounding to small integer apply to this function.

void move(const int16_t offset);

This function is given for convenience. As gotoPosition(const uint16_t position); is absolute, move(const int16_t offset); is used to shift from the current position. offset (mm) is relative to actual position (fore = positive, aft = negative).

void stop(bool smooth = false);

This function is used to stop the motor immediately, even if the target position has not been reached. The only argument smooth, false per default concern the breaking phase. If it is set to true, the motor will slow down before stopping, using the acceleration distance set up by setAccelerationDistance(). If acceleration is not set up, the motor will stop immediately, even if smooth = true. Be aware that at great speed, stopping the motor brutally could lead to missing steps. After that, the position of the device will be unaccurate.

bool isMoving() const;

This function return true is the motor is moving otherwise false. As all the methods of the libs are non blocking, you have to consider while(motor.isMoving); to wait until motor stop. As the operations are all interrupt driven, during the while, the other motor created from the lib will start to move if they have to.

bool isForeLStriggered() const;

Return true if the Fore limit switch is triggered, otherwise false. This method is useful during initialization process

bool isAftLStriggered() const;

Return true if the Aft limit switch is triggered, otherwise false. This method is useful during initialization process

uint16_t getPosition() const;

This function return the current position in mm. You can call it during a move to chek the evolution of the position of the device.

uint16_t getMaxPosition() const;

This function return the max position in mm (i.e. the fore position). It is usefull to check after a setMaxPosition() call, due to the round issue described in setMaxPosition() paragraph.

int32_t getCurrentStep() const;

This function is given for convenience and debugging purpose. Using this lib, you don't have to care about steps once the mechanical calibration is done. Position are always calculated from the steps. The comportment of the steps follow the same rules as the position (autocorrect,...)

uint8_t getSpeed() const;

This function return the current speed setup in mm/s. It is NOT the present speed i.e. it will return a speed even if the motor is stopped. It is usefull to check after a setSpeed() call, due to the round issue described in setMaxPosition() paragraph.

uint8_t getAccelerationDistance() const;

Same as above for the acceleration.

static inline void handle_PCINT_isr() attribute((always_inline));

This is the global static ISR routine. Public only for interrupt access handler. You shall never call this function directly

static inline void handle_Timer_isr() attribute((always_inline));

This is the global static ISR routine. Public only for interrupt access handler. You shall never call this function directly

unsigned long getDeltaTime();

This function could be used only if macro DEBUG == 1. It could be used for debugging timing purpose (see §Timings). The return value deltaTime is time ellapsed between two steps.

static volatile unsigned long currentTime;

Same as above. This member is used only for debugging purpose, with macro DEBUG == 1.

static volatile unsigned long previousTime;

Same as above. This member is used only for debugging purpose, with macro DEBUG == 1.

Macros

#define MAX_INSTANCES

It define the maximum number of instance. The value shall be <=255 as the counter is define as an uint8_t. The class has been written to optimize memory management, it is useless to decrease this value, as the memory will be allocated only for created instances. The real issue is the timing. Each time an instance is created, it will receive the interrupt trigger. If the concerned motor should not move, the function will return immediately, but increasing the number of instance will increase the calls to the dedicated ISR function, reducing the available time for the main loop. Take a look at §Timings.

#define DEBUG

This macro is used to debug timing using unsigned long getDeltaTime(); set it to 1 will allow you to call this function. Take a look at §Timings.

#define MAX_POS

This macro is given for convenience. To throw the device to the fore stopper switch regardless the max position, simply write motor.gotoPosition(MAX_POS);