Skip to content

Commit

Permalink
Merge pull request #451 from CMU-Robotics-Club/buckley_unify_style
Browse files Browse the repository at this point in the history
Style Changes
  • Loading branch information
ramensandwich authored Dec 3, 2016
2 parents 031ef7f + fa102c6 commit 5a7ba58
Show file tree
Hide file tree
Showing 8 changed files with 216 additions and 205 deletions.
97 changes: 72 additions & 25 deletions offline/debugging/rbsm_console.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,28 +29,50 @@
import signal
signal.signal(signal.SIGINT, signal.SIG_DFL)

screenCopy = None
screen_copy = None

#Create a dictionary of message headers
#TODO create it from ../../real_time/rbsm_config.txt
mid_to_str = {
0: "ENC_TICKS_LAST",
1: "ENC_TICKS_RESET",
5: "ENC_RESET_REQUEST",
6: "ENC_RESET_CONFIRM",
17: "MEGA_TELEOP_BRAKE_COMMAND",
18: "MEGA_AUTON_BRAKE_COMMAND",
19: "MEGA_STEER_COMMAND",
20: "MEGA_STEER_ANGLE",
21: "MEGA_BRAKE_STATE",
22: "MEGA_AUTON_STATE" ,
23: "MEGA_BATTERY_LEVEL",
24: "MEGA_STEER_FEEDBACK",
30: "COMP_HASH",
253: "MEGA_TIMESTAMP",
254: "ERROR",
255: "DEVICE_ID"
}
# startOfValues is the comment before start of values for dict
# takeout is what needs to be taken out from the beginning of the values in dict
# if it is in the rbsm_config.txt file
def create_bit_to_str(start_of_values, takeout=""):
dict = {}
settings_file = None
try:
settings_file = open("../../real_time/rbsm_config.txt")
except:
print("Error! Unable to find real_time/rbsm_headers.txt\n")
sys.exit(1)
# when the correct headers found
headers_found = False
# when to stop looking for headers
end_headers = False
# go through each line to find the message headers
for line_num, line in enumerate(settings_file):
# to give right name takes out RBSM_MID_ if that is at beginning cause "RBSM_MID_" unneeded
if(line[0:len(takeout)] == takeout):
line = line[len(takeout):]
# key is in the part after , and value is before
if(headers_found):
definition = line.split(", ")
# if there are not enough values or too many, then line is assumed to be
# end of the parts with message headers
if(len(definition)!=2):
end_headers = True
break
# sets the key to equal the message header
dict[int(definition[1])] = definition[0]
elif(line[0:len(start_of_values)+3]=="// " + start_of_values):
# the string that is found is what symbolizes when message headers begin on
# next line
headers_found = True
# when the headers end do not care about anything else so breaks
if(end_headers):
break
return dict

mid_to_str = create_bit_to_str("RBSM Headers", "RBSM_MID_")
eid_to_str = create_bit_to_str("Error Message Bits", "RBSM_EID_")

def redraw(state):
screen = state["screen"]
Expand All @@ -70,10 +92,33 @@ def redraw(state):
message_cache[mid]["update_time"])
screen.addstr(row_id, 2, s)

# with open('someshit.txt', 'at') as f:
# f.write(s + "\n")

row_id = row_id + 1

#adding space and then error
screen.addstr(row_id, 2, "")
screen.addstr(row_id+1, 2, "--------ERRORS---------")
# since last steps went through 2 rows adding 2 to row_id
row_id = row_id + 2
# finds which value is error
error = 254
for key in mid_to_str.keys():
if(mid_to_str[key]=="ERROR"):
error = key
# check if key is even in message_cache
if(error in message_cache.keys()):
error_message = message_cache[error]["data"]
# max bit that has associated error
maxBit = max(eid_to_str.keys())
for bit in xrange(maxBit+1):
if(error_message%2==1 and bit in eid_to_str.keys()):
wipeScreenAndPost(screen, eid_to_str[bit], row_id)
row_id = row_id + 1
# can eliminate previous bit by diving by 2
error_message = error_message/2
# adding -- so that if there is nothing between error and this then no error
wipeScreenAndPost(screen, "------------------------", row_id)
# to make sure row below BottomLine clean:
wipeScreenAndPost(screen, "", row_id+1)


