Skip to content

Commit 240f4e3

Browse files
Adding comms on jetson
1 parent 33bd1e5 commit 240f4e3

File tree

6 files changed

+263
-141
lines changed

6 files changed

+263
-141
lines changed

beachRover-v1-arduino/Communication.cpp

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,22 @@ long readLong() {
2121
return NULL;
2222
}
2323

24+
float readFloat(){
25+
unsigned long startTime = millis();
26+
while((Serial.available()<sizeof(float) && (millis() - startTime)<timeout)){};
27+
if(Serial.available()>0){
28+
size_t bytesRead;
29+
byte floatBuffer[sizeof(float)];
30+
bytesRead = Serial.readBytes(floatBuffer,sizeof(float));
31+
if(bytesRead==sizeof(float)){
32+
float receivedFloat;
33+
memcpy(&receivedFloat,floatBuffer,sizeof(float));
34+
return receivedFloat;
35+
}
36+
}
37+
return NULL;
38+
}
39+
2440
int readInt() {
2541
unsigned long startTime = millis();
2642
while ((Serial.available() < sizeof(int)) && (millis() - startTime) < timeout) {};
@@ -66,7 +82,8 @@ bool readCommand(Command* command) {
6682
if (commandType == NULL) {
6783
return false;
6884
}
69-
long commandParam = readLong();
85+
float commandParam = readFloat();
86+
// Serial.println(commandParam);
7087
if (commandParam == NULL) {
7188
return false;
7289
}
@@ -82,3 +99,21 @@ bool readCommand(Command* command) {
8299
}
83100
return false;
84101
}
102+
103+
void sendInfo(Info * info){
104+
if(Serial.availableForWrite()){
105+
// Serial.println('^');
106+
// Serial.println(info->sensor);
107+
// Serial.println(info->x);
108+
// Serial.println(info->y);
109+
// Serial.println(info->z);
110+
111+
112+
Serial.write('^');
113+
Serial.write((byte *)&(info->sensor),2);
114+
Serial.write((byte *)&(info->x),4);
115+
Serial.write((byte *)&(info->y),4);
116+
Serial.write((byte *)&(info->z),4);
117+
Serial.write(10);
118+
}
119+
}

beachRover-v1-arduino/Communication.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,19 @@ enum COMMAND_TYPE{
1212

1313
struct Info{
1414
SENSOR_TYPE sensor;
15-
int x;
16-
int y;
17-
int z;
15+
float x;
16+
float y;
17+
float z;
1818
};
1919

2020
struct Command{
2121
COMMAND_TYPE command;
22-
long param;
22+
float param;
2323
};
2424

2525
int readInt();
2626
long readLong();
27+
float readFloat();
2728
double readDouble();
2829
bool readCommand(Command* commander);
30+
void sendInfo(Info* info);

beachRover-v1-arduino/beachRover-v1-arduino.ino

Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -83,37 +83,44 @@ void loop() {
8383

8484
int x, y, z;
8585
compass.read(&x, &y, &z);
86-
float heading = atan2(y, x);
8786

88-
float headingDegrees = heading * 180 / M_PI;
89-
float target = 0.;
90-
float error = target - headingDegrees;
91-
92-
if (abs(error) > 180 && error > 0) {
93-
error = 180 - error;
94-
} else if (abs(error) > 180 && error < 0) {
95-
error = (int)(180 - error) % 360;
96-
}
97-
98-
float correction = error;
99-
100-
if (error >= 0) {
101-
indicate_led(YELLOW_LED, false);
102-
} else {
103-
indicate_led(YELLOW_LED, true);
104-
}
105-
106-
Command receivedCommand;
107-
bool commandReceived = readCommand(&receivedCommand);
108-
if (commandReceived) {
109-
Serial.println(receivedCommand.command);
110-
Serial.println(receivedCommand.param);
111-
}
112-
113-
114-
motorDirection = kalman(motorDirection, (correction / 360) + 0.5, 0.98);
115-
motorDirection = (correction / 360) + 0.5;
116-
driveMotor(motorDirection, 0.35);
87+
Info info;
88+
info.sensor = MAGNETOMETER;
89+
info.x = (float)x;
90+
info.y = (float)y;
91+
info.z = (float)z;
92+
// Serial.println(info.x);
93+
sendInfo(&info);
94+
// float heading = atan2(y, x);
95+
//
96+
// float headingDegrees = heading * 180 / M_PI;
97+
// float target = 0.;
98+
// float error = target - headingDegrees;
99+
//
100+
// if (abs(error) > 180 && error > 0) {
101+
// error = 180 - error;
102+
// } else if (abs(error) > 180 && error < 0) {
103+
// error = (int)(180 - error) % 360;
104+
// }
105+
//
106+
// float correction = error;
107+
108+
// Command receivedCommand;
109+
// bool commandReceived = readCommand(&receivedCommand);
110+
// if (commandReceived) {
111+
//// Serial.println(receivedCommand.command);
112+
//// Serial.println(receivedCommand.param);
113+
// if (receivedCommand.param >= 0) {
114+
// indicate_led(YELLOW_LED, false);
115+
// } else {
116+
// indicate_led(YELLOW_LED, true);
117+
// }
118+
// }
119+
//
120+
121+
// motorDirection = kalman(motorDirection, (correction / 360) + 0.5, 0.98);
122+
// motorDirection = (correction / 360) + 0.5;
123+
// driveMotor(motorDirection, 0.35);
117124
delay(10);
118125
}
119126

0 commit comments

Comments
 (0)