Skip to content

Instantly share code, notes, and snippets.

@exerro
Created December 7, 2018 18:51
Show Gist options
  • Save exerro/27602c5e178135844d6fa0fdb4001778 to your computer and use it in GitHub Desktop.
Save exerro/27602c5e178135844d6fa0fdb4001778 to your computer and use it in GitHub Desktop.
Fix #2

Control interface

This document defines the structure and parameters of messages sent between the Arduino microcontroller on either robot, and the Raspberry Pi on the secondary robot which handles high level logic and control

Message structure

struct Message {
	char command;
	int16_t payload;
}

Pi to Arduino

Name Char Parameter Description
forward F distance Moves the robot forward by the distance in mm. Fires collision/status message on completion/failure.
turn T angle Fires status(0) message on completion
do D task Starts an activity on the arduino (e.g. moving a servo). task is a number representing that activity.
align A distance Moves backwards into the wall to align, then forwards by distance, similar to forward.
request R component Reads a value from a component (e.g. servo rotation). component is a number representing that component.

Arduino to Pi

Name Char Parameter Description
collision c distance Fired during forward/align action by the arduino, sending distance travelled.
status s error Returns an error code for some arbitrary action. If error is 0, no error was encountered (success).
return r data Response to a request message.
log l length Notifies the pi that the arduino is about to send a debug message. length is the length of the message.
#include "debug.h"
namespace robot {
namespace debug {
void log(const char* message, const char* function) {
size_t message_len = strlen(message);
size_t function_len = strlen(function);
Serial.write('l');
Serial.write(message_len + function_len);
Serial.write(message, message_len);
Serial.write(function, function_len);
}
void error(const char* error_message, const char* function) {
size_t message_len = strlen(error_message);
size_t function_len = strlen(function);
Serial.write('l');
Serial.write(message_len + function_len + 5 + 2); // + 5 + 2 bytes for the "[ in " and "]\n" strings
Serial.write(error_message, message_len);
Serial.print("[ in "); // TODO: gotta check that this is actually writing 5 bytes, not 6 (including \0)
Serial.write(function, function_len);
Serial.print("]\n"); // TODO: gotta check that this is actually writing 2 bytes, not 3 (including \0)
delay(100);
exit(1);
}
bool assert(bool value, const char* error_message, const char* function) {
if (!value) {
robot::debug::error(error_message, function);
}
return value;
}
uint8_t assert(uint8_t value, const char* error_message, const char* function) {
if (value == 0) {
robot::debug::error(error_message, function);
}
return value;
}
}
}
#pragma once
#include <arduino.h>
// use rerror(message) to error with a message
#define rerror(s) robot::debug::error(s, __FUNCTION__);
// use rassert(condition, message) to error with a message if `condition` is false
#define rassert(v, s) robot::debug::assert(v, s, __FUNCTION__);
// inline __attribute__((always_inline))
namespace robot {
namespace debug {
void log(const char* message, const char* function);
void error(const char* error_message, const char* function);
bool assert(bool value, const char* error_message, const char* function);
uint8_t assert(uint8_t value, const char* error_message, const char* function);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment