update 13/05
This commit is contained in:
parent
6d1c70948f
commit
5fdca1699a
|
|
@ -0,0 +1,238 @@
|
||||||
|
#include <SoftwareSerial.h>
|
||||||
|
|
||||||
|
#define RX_PIN 13 // Connected to HC-05 TX pin
|
||||||
|
#define TX_PIN 12 // Connected to HC-05 RX pin
|
||||||
|
#define LED_PIN 2 // Connected to LED pin
|
||||||
|
#include <AccelStepper.h>
|
||||||
|
long receivedSteps = 0; //Number of steps
|
||||||
|
long receivedSpeed = 0; //Steps / second
|
||||||
|
long receivedAcceleration = 0; //Steps / second^2
|
||||||
|
char receivedCommand;
|
||||||
|
//-------------------------------------------------------------------------------
|
||||||
|
int directionMultiplier = 1;
|
||||||
|
int directionMultiplier2 = -1;// = 1: positive direction, = -1: negative direction
|
||||||
|
bool newData, runallowed = false; // booleans for new data from serial, and runallowed flag
|
||||||
|
AccelStepper stepper(1, 8, 9);// pulses Digital 8 (CLK) direction Digital 9 (CCW),
|
||||||
|
AccelStepper stepper2(1, 5, 6);
|
||||||
|
SoftwareSerial bluetooth(RX_PIN, TX_PIN); // Create a software serial object
|
||||||
|
|
||||||
|
|
||||||
|
void setup() {
|
||||||
|
Serial.begin(38400); // Initialize the serial monitor
|
||||||
|
bluetooth.begin(38400); // Initialize the Bluetooth communication
|
||||||
|
pinMode(LED_PIN, OUTPUT); // Set LED pin as output
|
||||||
|
stepper.setMaxSpeed(1000000); //SPEED = Steps / second
|
||||||
|
stepper.setAcceleration(80000); //ACCELERATION = Steps /(second)^2
|
||||||
|
stepper.disableOutputs(); //disable outputs
|
||||||
|
stepper2.setMaxSpeed(1000000); //SPEED = Steps / second
|
||||||
|
stepper2.setAcceleration(80000); //ACCELERATION = Steps /(second)^2
|
||||||
|
stepper2.disableOutputs(); //disable outputs
|
||||||
|
}
|
||||||
|
|
||||||
|
void loop() {
|
||||||
|
|
||||||
|
readinfo();
|
||||||
|
RunTheMotor();
|
||||||
|
}
|
||||||
|
|
||||||
|
void RunTheMotor() //function for the motor
|
||||||
|
{
|
||||||
|
if (runallowed == true)
|
||||||
|
{
|
||||||
|
stepper.enableOutputs(); //enable pins
|
||||||
|
stepper.run(); //step the motor (this will step the motor by 1 step at each loop)
|
||||||
|
stepper2.enableOutputs(); //enable pins
|
||||||
|
stepper2.run(); //step the motor (this will step the motor by 1 step at each loop)
|
||||||
|
}
|
||||||
|
else //program enters this part if the runallowed is FALSE, we do not do anything
|
||||||
|
{
|
||||||
|
stepper.disableOutputs(); //disable outputs
|
||||||
|
stepper2.disableOutputs(); //disable outputs
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void GoHome()
|
||||||
|
{
|
||||||
|
if (stepper.currentPosition() == 0)
|
||||||
|
{
|
||||||
|
bluetooth.println("We are at the home position.");
|
||||||
|
stepper.disableOutputs(); //disable power
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
stepper.setMaxSpeed(800); //set speed manually to 400. In this project 400 is 400 step/sec = 1 rev/sec.
|
||||||
|
stepper.moveTo(0); //set abolute distance to move
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void RotateRelative()
|
||||||
|
{
|
||||||
|
//We move X steps from the current position of the stepper motor in a given direction.
|
||||||
|
//The direction is determined by the multiplier (+1 or -1)
|
||||||
|
|
||||||
|
runallowed = true; //allow running - this allows entering the RunTheMotor() function.
|
||||||
|
stepper.setMaxSpeed(receivedSpeed); //set speed
|
||||||
|
stepper.move(directionMultiplier * receivedSteps); //set relative distance and direction
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void RotateAbsolute()
|
||||||
|
{
|
||||||
|
//We move to an absolute position.
|
||||||
|
//The AccelStepper library keeps track of the position.
|
||||||
|
//The direction is determined by the multiplier (+1 or -1)
|
||||||
|
//Why do we need negative numbers? - If you drive a threaded rod and the zero position is in the middle of the rod...
|
||||||
|
|
||||||
|
runallowed = true; //allow running - this allows entering the RunTheMotor() function.
|
||||||
|
stepper.setMaxSpeed(receivedSpeed); //set speed
|
||||||
|
stepper.moveTo(directionMultiplier * receivedSteps); //set relative distance
|
||||||
|
}
|
||||||
|
void GoHome2()
|
||||||
|
{
|
||||||
|
if (stepper2.currentPosition() == 0)
|
||||||
|
{
|
||||||
|
bluetooth.println("We are at the home position.");
|
||||||
|
stepper2.disableOutputs(); //disable power
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
stepper2.setMaxSpeed(800); //set speed manually to 400. In this project 400 is 400 step/sec = 1 rev/sec.
|
||||||
|
stepper2.moveTo(0); //set abolute distance to move
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void RotateRelative2()
|
||||||
|
{
|
||||||
|
//We move X steps from the current position of the stepper motor in a given direction.
|
||||||
|
//The direction is determined by the multiplier (+1 or -1)
|
||||||
|
|
||||||
|
runallowed = true; //allow running - this allows entering the RunTheMotor() function.
|
||||||
|
stepper2.setMaxSpeed(receivedSpeed); //set speed
|
||||||
|
stepper2.move(directionMultiplier2 * receivedSteps); //set relative distance and direction
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void RotateAbsolute2()
|
||||||
|
{
|
||||||
|
//We move to an absolute position.
|
||||||
|
//The AccelStepper library keeps track of the position.
|
||||||
|
//The direction is determined by the multiplier (+1 or -1)
|
||||||
|
//Why do we need negative numbers? - If you drive a threaded rod and the zero position is in the middle of the rod...
|
||||||
|
|
||||||
|
runallowed = true; //allow running - this allows entering the RunTheMotor() function.
|
||||||
|
stepper2.setMaxSpeed(receivedSpeed); //set speed
|
||||||
|
stepper2.moveTo(directionMultiplier2 * receivedSteps); //set relative distance
|
||||||
|
}
|
||||||
|
|
||||||
|
void readinfo()
|
||||||
|
{
|
||||||
|
if (bluetooth.available() > 0) //if something comes from the computer
|
||||||
|
{
|
||||||
|
receivedCommand = bluetooth.read(); // pass the value to the receivedCommad variable
|
||||||
|
newData = true; //indicate that there is a new data by setting this bool to true
|
||||||
|
|
||||||
|
if (newData == true) //we only enter this long switch-case statement if there is a new command from the computer
|
||||||
|
{
|
||||||
|
switch (receivedCommand) //we check what is the command
|
||||||
|
{
|
||||||
|
case 'L':
|
||||||
|
digitalWrite(LED_PIN, HIGH);
|
||||||
|
break;
|
||||||
|
case 'O':
|
||||||
|
digitalWrite(LED_PIN, LOW);
|
||||||
|
break;
|
||||||
|
case 'P': //P uses the move() function of the AccelStepper library, which means that it moves relatively to the current position.
|
||||||
|
|
||||||
|
receivedSteps = bluetooth.parseFloat(); //value for the steps
|
||||||
|
receivedSpeed = bluetooth.parseFloat(); //value for the speed
|
||||||
|
directionMultiplier = 1; //We define the direction
|
||||||
|
bluetooth.println("Positive direction."); //print the action
|
||||||
|
RotateRelative(); //Run the function
|
||||||
|
|
||||||
|
|
||||||
|
//example: P2000 400 - 2000 steps (5 revolution with 400 step/rev microstepping) and 400 steps/s speed
|
||||||
|
//In theory, this movement should take 5 seconds
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'N': //N uses the move() function of the AccelStepper library, which means that it moves relatively to the current position.
|
||||||
|
|
||||||
|
receivedSteps = bluetooth.parseFloat(); //value for the steps
|
||||||
|
receivedSpeed = bluetooth.parseFloat(); //value for the speed
|
||||||
|
directionMultiplier = -1; //We define the direction
|
||||||
|
bluetooth.println("Negative direction."); //print action
|
||||||
|
RotateRelative(); //Run the function
|
||||||
|
|
||||||
|
//example: N2000 400 - 2000 steps (5 revolution with 400 step/rev microstepping) and 500 steps/s speed; will rotate in the other direction
|
||||||
|
//In theory, this movement should take 5 seconds
|
||||||
|
break;
|
||||||
|
case '1': //avancer
|
||||||
|
|
||||||
|
receivedSteps = bluetooth.parseFloat(); //value for the steps
|
||||||
|
receivedSpeed = bluetooth.parseFloat(); //value for the speed
|
||||||
|
directionMultiplier = -1; //We define the direction
|
||||||
|
directionMultiplier2 = 1;
|
||||||
|
bluetooth.println("CA AVANCE."); //print the action
|
||||||
|
RotateRelative();
|
||||||
|
RotateRelative2(); //Run the function
|
||||||
|
|
||||||
|
|
||||||
|
//example: P2000 400 - 2000 steps (5 revolution with 400 step/rev microstepping) and 400 steps/s speed
|
||||||
|
//In theory, this movement should take 5 seconds
|
||||||
|
break;
|
||||||
|
case '2': //reculer
|
||||||
|
|
||||||
|
receivedSteps = bluetooth.parseFloat(); //value for the steps
|
||||||
|
receivedSpeed = bluetooth.parseFloat(); //value for the speed
|
||||||
|
directionMultiplier = 1; //We define the direction
|
||||||
|
directionMultiplier2 = -1;
|
||||||
|
bluetooth.println("CA RECULE"); //print the action
|
||||||
|
RotateRelative();
|
||||||
|
RotateRelative2(); //Run the function
|
||||||
|
|
||||||
|
|
||||||
|
//example: P2000 400 - 2000 steps (5 revolution with 400 step/rev microstepping) and 400 steps/s speed
|
||||||
|
//In theory, this movement should take 5 seconds
|
||||||
|
break;
|
||||||
|
case '3': //a gauche
|
||||||
|
|
||||||
|
receivedSteps = bluetooth.parseFloat(); //value for the steps
|
||||||
|
receivedSpeed = bluetooth.parseFloat(); //value for the speed
|
||||||
|
directionMultiplier = 1; //We define the direction
|
||||||
|
directionMultiplier2 = 1;
|
||||||
|
bluetooth.println("CA TOURNE A GAUCHE"); //print the action
|
||||||
|
RotateRelative();
|
||||||
|
RotateRelative2(); //Run the function
|
||||||
|
|
||||||
|
|
||||||
|
//example: P2000 400 - 2000 steps (5 revolution with 400 step/rev microstepping) and 400 steps/s speed
|
||||||
|
//In theory, this movement should take 5 seconds
|
||||||
|
break;
|
||||||
|
|
||||||
|
case '4': //a droite.
|
||||||
|
|
||||||
|
receivedSteps = bluetooth.parseFloat(); //value for the steps
|
||||||
|
receivedSpeed = bluetooth.parseFloat(); //value for the speed
|
||||||
|
directionMultiplier = -1; //We define the direction
|
||||||
|
directionMultiplier2 = -1;
|
||||||
|
bluetooth.println("CA TOURNE A DROITE"); //print the action
|
||||||
|
RotateRelative();
|
||||||
|
RotateRelative2(); //Run the function
|
||||||
|
|
||||||
|
|
||||||
|
//example: P2000 400 - 2000 steps (5 revolution with 400 step/rev microstepping) and 400 steps/s speed
|
||||||
|
//In theory, this movement should take 5 seconds
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -20,7 +20,12 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <AccelStepper.h>
|
#include <AccelStepper.h>
|
||||||
|
#include <SoftwareSerial.h>
|
||||||
|
|
||||||
|
#define RX_PIN 13 // Connected to HC-05 TX pin
|
||||||
|
#define TX_PIN 12 // Connected to HC-05 RX pin
|
||||||
|
|
||||||
|
SoftwareSerial bluetooth(RX_PIN, TX_PIN); // Create a software serial object
|
||||||
//User-defined values
|
//User-defined values
|
||||||
long receivedSteps = 0; //Number of steps
|
long receivedSteps = 0; //Number of steps
|
||||||
long receivedSpeed = 0; //Steps / second
|
long receivedSpeed = 0; //Steps / second
|
||||||
|
|
@ -31,11 +36,12 @@ int directionMultiplier = 1;
|
||||||
int directionMultiplier2 = -1;// = 1: positive direction, = -1: negative direction
|
int directionMultiplier2 = -1;// = 1: positive direction, = -1: negative direction
|
||||||
bool newData, runallowed = false; // booleans for new data from serial, and runallowed flag
|
bool newData, runallowed = false; // booleans for new data from serial, and runallowed flag
|
||||||
AccelStepper stepper(1, 8, 9);// pulses Digital 8 (CLK) direction Digital 9 (CCW),
|
AccelStepper stepper(1, 8, 9);// pulses Digital 8 (CLK) direction Digital 9 (CCW),
|
||||||
AccelStepper stepper2(1, 4, 5);
|
AccelStepper stepper2(1, 5, 6);
|
||||||
|
|
||||||
void setup()
|
void setup()
|
||||||
{
|
{
|
||||||
Serial.begin(9600); //define baud rate
|
Serial.begin(38400); // Initialize the serial monitor
|
||||||
|
bluetooth.begin(38400); // Initialize the Bluetooth communication
|
||||||
Serial.println("Demonstration of AccelStepper Library"); //print a messages
|
Serial.println("Demonstration of AccelStepper Library"); //print a messages
|
||||||
Serial.println("Send 'C' for printing the commands.");
|
Serial.println("Send 'C' for printing the commands.");
|
||||||
|
|
||||||
|
|
@ -53,9 +59,8 @@ void loop()
|
||||||
{
|
{
|
||||||
//Constantly looping through these 2 functions.
|
//Constantly looping through these 2 functions.
|
||||||
//We only use non-blocking commands, so something else (should also be non-blocking) can be done during the movement of the motor
|
//We only use non-blocking commands, so something else (should also be non-blocking) can be done during the movement of the motor
|
||||||
|
checkSerial();
|
||||||
checkSerial(); //check serial port for new commands
|
checkbluetooth();
|
||||||
|
|
||||||
RunTheMotor(); //function to handle the motor
|
RunTheMotor(); //function to handle the motor
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -273,11 +278,6 @@ void checkSerial() //function for receiving the commands
|
||||||
Serial.print("The current position is updated to: "); //Print message
|
Serial.print("The current position is updated to: "); //Print message
|
||||||
Serial.println(stepper2.currentPosition()); //Check position after reset.
|
Serial.println(stepper2.currentPosition()); //Check position after reset.
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'T':
|
|
||||||
|
|
||||||
PrintCommands2(); //Print the commands for controlling the motor
|
|
||||||
break;
|
|
||||||
case '1': //avancer
|
case '1': //avancer
|
||||||
|
|
||||||
receivedSteps = Serial.parseFloat(); //value for the steps
|
receivedSteps = Serial.parseFloat(); //value for the steps
|
||||||
|
|
@ -438,18 +438,4 @@ void RotateAbsolute2()
|
||||||
stepper2.setMaxSpeed(receivedSpeed); //set speed
|
stepper2.setMaxSpeed(receivedSpeed); //set speed
|
||||||
stepper2.moveTo(directionMultiplier2 * receivedSteps); //set relative distance
|
stepper2.moveTo(directionMultiplier2 * receivedSteps); //set relative distance
|
||||||
}
|
}
|
||||||
|
|
||||||
void PrintCommands2()
|
|
||||||
{
|
|
||||||
//Printing the commands
|
|
||||||
Serial.println(" 'T' : Prints all the commands and their functions.");
|
|
||||||
Serial.println(" 'Y' : Rotates the motor in positive (CW) direction, relative.");
|
|
||||||
Serial.println(" 'y' : Rotates the motor in negative (CCW) direction, relative.");
|
|
||||||
Serial.println(" 'O' : Rotates the motor to an absolute positive position (+).");
|
|
||||||
Serial.println(" 'o' : Rotates the motor to an absolute negative position (-).");
|
|
||||||
Serial.println(" 'M' : Stops the motor immediately.");
|
|
||||||
Serial.println(" 'K' : Sets an acceleration value.");
|
|
||||||
Serial.println(" 'G' : Prints the current position/location of the motor.");
|
|
||||||
Serial.println(" 'V' : Goes back to 0 position from the current position (homing).");
|
|
||||||
Serial.println(" 'E' : Updates the position current position and makes it as the new 0 position. ");
|
|
||||||
}
|
|
||||||
Loading…
Reference in New Issue