Hallo,
ich habe mir mit "MIT App inventor 2" ein Programm geschrieben, mit dem ich Befehle (in dem Fall Buchstaben) per Bluetooth an den Arduino senden kann. Das funktioniert soweit auch. Wenn ich Homen drücke, sendet das Programm ein "h" und der Arduino dreht den Motor bis ich einen Taster betätige und fährt dann kurz zurück. Allerings, wenn ich "Vorwärts" (v) oder "Rückwärts" (r) drücke, dann dreht der Motor viel langsamer. Woran könnte das liegen?
#include <AccelStepper.h>
#include <MultiStepper.h>
#include <TMC2208Stepper.h> // Include library
TMC2208Stepper driver = TMC2208Stepper(&Serial2); // Create driver and use
// HardwareSerial2 for communication
#define limitSwitch 50
#define EN_PIN 53
AccelStepper stepper(1, 52, 51); // (Type:driver, STEP, DIR)
char receivedCommand; // character for commands
long stepper_current; // number for stepper current in milliampere
long micro_steps; // number for microstep setting of the driver
long set_start;
long set_end;
bool newData, runallowed = false; // booleans for new data from serial, and runallowed flag
void setup() {
Serial.begin(115200); //define baud rate
Serial1.begin(9600); //define HC-06 Bluetooth baud rate
Serial2.begin(115200); //TMC2208 UART Baudrate
Serial.println("Focus Stacking Bot Bereit!"); //print a message
driver.pdn_disable(1); // Use PDN/UART pin for communication
driver.I_scale_analog(0); // Adjust current from the registers
driver.rms_current(700); // Set driver current 700mA
driver.mstep_reg_select(1); // Set microsteps using pins or Register
driver.mres(8); //
driver.microsteps(64); // Set driver microsteps
driver.toff(0x2); // Enable driver
// Set initial seed values for the steppers
stepper.setMaxSpeed(6000);
stepper.setAcceleration(200); //ACCELERATION = Steps /(second)^2
stepper.setSpeed(2000);
pinMode(limitSwitch, INPUT_PULLUP);
pinMode(EN_PIN, OUTPUT);
}
void loop() {
checkSerial(); //check serial port for new commands
// Limiting the movement - Do nothing if limit switch pressed or distance traveled in other direction greater then 80cm
while (digitalRead(limitSwitch) == 0 || stepper.currentPosition() < -64800) {}
}
void checkSerial() //method for receiving the commands
{
//switch-case would also work, and maybe more elegant
if (Serial1.available() > 0) //if something comes
{
receivedCommand = Serial1.read(); // this will read the command character
newData = true; //this creates a flag
}
if (newData == true) //if we received something (see above)
{
//HOMING
if (receivedCommand == 'h') //homing, this movement will be interrupted via the attachInterrupt() triggered by the microswitch.
{
runallowed = true; //allow running
Serial.println("HOMING!"); //print action
while (digitalRead(limitSwitch) != 0) {
digitalWrite(53, LOW); // Enable driver
stepper.setSpeed(6000);
stepper.runSpeed();
stepper.setCurrentPosition(0); // When limit switch pressed set position to 0 steps
}
delay(20);
// Move 200 steps back from the limit switch
while (stepper.currentPosition() != -200) {
stepper.setSpeed(-6000);
stepper.run();
}
Serial.println("HOMING FINISHED!"); //print action
}
}
//Vorwärts Verfahren
if (receivedCommand == 'v') {
runallowed = true; //allow running
digitalWrite(53, LOW); // Enable driver
stepper.setSpeed(6000);
stepper.runSpeed();
Serial.print("Stepper Position: ");
Serial.println(stepper.currentPosition() );
while (receivedCommand == 's') {
stepper.stop();
Serial.println("Stop!");
}
}
//Rückwärts Verfahren
if (receivedCommand == 'r') {
runallowed = true; //allow running
digitalWrite(53, LOW); // Enable driver
stepper.setSpeed(-6000);
stepper.runSpeed();
Serial.print("Stepper Position: ");
Serial.println(stepper.currentPosition() );
while (receivedCommand == 's') {
stepper.stop();
Serial.println("Stop!");
}
}
//after we went through the above tasks, newData becomes false again, so we are ready to receive new commands again.
newData = false;
}