quoicoubeh
This commit is contained in:
parent
5fdca1699a
commit
a14db7e880
|
|
@ -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
|
||||||
|
|
@ -0,0 +1,117 @@
|
||||||
|
#include <SoftwareSerial.h>
|
||||||
|
#include <AccelStepper.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
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -21,11 +21,11 @@ void setup() {
|
||||||
Serial.begin(38400); // Initialize the serial monitor
|
Serial.begin(38400); // Initialize the serial monitor
|
||||||
bluetooth.begin(38400); // Initialize the Bluetooth communication
|
bluetooth.begin(38400); // Initialize the Bluetooth communication
|
||||||
pinMode(LED_PIN, OUTPUT); // Set LED pin as output
|
pinMode(LED_PIN, OUTPUT); // Set LED pin as output
|
||||||
stepper.setMaxSpeed(1000000); //SPEED = Steps / second
|
stepper.setMaxSpeed(10000); //SPEED = Steps / second
|
||||||
stepper.setAcceleration(80000); //ACCELERATION = Steps /(second)^2
|
stepper.setAcceleration(800); //ACCELERATION = Steps /(second)^2
|
||||||
stepper.disableOutputs(); //disable outputs
|
stepper.disableOutputs(); //disable outputs
|
||||||
stepper2.setMaxSpeed(1000000); //SPEED = Steps / second
|
stepper2.setMaxSpeed(10000); //SPEED = Steps / second
|
||||||
stepper2.setAcceleration(80000); //ACCELERATION = Steps /(second)^2
|
stepper2.setAcceleration(800); //ACCELERATION = Steps /(second)^2
|
||||||
stepper2.disableOutputs(); //disable outputs
|
stepper2.disableOutputs(); //disable outputs
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -60,7 +60,6 @@ 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();
|
||||||
checkbluetooth();
|
|
||||||
RunTheMotor(); //function to handle the motor
|
RunTheMotor(); //function to handle the motor
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue