diff --git a/Deplacement-robot/arrowkeys/arrowkey.py b/Deplacement-robot/arrowkeys/arrowkey.py new file mode 100644 index 0000000..c8b9ed2 --- /dev/null +++ b/Deplacement-robot/arrowkeys/arrowkey.py @@ -0,0 +1,26 @@ +import serial +import time +import keyboard + +ser = serial.Serial('COM16', 38400) # Replace '/dev/ttyACM0' with your Arduino's port + +def send_command(command): + ser.write(command.encode()) + time.sleep(0.1) + +def check_key(key, command): + while True: + if keyboard.is_pressed(key): + send_command(command) + else: + send_command('0') + break + +# binding keys to commands +keyboard.add_hotkey('up', check_key, args=('up', '1')) # forward +keyboard.add_hotkey('down', check_key, args=('down', '2')) # backward +keyboard.add_hotkey('left', check_key, args=('left', '3')) # left +keyboard.add_hotkey('right', check_key, args=('right', '4')) # right +keyboard.add_hotkey('space', send_command, args=('0',)) # stop + +keyboard.wait('esc') # wait for 'esc' to exit the program diff --git a/Deplacement-robot/arrowkeys/arrowkeys/arrowkeys.ino b/Deplacement-robot/arrowkeys/arrowkeys/arrowkeys.ino new file mode 100644 index 0000000..223b6a5 --- /dev/null +++ b/Deplacement-robot/arrowkeys/arrowkeys/arrowkeys.ino @@ -0,0 +1,117 @@ +#include +#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 + +long receivedSpeed = 800; //Steps / second +char receivedCommand; + +int directionMultiplier = 1; +int directionMultiplier2 = -1; +bool runallowed = false; + +AccelStepper stepper(1, 8, 9); +AccelStepper stepper2(1, 5, 6); + +SoftwareSerial bluetooth(RX_PIN, TX_PIN); + +void setup() { + Serial.begin(38400); + bluetooth.begin(38400); + pinMode(LED_PIN, OUTPUT); + + stepper.setMaxSpeed(10000); + stepper.setAcceleration(1000); + stepper.disableOutputs(); + + stepper2.setMaxSpeed(10000); + stepper2.setAcceleration(1000); + stepper2.disableOutputs(); +} + +void loop() { + readinfo(); + RunTheMotor(); +} + +void RunTheMotor() +{ + if (runallowed) + { + stepper.enableOutputs(); + stepper.runSpeed(); // use runSpeed instead of run + stepper2.enableOutputs(); + stepper2.runSpeed(); // use runSpeed instead of run + } + else + { + stepper.disableOutputs(); + stepper.stop(); // stop the stepper + stepper2.disableOutputs(); + stepper2.stop(); // stop the stepper + } +} + +void RotateRelative() +{ + runallowed = true; + stepper.setSpeed(directionMultiplier * receivedSpeed); // set speed and direction +} + +void RotateRelative2() +{ + runallowed = true; + stepper2.setSpeed(directionMultiplier2 * receivedSpeed); // set speed and direction +} + +void readinfo() +{ + if (bluetooth.available() > 0) + { + receivedCommand = bluetooth.read(); + + switch (receivedCommand) + { + case '1': // move forward + directionMultiplier = -1; + directionMultiplier2 = 1; + bluetooth.println("Moving forward."); + RotateRelative(); + RotateRelative2(); + break; + + case '2': // move backward + directionMultiplier = 1; + directionMultiplier2 = -1; + bluetooth.println("Moving backward."); + RotateRelative(); + RotateRelative2(); + break; + + case '3': // turn left + directionMultiplier = 1; + directionMultiplier2 = 1; + bluetooth.println("Turning left."); + RotateRelative(); + RotateRelative2(); + break; + + case '4': // turn right + directionMultiplier = -1; + directionMultiplier2 = -1; + bluetooth.println("Turning right."); + RotateRelative(); + RotateRelative2(); + break; + case '0': //stop + runallowed = false; + bluetooth.println("Stop."); + break; + + default: + break; + } + } +} diff --git a/Deplacement-robot/bluetooth_control/bluetooth_control.ino b/Deplacement-robot/bluetooth_control/bluetooth_control.ino index 301d4e2..68c202a 100644 --- a/Deplacement-robot/bluetooth_control/bluetooth_control.ino +++ b/Deplacement-robot/bluetooth_control/bluetooth_control.ino @@ -21,11 +21,11 @@ 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.setMaxSpeed(10000); //SPEED = Steps / second + stepper.setAcceleration(800); //ACCELERATION = Steps /(second)^2 stepper.disableOutputs(); //disable outputs - stepper2.setMaxSpeed(1000000); //SPEED = Steps / second - stepper2.setAcceleration(80000); //ACCELERATION = Steps /(second)^2 + stepper2.setMaxSpeed(10000); //SPEED = Steps / second + stepper2.setAcceleration(800); //ACCELERATION = Steps /(second)^2 stepper2.disableOutputs(); //disable outputs } diff --git a/Deplacement-robot/stepperspeed/stepperspeed.ino b/Deplacement-robot/stepperspeed/stepperspeed.ino index 5cb3b52..12ad582 100644 --- a/Deplacement-robot/stepperspeed/stepperspeed.ino +++ b/Deplacement-robot/stepperspeed/stepperspeed.ino @@ -60,7 +60,6 @@ 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(); - checkbluetooth(); RunTheMotor(); //function to handle the motor }