From 5fdca1699a69b32cd0abfe7c153e10ef9761a8c5 Mon Sep 17 00:00:00 2001 From: theyatokami Date: Sat, 13 May 2023 23:23:34 +0200 Subject: [PATCH] update 13/05 --- .../bluetooth_control/bluetooth_control.ino | 238 ++++++++++++++++++ .../stepperspeed/stepperspeed.ino | 38 +-- 2 files changed, 250 insertions(+), 26 deletions(-) create mode 100644 Deplacement-robot/bluetooth_control/bluetooth_control.ino diff --git a/Deplacement-robot/bluetooth_control/bluetooth_control.ino b/Deplacement-robot/bluetooth_control/bluetooth_control.ino new file mode 100644 index 0000000..301d4e2 --- /dev/null +++ b/Deplacement-robot/bluetooth_control/bluetooth_control.ino @@ -0,0 +1,238 @@ +#include + +#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 +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; + } + } + } + +} diff --git a/Deplacement-robot/stepperspeed/stepperspeed.ino b/Deplacement-robot/stepperspeed/stepperspeed.ino index 36a6bab..5cb3b52 100644 --- a/Deplacement-robot/stepperspeed/stepperspeed.ino +++ b/Deplacement-robot/stepperspeed/stepperspeed.ino @@ -20,7 +20,12 @@ */ #include - +#include + +#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 long receivedSteps = 0; //Number of steps long receivedSpeed = 0; //Steps / second @@ -31,11 +36,12 @@ 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, 4, 5); +AccelStepper stepper2(1, 5, 6); 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("Send 'C' for printing the commands."); @@ -53,9 +59,8 @@ void loop() { //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 - - checkSerial(); //check serial port for new commands - + checkSerial(); + checkbluetooth(); 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.println(stepper2.currentPosition()); //Check position after reset. break; - - case 'T': - - PrintCommands2(); //Print the commands for controlling the motor - break; case '1': //avancer receivedSteps = Serial.parseFloat(); //value for the steps @@ -438,18 +438,4 @@ void RotateAbsolute2() stepper2.setMaxSpeed(receivedSpeed); //set speed 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. "); -} \ No newline at end of file + \ No newline at end of file