Serial functions and general firmware functionality seem to be working. Need to test on real hardware with motors and test softserial next.

This commit is contained in:
Matt McWilliams 2023-02-13 14:45:01 -05:00
parent f5458ba8f1
commit c16233dfda
1 changed files with 124 additions and 63 deletions

View File

@ -22,17 +22,20 @@
#define Fbutton 3 #define Fbutton 3
#define Bbutton 4 #define Bbutton 4
const frameTime = 40; //ms const int frameTime = 40; //ms
const frameSpeed = 255; const int frameSpeed = 255;
const int buttonPress = 100; //ms
/** /**
* STATE * STATE
**/ **/
boolean debug_state = false;
const int Buttons[2] = {Fbutton, Bbutton}; const int Buttons[2] = {Fbutton, Bbutton};
volatile int ButtonState[4] = {1, 1}; volatile int ButtonState[2] = {1, 1};
volatile long ButtonTime[4] = {0, 0}; volatile long ButtonTime[2] = {0, 0};
volatile long buttontime = 0; volatile int ButtonMultiple[2] = {0, 0};
volatile long buttonTime = 0;
const int Fmotor = 0; const int Fmotor = 0;
const int Bmotor = 1; const int Bmotor = 1;
@ -40,16 +43,23 @@ const int Bmotor = 1;
volatile int Fspeed = 255; volatile int Fspeed = 255;
volatile int Bspeed = 255; volatile int Bspeed = 255;
volatile long Ftime = 0;
volatile long Btime = 0;
volatile boolean Frunning = false; volatile boolean Frunning = false;
volatile boolean Brunning = false; volatile boolean Brunning = false;
voilatile long timer = 0; volatile long timer = 0;
/** /**
* SERIAL * SERIAL
**/ **/
volatile char cmdChar = 'z'; volatile char cmdChar = 'z';
const char cmd_debug = 'd';
const char cmd_connect = 'i';
const char cmd_mcopy_identifier = 'm';
const char cmd_takeup_identifier = 'F';
const char Fcmd = 'D'; const char Fcmd = 'D';
const char Bcmd = 'E'; const char Bcmd = 'E';
@ -76,32 +86,78 @@ void setup() {
} }
void loop() { void loop() {
timer = millis();
if (Serial.available()) { if (Serial.available()) {
cmdChar = (char)Serial.read(); cmdChar = (char)Serial.read();
} }
if (cmdChar != 'z') {
cmd(cmdChar);
}
if (softSerial.available() > 0) {
cmdChar = (char)softSerial.read();
}
if (cmdChar != 'z') { if (cmdChar != 'z') {
cmd(cmdChar); cmd(cmdChar);
} }
cmdChar = 'z'; cmdChar = 'z';
if (softSerial.available() > 0) {
cmdChar = (char)softSerial.read();
}
cmdChar = 'z';
btn(Fmotor);
btn(Bmotor);
if (Brunning || Frunning) {
monitor();
}
} }
void cmd (char which) { void cmd (char which) {
if (which == Fcmd && !Frunning) {
forward();
} else if (which == Bcmd && !Brunning) {
backward();
} else if (which == cmd_debug) {
debug();
} else if (which == cmd_connect) {
connect();
} else if (which == cmd_mcopy_identifier) {
identify();
}
}
void forward () {
log("forward()");
Ftime = timer;
set_speed(Fmotor, frameSpeed);
clockwise(Fmotor);
}
void backward () {
log("backward()");
Btime = timer;
set_speed(Bmotor, frameSpeed);
counter_clockwise(Bmotor);
}
void monitor () {
if (Frunning && timer - Ftime >= frameTime) {
stop(Fmotor);
}
if (Brunning && timer - Btime >= frameTime) {
stop(Bmotor);
}
}
void stop (int motor) {
brake_motor(motor);
} }
//input value from 0 to 255 //input value from 0 to 255
void set_speed (int motor, int val){ void set_speed (int motor, int val){
if (motor == 0) { if (motor == Fmotor) {
Fspeed = val; Fspeed = val;
} else if (motor == 1) { } else if (motor == Bmotor) {
Bspeed = val; Bspeed = val;
} }
} }
@ -117,15 +173,15 @@ void clockwise_fade (int motor, int startSpeed, int stopSpeed, int ms) {
analogWrite(Bneg, 0); analogWrite(Bneg, 0);
} }
for (int i = 0; i < steps; i++) { for (int i = 0; i < steps; i++) {
if (motor == Fmotor) { if (motor == Fmotor) {
analogWrite(Fpos, motorSpeed); analogWrite(Fpos, motorSpeed);
Frunning = true; Frunning = true;
} else if (motor == Bmotor) { } else if (motor == Bmotor) {
analogWrite(Bpos, motorSpeed); analogWrite(Bpos, motorSpeed);
Brunning = true; Brunning = true;
} }
delay(stepMs); delay(stepMs);
motorSpeed += stopSpeed > startSpeed ? 1 : -1; motorSpeed += stopSpeed > startSpeed ? 1 : -1;
} }
if (motor == Fmotor) { if (motor == Fmotor) {
Fspeed = stopSpeed; Fspeed = stopSpeed;
@ -145,15 +201,15 @@ void counter_clockwise_fade (int motor, int startSpeed, int stopSpeed, int ms) {
analogWrite(Bpos, 0); analogWrite(Bpos, 0);
} }
for (int i = 0; i < steps; i++) { for (int i = 0; i < steps; i++) {
if (motor == Fmotor) { if (motor == Fmotor) {
analogWrite(Fneg, motorSpeed); analogWrite(Fneg, motorSpeed);
Frunning = true; Frunning = true;
} else if (motor == Bmotor) { } else if (motor == Bmotor) {
analogWrite(Bneg, motorSpeed); analogWrite(Bneg, motorSpeed);
Brunning = true; Brunning = true;
} }
delay(stepMs); delay(stepMs);
motorSpeed += stopSpeed > startSpeed ? 1 : -1; motorSpeed += stopSpeed > startSpeed ? 1 : -1;
} }
if (motor == Fmotor) { if (motor == Fmotor) {
Fspeed = stopSpeed; Fspeed = stopSpeed;
@ -188,14 +244,14 @@ void counter_clockwise (int motor){
void brake (){ void brake (){
if (Frunning) { if (Frunning) {
brakeMotor(Fmotor); brake_motor(Fmotor);
} }
if (Brunning) { if (Brunning) {
brakeMotor(Bmotor); brake_motor(Bmotor);
} }
} }
void brakeMotor (int motor) { void brake_motor (int motor) {
if (motor == Fmotor) { if (motor == Fmotor) {
digitalWrite(Fpos, LOW); digitalWrite(Fpos, LOW);
digitalWrite(Fneg, LOW); digitalWrite(Fneg, LOW);
@ -219,42 +275,47 @@ void btn (int index) {
int val = digitalRead(Buttons[index]); int val = digitalRead(Buttons[index]);
if (val != ButtonState[index]) { if (val != ButtonState[index]) {
if (val == LOW) { // pressed if (val == LOW) { // pressed
ButtonTime[index] = millis(); ButtonTime[index] = timer;
button_start(index, buttontime); ButtonMultiple[index] = -1;
} else if (val == HIGH) { // not pressed
buttontime = millis() - ButtonTime[index];
button_end(index, buttontime);
} }
} }
if (val == LOW) { if (val == LOW) {
buttonTime = timer - ButtonTime[index];
btnCmd(index, buttonTime);
} }
ButtonState[index] = val; ButtonState[index] = val;
} }
/* ------------------------------------------------ void btnCmd (int index, int time) {
* Determines a specific action for each press length int multiple = floor(time / buttonPress);
* of each button. if (multiple > ButtonMultiple[index]) {
* ------------------------------------------------*/ if (index == Fmotor) {
void button_end (int index, long buttontime) { cmd(Fcmd);
if (index == 0) { //forward } else if (index == Bmotor) {
if (buttontime > 10) { cmd(Bcmd);
//
}
} else if (index == 1) { //backward
if (buttontime > 10) {
//
} }
ButtonMultiple[index] = multiple;
} }
buttontime = 0;
} }
/** void connect () {
* Vex = Higher voltage that can power the motor and/or the shield as well as the Arduino if wanted Serial.println(cmd_connect);
* 5V = Must be a clean 5V that can power the Arduino and shield, and if wanted also the motor itself log("connect()");
* GND = as always, the ground of everything }
* B- = Connection of the first motor or first winding of the stepper motor
* B+ = Connection of the first motor or first winding of the stepper motor void identify () {
* A- = Connection of the second motor or second winding of the stepper motor Serial.println(cmd_takeup_identifier);
* A+ = Connection of the second motor or second winding of the stepper motor log("identify()");
**/ }
void debug () {
debug_state = true;
Serial.println(cmd_debug);
log("debugging enabled");
}
void log (String msg) {
if (debug_state) {
Serial.println(msg);
}
}