-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
89 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#include "SerialTalker.h" | ||
|
||
SerialTalker::SerialTalker() {} | ||
|
||
float* SerialTalker::rx() { | ||
Serial.readBytesUntil('\n', buffer, 17); | ||
for (int i = 0; i < 4; i++) { | ||
n1.b[i] = buffer[i]; | ||
n2.b[i] = buffer[i + 4]; | ||
n3.b[i] = buffer[i + 8]; | ||
n4.b[i] = buffer[i + 12]; | ||
} | ||
rpms[0] = n1.f; | ||
rpms[1] = n2.f; | ||
rpms[2] = n3.f; | ||
rpms[3] = n4.f; | ||
return rpms; | ||
} | ||
|
||
void SerialTalker::tx(float values[4]) { | ||
rpms[0] = values[0]; | ||
rpms[1] = values[1]; | ||
rpms[2] = values[2]; | ||
rpms[3] = values[3]; | ||
Serial.write((byte*)&rpms, sizeof(float)*4); | ||
Serial.write('\n'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#include <Arduino.h> | ||
|
||
class SerialTalker | ||
{ | ||
private: | ||
byte buffer[17]; | ||
union { float f; byte b[4]; } n1; | ||
union { float f; byte b[4]; } n2; | ||
union { float f; byte b[4]; } n3; | ||
union { float f; byte b[4]; } n4; | ||
float rpms[4]; | ||
|
||
public: | ||
SerialTalker(); | ||
float* rx(); | ||
void tx(float rpms[4]); | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,113 +1,31 @@ | ||
#include <PinChangeInterrupt.h> | ||
// #include "Wheel.h" | ||
#include "Car.h" | ||
#include "SerialTalker.h" | ||
|
||
int intervalMillis = 40; // millis | ||
Car car(1, 2, 3, 4, 2, 13, 18, 19, intervalMillis); | ||
// Wheel w1(1, 2, intervalMillis); | ||
// Wheel w2(2, 13, intervalMillis); | ||
// Wheel w3(3, 18, intervalMillis); | ||
// Wheel w4(4, 19, intervalMillis); | ||
SerialTalker talker; | ||
int intervalMillis = 40; | ||
int encoderPins[4] = {2, 13, 18, 19}; | ||
Car car(encoderPins, intervalMillis); | ||
void updateW1() { car.incEnc1(); } | ||
void updateW2() { car.incEnc2(); } | ||
void updateW3() { car.incEnc3(); } | ||
void updateW4() { car.incEnc4(); } | ||
// void updateW1() { w1.incEnc(); } | ||
// void updateW2() { w2.incEnc(); } | ||
// void updateW3() { w3.incEnc(); } | ||
// void updateW4() { w4.incEnc(); } | ||
int vel = 0; | ||
int u = 0; | ||
|
||
unsigned long now = 0; | ||
unsigned long prev = 0; | ||
unsigned long timer = 5000; | ||
bool state = false; | ||
|
||
void setup() { | ||
attachInterrupt(digitalPinToInterrupt(car.getEncPin1()), updateW1, RISING); | ||
attachPCINT(digitalPinToPCINT(car.getEncPin2()), updateW2, RISING); | ||
attachInterrupt(digitalPinToInterrupt(car.getEncPin3()), updateW3, RISING); | ||
attachInterrupt(digitalPinToInterrupt(car.getEncPin4()), updateW4, RISING); | ||
// attachInterrupt(digitalPinToInterrupt(w1.getEncPin()), updateW1, RISING); | ||
// attachPCINT(digitalPinToPCINT(w2.getEncPin()), updateW2, RISING); | ||
// attachInterrupt(digitalPinToInterrupt(w3.getEncPin()), updateW3, RISING); | ||
// attachInterrupt(digitalPinToInterrupt(w4.getEncPin()), updateW4, RISING); | ||
|
||
Serial.begin(9600); | ||
Serial.begin(115200); | ||
Serial.setTimeout(intervalMillis); | ||
} | ||
|
||
void userInput() { | ||
int parsedInt = Serial.parseInt(); | ||
if (parsedInt != 0) { | ||
parsedInt = constrain(parsedInt, -255, 256); | ||
if (parsedInt == 256) { parsedInt = 0; } | ||
vel = parsedInt; | ||
Serial.print("Set velocity: "); | ||
Serial.println(vel); | ||
} | ||
} | ||
|
||
void runMotors() { | ||
// u = w1.reachVelocity(vel); | ||
// w1.setMotorValue(vel); | ||
// w1.reachVelocity(vel); | ||
// w2.reachVelocity(vel); | ||
// w3.reachVelocity(vel); | ||
// w4.reachVelocity(vel); | ||
car.runForward(vel); | ||
} | ||
|
||
void sendData() { | ||
Serial.print('$'); | ||
|
||
// Serial.print(vel); | ||
// Serial.print(' '); | ||
// Serial.print(u); | ||
// Serial.print(' '); | ||
// Serial.print(w1.getRPM(), 2); | ||
|
||
Serial.print(car.getRPM1(), 2); | ||
Serial.print(' '); | ||
Serial.print(car.getRPM2(), 2); | ||
Serial.print(' '); | ||
Serial.print(car.getRPM3(), 2); | ||
Serial.print(' '); | ||
Serial.print(car.getRPM4(), 2); | ||
|
||
// Serial.print(w1.getRPM(), 2); | ||
// Serial.print(' '); | ||
// Serial.print(w2.getRPM(), 2); | ||
// Serial.print(' '); | ||
// Serial.print(w3.getRPM(), 2); | ||
// Serial.print(' '); | ||
// Serial.print(w4.getRPM(), 2); | ||
|
||
Serial.print(';'); | ||
} | ||
|
||
void changeState() { | ||
state = !state; | ||
if(state) { | ||
vel = 100; | ||
} else { | ||
vel = -100; | ||
} | ||
} | ||
|
||
void loop() { | ||
// userInput(); | ||
|
||
// auto t = micros(); | ||
runMotors(); | ||
// Serial.println("Time: " + (String)(micros() - t)); | ||
|
||
sendData(); | ||
|
||
now = millis(); | ||
if (now - prev > timer) { | ||
prev = now; | ||
changeState(); | ||
} | ||
float* control_from_rpi = talker.rx(); | ||
car.setDesiredRpms(control_from_rpi); | ||
car.updateFeedbackRpms(); | ||
talker.tx(car.getFeedbackRpms()); | ||
car.reachDesiredRpms(); | ||
} |