# update status line
Expand All @@ -86,6 +131,10 @@ def redraw(state):

screen.refresh()

def wipeScreenAndPost(screen, str, pos):
# long empty space to wipe line clean
screen.addstr(pos, 2, " ")
screen.addstr(pos, 2, str)

def rbsm_worker(state):
message_cache = state["message_cache"]
Expand Down Expand Up @@ -152,8 +201,6 @@ def command_worker(state):
# standard ascii characters
elif(new_char < 128) and len(state["command_line"]) < INPUTUPPERBOUND:
state["command_line"] += chr(new_char)
with open('someshit.txt', 'at') as f:
f.write(state["command_line"])

# redraw(state)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@


class RadioReceiver {

protected:
volatile uint8_t *receiver_pin_reg_;
uint8_t receiver_pin_num_;
Expand Down
47 changes: 15 additions & 32 deletions real_time/arduino_src/lib_avr/rbserialmessages/rbserialmessages.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,11 @@

// Public //////////////////////////////////////////////////////////////////////

RBSerialMessages::RBSerialMessages()
{
RBSerialMessages::RBSerialMessages() {
}


int RBSerialMessages::Init(UARTFILE *in_file, FILE *out_file)
{
int RBSerialMessages::Init(UARTFILE *in_file, FILE *out_file) {
// setup the hardware serial
in_file_ = in_file;
out_file_ = out_file;
Expand All @@ -36,14 +34,12 @@ int RBSerialMessages::Init(UARTFILE *in_file, FILE *out_file)
return 1;
}

int RBSerialMessages::Send(uint8_t id, uint32_t message)
{
int RBSerialMessages::Send(uint8_t id, uint32_t message) {
uint8_t buffer_pos;
buffer_pos = InitMessageBuffer();
buffer_pos = AppendMessageToBuffer(id, message, buffer_pos);

for(int i = 0; i < buffer_pos; i++)
{
for(int i = 0; i < buffer_pos; i++) {
fputc(buffer_out_[i], out_file_);
}

Expand All @@ -55,48 +51,38 @@ int RBSerialMessages::Send(uint8_t id, uint32_t message)
// return 0 if a complete message was found
// return -1 if not enough data was found
// return -2 if an invalid message was found
int RBSerialMessages::Read(rb_message_t* read_message)
{
while(in_file_->available() > 0)
{
int RBSerialMessages::Read(rb_message_t* read_message) {
while(in_file_->available() > 0) {
// check if there is new data
char new_serial_byte = fgetc(in_file_);

// first, we need to try to lock on to the stream
if(buffer_in_stream_lock_ == false)
{
if(buffer_in_stream_lock_ == false) {
printf("%s: searching for lock...\n", __PRETTY_FUNCTION__);
if((uint8_t)new_serial_byte == FOOTER)
{
if((uint8_t)new_serial_byte == FOOTER) {
printf("%s: got lock!\n", __PRETTY_FUNCTION__);
buffer_in_stream_lock_ = true;
}
}
// after we lock we need to read in to the buffer until full
else
{
} else {
// this->Send(RBSM_MID_ERROR, buffer_in_pos_);
buffer_in_[buffer_in_pos_] = (uint8_t)new_serial_byte;
buffer_in_pos_++;
// handle the end of a packet
if(buffer_in_pos_ == RBSM_PACKET_LENGTH)
{
if(buffer_in_pos_ == RBSM_PACKET_LENGTH) {
// reset buffer for next packet
buffer_in_pos_ = 0;
// parse this complete packet
if(buffer_in_[5] == FOOTER)
{
if(buffer_in_[5] == FOOTER) {
read_message->message_id = buffer_in_[0];
uint8_t *data_bytes = (uint8_t *) &(read_message->data);
data_bytes[0] = buffer_in_[4];
data_bytes[1] = buffer_in_[3];
data_bytes[2] = buffer_in_[2];
data_bytes[3] = buffer_in_[1];
return 0;
}
// skip packet as an error
else
{
} else {
buffer_in_stream_lock_ = false;
return RBSM_ERROR_INVALID_MESSAGE;
}
Expand All @@ -112,8 +98,7 @@ int RBSerialMessages::Read(rb_message_t* read_message)
// Private /////////////////////////////////////////////////////////////////////
uint8_t RBSerialMessages::AppendMessageToBuffer(uint8_t id,
uint32_t message,
uint8_t out_start_pos)
{
uint8_t out_start_pos) {
uint8_t buffer_pos = out_start_pos;
uint8_t message_ll = message & RBSM_ONE_BYTE_MASK;
uint8_t message_lh = (message >> RBSM_ONE_BYTE_SIZE) & RBSM_ONE_BYTE_MASK;
Expand All @@ -138,8 +123,7 @@ uint8_t RBSerialMessages::AppendMessageToBuffer(uint8_t id,
}


uint8_t RBSerialMessages::InitMessageBuffer()
{
uint8_t RBSerialMessages::InitMessageBuffer() {
uint8_t buffer_pos = 0;

// write null terminator just in case. note no increment
Expand All @@ -149,8 +133,7 @@ uint8_t RBSerialMessages::InitMessageBuffer()
}


int RBSerialMessages::InitReadBuffer()
{
int RBSerialMessages::InitReadBuffer() {
buffer_in_pos_ = 0;
buffer_in_stream_lock_ = false;

Expand Down
38 changes: 17 additions & 21 deletions real_time/arduino_src/lib_avr/rbserialmessages/rbserialmessages.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,41 +42,37 @@
#define RBSM_DID_MEGA 0


struct rb_message_t
{
struct rb_message_t {
char message_id;
uint32_t data;
};


class RBSerialMessages
{
class RBSerialMessages {
public:
RBSerialMessages();
int Init(UARTFILE *in_file, FILE *out_file);
int Send(uint8_t id, uint32_t message);
int Read(rb_message_t* read_message);

RBSerialMessages();
int Init(UARTFILE *in_file, FILE *out_file);
int Send(uint8_t id, uint32_t message);
int Read(rb_message_t* read_message);
private:

UARTFILE *in_file_;
FILE *out_file_;
char buffer_out_[RBSM_BUFFER_OUT_LENGTH];
char buffer_in_[RBSM_BUFFER_IN_LENGTH];
uint8_t buffer_in_pos_;
bool buffer_in_stream_lock_;
uint8_t AppendMessageToBuffer(uint8_t id,
uint32_t message,
uint8_t out_start_pos);
uint8_t InitMessageBuffer();
int InitReadBuffer();
UARTFILE *in_file_;
FILE *out_file_;
char buffer_out_[RBSM_BUFFER_OUT_LENGTH];
char buffer_in_[RBSM_BUFFER_IN_LENGTH];
uint8_t buffer_in_pos_;
bool buffer_in_stream_lock_;
uint8_t AppendMessageToBuffer(uint8_t id,
uint32_t message,
uint8_t out_start_pos);
uint8_t InitMessageBuffer();
int InitReadBuffer();
};


// old api. may still want some of these functions
//
// int protocol_init( void ); // TODO: I'm not sure exactly what needs to be initialized yet
// int protocol_run( void ); // TODO:
// unsigned long protocol_send( byte id, unsigned int message); // Currently working on
// unsigned long protocol_getMessage(unsigned long packet);
// unsigned long protocol_getID(unsigned long packet);
Expand Down
2 changes: 1 addition & 1 deletion real_time/arduino_src/radio_buggy_mega/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ clean_list :
$(REMOVE) $(patsubst %.c,%.d,$(patsubst %.cpp,%.d,$(SRC)))
$(REMOVE) .dep/*
$(REMOVE) fingerprint.h
$(REMOVE) rbsm_config.h
$(REMOVE) ../lib_avr/rbserialmessages/rbsm_config.h



Expand Down
Loading

0 comments on commit 5a7ba58

Please sign in to comment.