StepperUNO Library 1.4: Difference between revisions

From lechacal
Jump to navigation Jump to search
 
(27 intermediate revisions by the same user not shown)
Line 1: Line 1:
under construction...


=Installation=
=Installation=
Line 7: Line 5:
=Download=
=Download=
[http://lechacal.com/stepperUNO/stepperUNO_v1_4.zip StepperUNO Library v1.4]<br>
[http://lechacal.com/stepperUNO/stepperUNO_v1_4.zip StepperUNO Library v1.4]<br>
[http://lechacal.com/stepperUNO/stepperUNO_v1_4_1.zip StepperUNO Library v1.4.1]<br>


=Configuration=
=Configuration=
Open the config.h file in the library/stepperUNO folder.<br>
Open the config.h file in the library/stepperUNO folder.<br>
Change the KEYPAD_VERSION value as needed. Value should be 0 1 or 2. If you do not know just try one or the other and see how the button reacts.
Change the KEYPAD_VERSION value as needed. Values should be 0 1 or 2. Use test and trials to find out which options works best for your keypad.


=Function list=
=Function list=
==Keypad==
==Keypad==
If the keypad does not reply correctly on the left/right/up/down/select button then a calibration should be performed. Values should be changed in the .h file of the library.
 
===uint8_t ReadKey()===
===uint8_t ReadKey()===
This function should be called as often as possible. Typically inside the main loop. This will update the Keypad.state variable with one of the defined event below.
This function should be called as often as possible. Typically inside the main loop. This will update the Keypad.state variable with one of the defined event below.
Line 45: Line 44:
  #define LONG_RELEASED_RST 15
  #define LONG_RELEASED_RST 15


On a conventional Arduino UNO these LCD keypad will connect the RST button directly to the reset of the arduino. This is a waste in our opinion and we have re-routed this button for using it on something more useful.
On a conventional Arduino UNO these LCD keypad will connect the RST button directly to the reset of the arduino. On the stepperUNO we have rerouted this RST key so that it can be used for something else than reset. This is to explain why the RST key used different functions.


===uint8_t state===
===uint8_t state===


Event state. Will be equal to one of the define listed above once an event has occurred.
Event state variable. This is equal to one of the states listed above once an event has occurred. It indicates what buttons have been pressed and released at any given time.


===uint8_t rst_state===
===uint8_t rst_state===
Line 64: Line 63:


==Motor Control==
==Motor Control==
===void begin(uint8_t _address, uint16_t n_step)===
Initialiser to be used in Setup section of Arduino.
''address'' parameter is the i2c address of the motor controller. Usually 0x04 or 0x05.
''n_step'' is the number of steps per revolution. This depends on the motor specifications and driver setup. This should be modified accordingly if microstepping is used on the driver.
===void set_speed(uint16_t step_delay)===
===void set_speed(uint16_t step_delay)===
Set the motor speed.
Set the motor speed.
Line 75: Line 81:
rpm is the rotation speed.  
rpm is the rotation speed.  


This function will check for minimum speed to avoid a step_delay larger than 65535. Returns 0 is successful. 1 otherwise.
This function will check for minimum speed to avoid a step_delay larger than 65535us. Returns 0 is successful. 1 otherwise.


===void setDirection(bool dir);===
===void set_direction(bool dir);===


Set the direction.
Set the direction.


===void enable();===
===void enable();===
Enable the motor. Holds torque on the motor.
Enable the motor driver. Holds torque on the motor.


===void disable();===
===void disable();===
Line 89: Line 95:
===void step();===
===void step();===
Perform one single step.
Perform one single step.
Avoid using this in a loop to create motion as it will make more inter-mcu communications than actual motion.


===void start();===
===void start();===
Stop the motor.
Start the motor.


===void stop()===
===void stop()===
Line 97: Line 105:


===void brute_stop()===
===void brute_stop()===
Stop the motor immediately regardless of acceleration.
Stop the motor immediately regardless of acceleration state. If acceleration is off this will be no different than stop().
 
===void begin(uint8_t _address, uint16_t n_step)===
Initialiser to be used in Setup section of Arduino. ''address'' parameter is the i2c address of the motor controller. Usually 0x04 or 0x05.
 
n_step is the number of steps per revolution the motor executes. See relevant datasheet. This should be modified accordingly if microstepping.


===void goto_position(int32_t goto_position)===
===void goto_position(int32_t goto_position)===
Line 129: Line 132:


===bool running===
===bool running===
Indicates if the motor is running.
Boolean variable. Indicates if the motor is running.


===bool direction===
===bool direction===
Indicates the motor direction.
Boolean variable. Indicates the motor direction.
 
The assignment of True/False to clockwise and anticlockwise will depend on the wiring of the stepper motor. Adapt the code according to observed behaviour.


===float min_rpm===
===float min_rpm===
Line 143: Line 148:


Step delay currently used. This is updated when calling set_speed() and set_speed_rpm().
Step delay currently used. This is updated when calling set_speed() and set_speed_rpm().
Note this is not the instant step_delay as computed by acceleration. This is only the step delay for the target speed.


===bool acceleration===
===bool acceleration===


Returns true if acceleration is enabled. false otherwise.
If equals to true then acceleration is enabled. false otherwise.
 
==Usage Example==
 
The installation package contains a set of examples. We are showing the most basic example below.
 
More examples are in the zip file of the library itself.
 
<syntaxhighlight lang="python">
// simpledrive v1.3
// lechacal.com
// Example usage of stepperUNO library
// drive a single motor with left/right button
 
// library: stepperUNO_v1.4
 
#define Pot 3 // Potentiometer is on pin 3
 
#include <LiquidCrystal.h>
#include <stepperUNO.h>
 
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
keypad Keypad;
stepMOTOR motor1;
 
const int Nstep = 200; // Enter here the number of step/rev for your motor
 
void setup() {
 
  motor1.begin(0x4, Nstep); // Motor 1 is at address 0x5. Motor 2 on 0x4
  motor1.enable();
 
 
  lcd.begin(16, 2); //LCD INITIALISATION
  lcd.setCursor(0, 0);
  lcd.print("READY");
}
 
void loop() {
 
  int POTval = analogRead(Pot);
  int rpm = map(POTval, 0, 1023, 5, 500); // Speed range from 5 to 500rpm
 
  Keypad.ReadKey();
 
  if (Keypad.state == PRESSED_RIGHT) {
    motor1.set_direction(true);
    motor1.set_speed_rpm(rpm);
    motor1.start();
    lcd.setCursor(0, 0);
    lcd.print("RIGHT");
    Keypad.closeEvent(); // Must be here to indicate we are done with the event
  }
  else if (Keypad.state == PRESSED_LEFT) {
    motor1.set_direction(false);
    motor1.set_speed_rpm(rpm);
    motor1.start();
    lcd.setCursor(0, 0);
    lcd.print("LEFT ");
    Keypad.closeEvent();
  }
 
  else if ( Keypad.state == RELEASED_RIGHT ) {
    motor1.stop();
    lcd.setCursor(0, 0);
    lcd.print("READY");
    Keypad.closeEvent();
  }
  else if ( Keypad.state == RELEASED_LEFT ) {
    motor1.stop();
    lcd.setCursor(0, 0);
    lcd.print("READY");
    Keypad.closeEvent();
  }
 
}//end loop
</syntaxhighlight>
 
=Version history=
 
==1.4.1==
* Fixed bug where 'running' variable was not updating when using brute_stop.
* Added #define stepperUNO_1_4_1 for future library version control.
* Acceleration value was wrongly set as int16_t. Now using uint16_t.
* Added library.properties file.
 
==1.4==
* For use with motor controller flashed with attiny_stepperUNO_v1.2

Latest revision as of 14:42, 18 April 2022

Installation

Download the zip file from Stepper_UNO_v1.0#Files.
Copy the StepperUNO folder in the sketchbook/library directory.

Download

StepperUNO Library v1.4
StepperUNO Library v1.4.1

Configuration

Open the config.h file in the library/stepperUNO folder.
Change the KEYPAD_VERSION value as needed. Values should be 0 1 or 2. Use test and trials to find out which options works best for your keypad.

Function list

Keypad

uint8_t ReadKey()

This function should be called as often as possible. Typically inside the main loop. This will update the Keypad.state variable with one of the defined event below.

#define PRESSED_NONE    0
#define PRESSED_RIGHT   1
#define PRESSED_UP      2
#define PRESSED_DOWN    3
#define PRESSED_LEFT    4
#define PRESSED_SELECT  5

#define RELEASED_NONE   6
#define RELEASED_RIGHT  7
#define RELEASED_UP     8
#define RELEASED_DOWN   9
#define RELEASED_LEFT   10
#define RELEASED_SELECT 11

Note this function does not manage the RST button. Use RstReadKey instead.

bool RstReadKey()

Same as above but for the RST button only.

The RST button is a special case. It has its own pin to the arduino mcu.

#define PRESSED_RST       12
#define RELEASED_RST      13
#define LONG_PRESSED_RST  14
#define LONG_RELEASED_RST 15

On a conventional Arduino UNO these LCD keypad will connect the RST button directly to the reset of the arduino. On the stepperUNO we have rerouted this RST key so that it can be used for something else than reset. This is to explain why the RST key used different functions.

uint8_t state

Event state variable. This is equal to one of the states listed above once an event has occurred. It indicates what buttons have been pressed and released at any given time.

uint8_t rst_state

Same as state but for the RST button only.

void closeEvent()

Must be called once all action have been executed for a given event.

void rst_closeEvent()

Must be called once all action have been executed for a given event on the RST button.

Motor Control

void begin(uint8_t _address, uint16_t n_step)

Initialiser to be used in Setup section of Arduino.

address parameter is the i2c address of the motor controller. Usually 0x04 or 0x05.

n_step is the number of steps per revolution. This depends on the motor specifications and driver setup. This should be modified accordingly if microstepping is used on the driver.

void set_speed(uint16_t step_delay)

Set the motor speed.

step_delay is the delay between steps in microseconds.

uint8_t set_speed_rpm(float rpm)

Set the motor speed.

rpm is the rotation speed.

This function will check for minimum speed to avoid a step_delay larger than 65535us. Returns 0 is successful. 1 otherwise.

void set_direction(bool dir);

Set the direction.

void enable();

Enable the motor driver. Holds torque on the motor.

void disable();

Disable the motor. Allows the motor to spin freely.

void step();

Perform one single step.

Avoid using this in a loop to create motion as it will make more inter-mcu communications than actual motion.

void start();

Start the motor.

void stop()

Stop the motor. Will slow down and stop if acceleration is on.

void brute_stop()

Stop the motor immediately regardless of acceleration state. If acceleration is off this will be no different than stop().

void goto_position(int32_t goto_position)

Move motor to the given position. 'goto_position' is the position in terms of step number.

void set_position(int32_t new_position)

Modify set position. This is similar as setting the tare.

int32_t get_position()

Returns current motor position.

void set_acceleration(int16_t Acc)

Set the acceleration level. Default is 1000step/s/s.

void acceleration_on()

Set acceleration on.

void acceleration_off()

Set acceleration off.

bool running

Boolean variable. Indicates if the motor is running.

bool direction

Boolean variable. Indicates the motor direction.

The assignment of True/False to clockwise and anticlockwise will depend on the wiring of the stepper motor. Adapt the code according to observed behaviour.

float min_rpm

Returns the smallest rpm that can be used. Calculated when begin() is called.

float rpm

Current speed in rpm. This is only valid if the set_speed_rpm function is used.

uint16_t step_delay

Step delay currently used. This is updated when calling set_speed() and set_speed_rpm().

Note this is not the instant step_delay as computed by acceleration. This is only the step delay for the target speed.

bool acceleration

If equals to true then acceleration is enabled. false otherwise.

Usage Example

The installation package contains a set of examples. We are showing the most basic example below.

More examples are in the zip file of the library itself.

// simpledrive v1.3
// lechacal.com
// Example usage of stepperUNO library
// drive a single motor with left/right button

// library: stepperUNO_v1.4

#define Pot 3 // Potentiometer is on pin 3

#include <LiquidCrystal.h>
#include <stepperUNO.h>

LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
keypad Keypad;
stepMOTOR motor1;

const int Nstep = 200; // Enter here the number of step/rev for your motor

void setup() {

  motor1.begin(0x4, Nstep); // Motor 1 is at address 0x5. Motor 2 on 0x4
  motor1.enable();


  lcd.begin(16, 2); //LCD INITIALISATION
  lcd.setCursor(0, 0);
  lcd.print("READY");
}

void loop() {

  int POTval = analogRead(Pot);
  int rpm = map(POTval, 0, 1023, 5, 500); // Speed range from 5 to 500rpm

  Keypad.ReadKey();

  if (Keypad.state == PRESSED_RIGHT) {
    motor1.set_direction(true);
    motor1.set_speed_rpm(rpm);
    motor1.start();
    lcd.setCursor(0, 0);
    lcd.print("RIGHT");
    Keypad.closeEvent(); // Must be here to indicate we are done with the event 
  }
  else if (Keypad.state == PRESSED_LEFT) {
    motor1.set_direction(false);
    motor1.set_speed_rpm(rpm);
    motor1.start();
    lcd.setCursor(0, 0);
    lcd.print("LEFT ");
    Keypad.closeEvent();
  }

  else if ( Keypad.state == RELEASED_RIGHT ) {
    motor1.stop();
    lcd.setCursor(0, 0);
    lcd.print("READY");
    Keypad.closeEvent();
  }
  else if ( Keypad.state == RELEASED_LEFT ) {
    motor1.stop();
    lcd.setCursor(0, 0);
    lcd.print("READY");
    Keypad.closeEvent();
  }

}//end loop

Version history

1.4.1

  • Fixed bug where 'running' variable was not updating when using brute_stop.
  • Added #define stepperUNO_1_4_1 for future library version control.
  • Acceleration value was wrongly set as int16_t. Now using uint16_t.
  • Added library.properties file.

1.4

  • For use with motor controller flashed with attiny_stepperUNO_v1.2