2011-03-19 07:20:11 -03:00
|
|
|
// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
|
2010-12-19 12:40:33 -04:00
|
|
|
|
|
|
|
// Code to Write and Read packets from DataFlash log memory
|
|
|
|
// Code to interact with the user to dump or erase logs
|
|
|
|
|
|
|
|
#define HEAD_BYTE1 0xA3 // Decimal 163
|
|
|
|
#define HEAD_BYTE2 0x95 // Decimal 149
|
|
|
|
#define END_BYTE 0xBA // Decimal 186
|
|
|
|
|
|
|
|
|
|
|
|
// These are function definitions so the Menu can be constructed before the functions
|
|
|
|
// are defined below. Order matters to the compiler.
|
|
|
|
static int8_t print_log_menu(uint8_t argc, const Menu::arg *argv);
|
|
|
|
static int8_t dump_log(uint8_t argc, const Menu::arg *argv);
|
|
|
|
static int8_t erase_logs(uint8_t argc, const Menu::arg *argv);
|
|
|
|
static int8_t select_logs(uint8_t argc, const Menu::arg *argv);
|
|
|
|
|
|
|
|
// This is the help function
|
|
|
|
// PSTR is an AVR macro to read strings from flash memory
|
|
|
|
// printf_P is a version of print_f that reads from flash memory
|
|
|
|
static int8_t help_log(uint8_t argc, const Menu::arg *argv)
|
|
|
|
{
|
|
|
|
Serial.printf_P(PSTR("\n"
|
|
|
|
"Commands:\n"
|
2011-03-26 03:35:52 -03:00
|
|
|
" dump <n>"
|
|
|
|
" erase (all logs)\n"
|
|
|
|
" enable <name> | all\n"
|
|
|
|
" disable <name> | all\n"
|
2010-12-19 12:40:33 -04:00
|
|
|
"\n"));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Creates a constant array of structs representing menu options
|
|
|
|
// and stores them in Flash memory, not RAM.
|
|
|
|
// User enters the string in the console to call the functions on the right.
|
|
|
|
// See class Menu in AP_Coommon for implementation details
|
|
|
|
const struct Menu::command log_menu_commands[] PROGMEM = {
|
|
|
|
{"dump", dump_log},
|
|
|
|
{"erase", erase_logs},
|
|
|
|
{"enable", select_logs},
|
|
|
|
{"disable", select_logs},
|
|
|
|
{"help", help_log}
|
|
|
|
};
|
|
|
|
|
|
|
|
// A Macro to create the Menu
|
|
|
|
MENU2(log_menu, "Log", log_menu_commands, print_log_menu);
|
|
|
|
|
|
|
|
static bool
|
|
|
|
print_log_menu(void)
|
|
|
|
{
|
|
|
|
int log_start;
|
|
|
|
int log_end;
|
2011-02-19 03:44:44 -04:00
|
|
|
byte last_log_num = get_num_logs();
|
2010-12-19 12:40:33 -04:00
|
|
|
|
|
|
|
Serial.printf_P(PSTR("logs enabled: "));
|
2011-02-17 03:09:13 -04:00
|
|
|
if (0 == g.log_bitmask) {
|
2010-12-19 12:40:33 -04:00
|
|
|
Serial.printf_P(PSTR("none"));
|
2011-02-24 01:56:59 -04:00
|
|
|
}else{
|
2010-12-19 12:40:33 -04:00
|
|
|
// Macro to make the following code a bit easier on the eye.
|
|
|
|
// Pass it the capitalised name of the log option, as defined
|
|
|
|
// in defines.h but without the LOG_ prefix. It will check for
|
|
|
|
// the bit being set and print the name of the log option to suit.
|
2011-03-09 02:37:09 -04:00
|
|
|
#define PLOG(_s) if (g.log_bitmask & MASK_LOG_ ## _s) Serial.printf_P(PSTR(" %S"), PSTR(#_s))
|
2010-12-19 12:40:33 -04:00
|
|
|
PLOG(ATTITUDE_FAST);
|
|
|
|
PLOG(ATTITUDE_MED);
|
|
|
|
PLOG(GPS);
|
|
|
|
PLOG(PM);
|
|
|
|
PLOG(CTUN);
|
|
|
|
PLOG(NTUN);
|
|
|
|
PLOG(MODE);
|
|
|
|
PLOG(RAW);
|
|
|
|
PLOG(CMD);
|
2011-03-09 02:37:09 -04:00
|
|
|
PLOG(CUR);
|
|
|
|
#undef PLOG
|
2010-12-19 12:40:33 -04:00
|
|
|
}
|
|
|
|
Serial.println();
|
|
|
|
|
|
|
|
if (last_log_num == 0) {
|
2011-03-26 03:35:52 -03:00
|
|
|
Serial.printf_P(PSTR("\nNo logs\n"));
|
2011-02-24 01:56:59 -04:00
|
|
|
}else{
|
2010-12-19 12:40:33 -04:00
|
|
|
|
2011-03-26 03:35:52 -03:00
|
|
|
Serial.printf_P(PSTR("\n%d logs\n"), last_log_num);
|
2010-12-19 12:40:33 -04:00
|
|
|
for(int i=1;i<last_log_num+1;i++) {
|
2011-02-19 03:44:44 -04:00
|
|
|
get_log_boundaries(last_log_num, i, log_start, log_end);
|
2011-03-26 03:35:52 -03:00
|
|
|
Serial.printf_P(PSTR("Log # %d, start %d, end %d\n"),
|
2010-12-19 12:40:33 -04:00
|
|
|
i, log_start, log_end);
|
|
|
|
}
|
|
|
|
Serial.println();
|
|
|
|
}
|
|
|
|
return(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int8_t
|
|
|
|
dump_log(uint8_t argc, const Menu::arg *argv)
|
|
|
|
{
|
|
|
|
byte dump_log;
|
|
|
|
int dump_log_start;
|
|
|
|
int dump_log_end;
|
|
|
|
byte last_log_num;
|
|
|
|
|
|
|
|
// check that the requested log number can be read
|
|
|
|
dump_log = argv[1].i;
|
2011-02-19 03:44:44 -04:00
|
|
|
last_log_num = get_num_logs();
|
2010-12-19 12:40:33 -04:00
|
|
|
if ((argc != 2) || (dump_log < 1) || (dump_log > last_log_num)) {
|
|
|
|
Serial.printf_P(PSTR("bad log number\n"));
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
|
2011-02-19 03:44:44 -04:00
|
|
|
get_log_boundaries(last_log_num, dump_log, dump_log_start, dump_log_end);
|
2011-03-26 03:35:52 -03:00
|
|
|
Serial.printf_P(PSTR("Dumping Log number %d, start %d, end %d\n"),
|
2011-03-09 02:37:09 -04:00
|
|
|
dump_log,
|
|
|
|
dump_log_start,
|
|
|
|
dump_log_end);
|
|
|
|
|
2010-12-19 12:40:33 -04:00
|
|
|
Log_Read(dump_log_start, dump_log_end);
|
2011-03-26 03:35:52 -03:00
|
|
|
Serial.printf_P(PSTR("Complete\n"));
|
2010-12-19 12:40:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int8_t
|
|
|
|
erase_logs(uint8_t argc, const Menu::arg *argv)
|
|
|
|
{
|
|
|
|
for(int i = 10 ; i > 0; i--) {
|
2011-03-26 03:35:52 -03:00
|
|
|
Serial.printf_P(PSTR("ATTENTION - Erasing log in %d seconds.\n"), i);
|
2010-12-19 12:40:33 -04:00
|
|
|
delay(1000);
|
|
|
|
}
|
|
|
|
Serial.printf_P(PSTR("\nErasing log...\n"));
|
2011-02-19 03:44:44 -04:00
|
|
|
for(int j = 1; j < 4096; j++)
|
2010-12-19 12:40:33 -04:00
|
|
|
DataFlash.PageErase(j);
|
2011-02-19 03:44:44 -04:00
|
|
|
DataFlash.StartWrite(1);
|
|
|
|
DataFlash.WriteByte(HEAD_BYTE1);
|
|
|
|
DataFlash.WriteByte(HEAD_BYTE2);
|
|
|
|
DataFlash.WriteByte(LOG_INDEX_MSG);
|
|
|
|
DataFlash.WriteByte(0);
|
|
|
|
DataFlash.WriteByte(END_BYTE);
|
2011-03-09 02:37:09 -04:00
|
|
|
DataFlash.FinishWrite();
|
2010-12-19 12:40:33 -04:00
|
|
|
Serial.printf_P(PSTR("\nLog erased.\n"));
|
|
|
|
}
|
|
|
|
|
|
|
|
static int8_t
|
|
|
|
select_logs(uint8_t argc, const Menu::arg *argv)
|
|
|
|
{
|
|
|
|
uint16_t bits;
|
|
|
|
|
|
|
|
if (argc != 2) {
|
|
|
|
Serial.printf_P(PSTR("missing log type\n"));
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
bits = 0;
|
|
|
|
|
|
|
|
// Macro to make the following code a bit easier on the eye.
|
|
|
|
// Pass it the capitalised name of the log option, as defined
|
|
|
|
// in defines.h but without the LOG_ prefix. It will check for
|
|
|
|
// that name as the argument to the command, and set the bit in
|
|
|
|
// bits accordingly.
|
|
|
|
//
|
|
|
|
if (!strcasecmp_P(argv[1].str, PSTR("all"))) {
|
|
|
|
bits = ~(bits = 0);
|
2011-03-09 02:37:09 -04:00
|
|
|
} else {
|
|
|
|
#define TARG(_s) if (!strcasecmp_P(argv[1].str, PSTR(#_s))) bits |= MASK_LOG_ ## _s
|
2010-12-19 12:40:33 -04:00
|
|
|
TARG(ATTITUDE_FAST);
|
|
|
|
TARG(ATTITUDE_MED);
|
|
|
|
TARG(GPS);
|
|
|
|
TARG(PM);
|
|
|
|
TARG(CTUN);
|
|
|
|
TARG(NTUN);
|
|
|
|
TARG(MODE);
|
|
|
|
TARG(RAW);
|
|
|
|
TARG(CMD);
|
2011-03-09 02:37:09 -04:00
|
|
|
TARG(CUR);
|
|
|
|
#undef TARG
|
2010-12-19 12:40:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!strcasecmp_P(argv[0].str, PSTR("enable"))) {
|
2011-02-19 03:44:44 -04:00
|
|
|
g.log_bitmask.set_and_save(g.log_bitmask | bits);
|
2011-02-24 01:56:59 -04:00
|
|
|
}else{
|
2011-02-19 03:44:44 -04:00
|
|
|
g.log_bitmask.set_and_save(g.log_bitmask & ~bits);
|
2010-12-19 12:40:33 -04:00
|
|
|
}
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int8_t
|
|
|
|
process_logs(uint8_t argc, const Menu::arg *argv)
|
|
|
|
{
|
|
|
|
log_menu.run();
|
|
|
|
}
|
|
|
|
|
2011-02-19 03:44:44 -04:00
|
|
|
|
|
|
|
byte get_num_logs(void)
|
|
|
|
{
|
|
|
|
int page = 1;
|
|
|
|
byte data;
|
|
|
|
byte log_step = 0;
|
|
|
|
|
|
|
|
DataFlash.StartRead(1);
|
|
|
|
while (page == 1) {
|
|
|
|
data = DataFlash.ReadByte();
|
|
|
|
switch(log_step) //This is a state machine to read the packets
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
if(data==HEAD_BYTE1) // Head byte 1
|
|
|
|
log_step++;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
if(data==HEAD_BYTE2) // Head byte 2
|
|
|
|
log_step++;
|
|
|
|
else
|
|
|
|
log_step = 0;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
if(data==LOG_INDEX_MSG){
|
|
|
|
byte num_logs = DataFlash.ReadByte();
|
|
|
|
return num_logs;
|
2011-02-24 01:56:59 -04:00
|
|
|
}else{
|
2011-02-19 03:44:44 -04:00
|
|
|
log_step=0; // Restart, we have a problem...
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
page = DataFlash.GetPage();
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void start_new_log(byte num_existing_logs)
|
|
|
|
{
|
|
|
|
int page;
|
|
|
|
int start_pages[50] = {0,0,0};
|
|
|
|
int end_pages[50] = {0,0,0};
|
|
|
|
byte data;
|
|
|
|
|
|
|
|
if(num_existing_logs > 0) {
|
|
|
|
for(int i=0;i<num_existing_logs;i++) {
|
|
|
|
get_log_boundaries(num_existing_logs, i+1,start_pages[i],end_pages[i]);
|
|
|
|
}
|
|
|
|
end_pages[num_existing_logs - 1] = find_last_log_page(start_pages[num_existing_logs - 1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(end_pages[num_existing_logs - 1] < 4095 && num_existing_logs < MAX_NUM_LOGS) {
|
|
|
|
if(num_existing_logs > 0)
|
|
|
|
start_pages[num_existing_logs] = end_pages[num_existing_logs - 1] + 1;
|
|
|
|
else
|
|
|
|
start_pages[0] = 2;
|
|
|
|
num_existing_logs++;
|
|
|
|
DataFlash.StartWrite(1);
|
|
|
|
DataFlash.WriteByte(HEAD_BYTE1);
|
|
|
|
DataFlash.WriteByte(HEAD_BYTE2);
|
|
|
|
DataFlash.WriteByte(LOG_INDEX_MSG);
|
|
|
|
DataFlash.WriteByte(num_existing_logs);
|
|
|
|
for(int i=0;i<MAX_NUM_LOGS;i++) {
|
|
|
|
DataFlash.WriteInt(start_pages[i]);
|
|
|
|
DataFlash.WriteInt(end_pages[i]);
|
|
|
|
}
|
|
|
|
DataFlash.WriteByte(END_BYTE);
|
|
|
|
DataFlash.FinishWrite();
|
|
|
|
DataFlash.StartWrite(start_pages[num_existing_logs-1]);
|
2011-02-24 01:56:59 -04:00
|
|
|
}else{
|
2011-03-26 03:35:52 -03:00
|
|
|
gcs.send_text_P(SEVERITY_LOW,PSTR("<start_new_log> Logs full"));
|
2011-02-19 03:44:44 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void get_log_boundaries(byte num_logs, byte log_num, int & start_page, int & end_page)
|
|
|
|
{
|
|
|
|
int page = 1;
|
|
|
|
byte data;
|
|
|
|
byte log_step = 0;
|
|
|
|
|
|
|
|
DataFlash.StartRead(1);
|
|
|
|
while (page = 1) {
|
|
|
|
data = DataFlash.ReadByte();
|
|
|
|
switch(log_step) //This is a state machine to read the packets
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
if(data==HEAD_BYTE1) // Head byte 1
|
|
|
|
log_step++;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
if(data==HEAD_BYTE2) // Head byte 2
|
|
|
|
log_step++;
|
|
|
|
else
|
|
|
|
log_step = 0;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
if(data==LOG_INDEX_MSG){
|
|
|
|
byte num_logs = DataFlash.ReadByte();
|
|
|
|
for(int i=0;i<log_num;i++) {
|
|
|
|
start_page = DataFlash.ReadInt();
|
|
|
|
end_page = DataFlash.ReadInt();
|
|
|
|
}
|
|
|
|
if(log_num==num_logs)
|
|
|
|
end_page = find_last_log_page(start_page);
|
|
|
|
|
|
|
|
return; // This is the normal exit point
|
2011-02-24 01:56:59 -04:00
|
|
|
}else{
|
2011-02-19 03:44:44 -04:00
|
|
|
log_step=0; // Restart, we have a problem...
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
page = DataFlash.GetPage();
|
|
|
|
}
|
|
|
|
// Error condition if we reach here with page = 2 TO DO - report condition
|
|
|
|
}
|
|
|
|
|
|
|
|
int find_last_log_page(int bottom_page)
|
|
|
|
{
|
|
|
|
int top_page = 4096;
|
|
|
|
int look_page;
|
|
|
|
long check;
|
|
|
|
|
|
|
|
while((top_page - bottom_page) > 1) {
|
|
|
|
look_page = (top_page + bottom_page) / 2;
|
|
|
|
DataFlash.StartRead(look_page);
|
|
|
|
check = DataFlash.ReadLong();
|
|
|
|
if(check == 0xFFFFFFFF)
|
|
|
|
top_page = look_page;
|
|
|
|
else
|
|
|
|
bottom_page = look_page;
|
|
|
|
}
|
|
|
|
return top_page;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-12-19 12:40:33 -04:00
|
|
|
// Write an attitude packet. Total length : 10 bytes
|
|
|
|
void Log_Write_Attitude(int log_roll, int log_pitch, uint16_t log_yaw)
|
|
|
|
{
|
|
|
|
DataFlash.WriteByte(HEAD_BYTE1);
|
|
|
|
DataFlash.WriteByte(HEAD_BYTE2);
|
|
|
|
DataFlash.WriteByte(LOG_ATTITUDE_MSG);
|
|
|
|
DataFlash.WriteInt(log_roll);
|
|
|
|
DataFlash.WriteInt(log_pitch);
|
|
|
|
DataFlash.WriteInt(log_yaw);
|
|
|
|
DataFlash.WriteByte(END_BYTE);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write a performance monitoring packet. Total length : 19 bytes
|
|
|
|
void Log_Write_Performance()
|
|
|
|
{
|
|
|
|
DataFlash.WriteByte(HEAD_BYTE1);
|
|
|
|
DataFlash.WriteByte(HEAD_BYTE2);
|
|
|
|
DataFlash.WriteByte(LOG_PERFORMANCE_MSG);
|
|
|
|
DataFlash.WriteLong(millis()- perf_mon_timer);
|
|
|
|
DataFlash.WriteInt(mainLoop_count);
|
|
|
|
DataFlash.WriteInt(G_Dt_max);
|
|
|
|
DataFlash.WriteByte(dcm.gyro_sat_count);
|
|
|
|
DataFlash.WriteByte(imu.adc_constraints);
|
|
|
|
DataFlash.WriteByte(dcm.renorm_sqrt_count);
|
|
|
|
DataFlash.WriteByte(dcm.renorm_blowup_count);
|
|
|
|
DataFlash.WriteByte(gps_fix_count);
|
|
|
|
DataFlash.WriteInt((int)(dcm.get_health() * 1000));
|
|
|
|
DataFlash.WriteByte(END_BYTE);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write a command processing packet. Total length : 19 bytes
|
|
|
|
//void Log_Write_Cmd(byte num, byte id, byte p1, long alt, long lat, long lng)
|
|
|
|
void Log_Write_Cmd(byte num, struct Location *wp)
|
|
|
|
{
|
|
|
|
DataFlash.WriteByte(HEAD_BYTE1);
|
|
|
|
DataFlash.WriteByte(HEAD_BYTE2);
|
|
|
|
DataFlash.WriteByte(LOG_CMD_MSG);
|
|
|
|
DataFlash.WriteByte(num);
|
|
|
|
DataFlash.WriteByte(wp->id);
|
|
|
|
DataFlash.WriteByte(wp->p1);
|
|
|
|
DataFlash.WriteLong(wp->alt);
|
|
|
|
DataFlash.WriteLong(wp->lat);
|
|
|
|
DataFlash.WriteLong(wp->lng);
|
|
|
|
DataFlash.WriteByte(END_BYTE);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Log_Write_Startup(byte type)
|
|
|
|
{
|
|
|
|
DataFlash.WriteByte(HEAD_BYTE1);
|
|
|
|
DataFlash.WriteByte(HEAD_BYTE2);
|
|
|
|
DataFlash.WriteByte(LOG_STARTUP_MSG);
|
|
|
|
DataFlash.WriteByte(type);
|
2011-02-17 03:09:13 -04:00
|
|
|
DataFlash.WriteByte(g.waypoint_total);
|
2010-12-19 12:40:33 -04:00
|
|
|
DataFlash.WriteByte(END_BYTE);
|
|
|
|
|
|
|
|
// create a location struct to hold the temp Waypoints for printing
|
|
|
|
struct Location cmd = get_wp_with_index(0);
|
|
|
|
Log_Write_Cmd(0, &cmd);
|
2011-02-19 03:44:44 -04:00
|
|
|
|
2011-03-09 02:37:09 -04:00
|
|
|
for (int i = 1; i <= g.waypoint_total; i++){
|
2010-12-19 12:40:33 -04:00
|
|
|
cmd = get_wp_with_index(i);
|
|
|
|
Log_Write_Cmd(i, &cmd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Write a control tuning packet. Total length : 22 bytes
|
2011-02-24 01:56:59 -04:00
|
|
|
#if HIL_MODE != HIL_MODE_ATTITUDE
|
2010-12-19 12:40:33 -04:00
|
|
|
void Log_Write_Control_Tuning()
|
2011-02-19 03:44:44 -04:00
|
|
|
{
|
|
|
|
Vector3f accel = imu.get_accel();
|
|
|
|
|
2010-12-19 12:40:33 -04:00
|
|
|
DataFlash.WriteByte(HEAD_BYTE1);
|
|
|
|
DataFlash.WriteByte(HEAD_BYTE2);
|
|
|
|
DataFlash.WriteByte(LOG_CONTROL_TUNING_MSG);
|
2011-02-17 03:09:13 -04:00
|
|
|
DataFlash.WriteInt((int)(g.rc_1.servo_out));
|
2010-12-19 12:40:33 -04:00
|
|
|
DataFlash.WriteInt((int)nav_roll);
|
2011-01-25 01:53:36 -04:00
|
|
|
DataFlash.WriteInt((int)dcm.roll_sensor);
|
2011-02-17 03:09:13 -04:00
|
|
|
DataFlash.WriteInt((int)(g.rc_2.servo_out));
|
2010-12-19 12:40:33 -04:00
|
|
|
DataFlash.WriteInt((int)nav_pitch);
|
2011-01-25 01:53:36 -04:00
|
|
|
DataFlash.WriteInt((int)dcm.pitch_sensor);
|
2011-02-17 03:09:13 -04:00
|
|
|
DataFlash.WriteInt((int)(g.rc_3.servo_out));
|
|
|
|
DataFlash.WriteInt((int)(g.rc_4.servo_out));
|
2011-02-24 01:56:59 -04:00
|
|
|
DataFlash.WriteInt((int)(accel.y * 10000));
|
2010-12-19 12:40:33 -04:00
|
|
|
DataFlash.WriteByte(END_BYTE);
|
|
|
|
}
|
2011-02-24 01:56:59 -04:00
|
|
|
#endif
|
2010-12-19 12:40:33 -04:00
|
|
|
|
|
|
|
// Write a navigation tuning packet. Total length : 18 bytes
|
|
|
|
void Log_Write_Nav_Tuning()
|
|
|
|
{
|
|
|
|
DataFlash.WriteByte(HEAD_BYTE1);
|
|
|
|
DataFlash.WriteByte(HEAD_BYTE2);
|
|
|
|
DataFlash.WriteByte(LOG_NAV_TUNING_MSG);
|
2011-01-25 01:53:36 -04:00
|
|
|
DataFlash.WriteInt((uint16_t)dcm.yaw_sensor);
|
2010-12-19 12:40:33 -04:00
|
|
|
DataFlash.WriteInt((int)wp_distance);
|
|
|
|
DataFlash.WriteInt((uint16_t)target_bearing);
|
|
|
|
DataFlash.WriteInt((uint16_t)nav_bearing);
|
|
|
|
DataFlash.WriteInt(altitude_error);
|
|
|
|
DataFlash.WriteInt((int)airspeed);
|
2011-02-19 03:44:44 -04:00
|
|
|
DataFlash.WriteInt((int)(nav_gain_scaler*1000));
|
2010-12-19 12:40:33 -04:00
|
|
|
DataFlash.WriteByte(END_BYTE);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write a mode packet. Total length : 5 bytes
|
|
|
|
void Log_Write_Mode(byte mode)
|
|
|
|
{
|
|
|
|
DataFlash.WriteByte(HEAD_BYTE1);
|
|
|
|
DataFlash.WriteByte(HEAD_BYTE2);
|
|
|
|
DataFlash.WriteByte(LOG_MODE_MSG);
|
|
|
|
DataFlash.WriteByte(mode);
|
|
|
|
DataFlash.WriteByte(END_BYTE);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write an GPS packet. Total length : 30 bytes
|
2011-02-19 03:44:44 -04:00
|
|
|
void Log_Write_GPS( long log_Time, long log_Lattitude, long log_Longitude, long log_mix_alt, long log_gps_alt,
|
2010-12-19 12:40:33 -04:00
|
|
|
long log_Ground_Speed, long log_Ground_Course, byte log_Fix, byte log_NumSats)
|
|
|
|
{
|
|
|
|
DataFlash.WriteByte(HEAD_BYTE1);
|
|
|
|
DataFlash.WriteByte(HEAD_BYTE2);
|
|
|
|
DataFlash.WriteByte(LOG_GPS_MSG);
|
|
|
|
DataFlash.WriteLong(log_Time);
|
|
|
|
DataFlash.WriteByte(log_Fix);
|
|
|
|
DataFlash.WriteByte(log_NumSats);
|
|
|
|
DataFlash.WriteLong(log_Lattitude);
|
|
|
|
DataFlash.WriteLong(log_Longitude);
|
|
|
|
DataFlash.WriteLong(log_mix_alt);
|
|
|
|
DataFlash.WriteLong(log_gps_alt);
|
|
|
|
DataFlash.WriteLong(log_Ground_Speed);
|
|
|
|
DataFlash.WriteLong(log_Ground_Course);
|
|
|
|
DataFlash.WriteByte(END_BYTE);
|
|
|
|
DataFlash.WriteByte(END_BYTE);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write an raw accel/gyro data packet. Total length : 28 bytes
|
2011-02-24 01:56:59 -04:00
|
|
|
#if HIL_MODE != HIL_MODE_ATTITUDE
|
2010-12-19 12:40:33 -04:00
|
|
|
void Log_Write_Raw()
|
|
|
|
{
|
2011-02-19 03:44:44 -04:00
|
|
|
Vector3f gyro = imu.get_gyro();
|
|
|
|
Vector3f accel = imu.get_accel();
|
2010-12-19 12:40:33 -04:00
|
|
|
gyro *= t7; // Scale up for storage as long integers
|
|
|
|
accel *= t7;
|
|
|
|
DataFlash.WriteByte(HEAD_BYTE1);
|
|
|
|
DataFlash.WriteByte(HEAD_BYTE2);
|
|
|
|
DataFlash.WriteByte(LOG_RAW_MSG);
|
2011-02-19 03:44:44 -04:00
|
|
|
|
2010-12-19 12:40:33 -04:00
|
|
|
DataFlash.WriteLong((long)gyro.x);
|
|
|
|
DataFlash.WriteLong((long)gyro.y);
|
|
|
|
DataFlash.WriteLong((long)gyro.z);
|
|
|
|
DataFlash.WriteLong((long)accel.x);
|
|
|
|
DataFlash.WriteLong((long)accel.y);
|
|
|
|
DataFlash.WriteLong((long)accel.z);
|
2011-02-24 01:56:59 -04:00
|
|
|
|
2010-12-19 12:40:33 -04:00
|
|
|
DataFlash.WriteByte(END_BYTE);
|
|
|
|
}
|
2011-02-24 01:56:59 -04:00
|
|
|
#endif
|
2010-12-19 12:40:33 -04:00
|
|
|
|
2011-01-16 23:44:12 -04:00
|
|
|
void Log_Write_Current()
|
|
|
|
{
|
|
|
|
DataFlash.WriteByte(HEAD_BYTE1);
|
|
|
|
DataFlash.WriteByte(HEAD_BYTE2);
|
|
|
|
DataFlash.WriteByte(LOG_CURRENT_MSG);
|
2011-02-17 03:09:13 -04:00
|
|
|
DataFlash.WriteInt(g.rc_3.control_in);
|
2011-01-18 02:11:48 -04:00
|
|
|
DataFlash.WriteInt((int)(current_voltage * 100.0));
|
|
|
|
DataFlash.WriteInt((int)(current_amps * 100.0));
|
2011-01-29 22:36:03 -04:00
|
|
|
DataFlash.WriteInt((int)current_total);
|
2011-01-16 23:44:12 -04:00
|
|
|
DataFlash.WriteByte(END_BYTE);
|
|
|
|
}
|
2011-01-29 22:36:03 -04:00
|
|
|
|
2011-01-16 23:44:12 -04:00
|
|
|
// Read a Current packet
|
|
|
|
void Log_Read_Current()
|
|
|
|
{
|
2011-03-09 02:37:09 -04:00
|
|
|
Serial.printf_P(PSTR("CURR: %d, %4.4f, %4.4f, %d\n"),
|
2011-01-29 22:36:03 -04:00
|
|
|
DataFlash.ReadInt(),
|
|
|
|
((float)DataFlash.ReadInt() / 100.f),
|
|
|
|
((float)DataFlash.ReadInt() / 100.f),
|
|
|
|
DataFlash.ReadInt());
|
2011-01-16 23:44:12 -04:00
|
|
|
}
|
|
|
|
|
2010-12-19 12:40:33 -04:00
|
|
|
// Read an control tuning packet
|
|
|
|
void Log_Read_Control_Tuning()
|
2011-02-19 03:44:44 -04:00
|
|
|
{
|
2010-12-19 12:40:33 -04:00
|
|
|
float logvar;
|
|
|
|
|
2011-03-09 02:37:09 -04:00
|
|
|
Serial.printf_P(PSTR("CTUN:"));
|
|
|
|
for (int y = 1; y < 10; y++) {
|
2010-12-19 12:40:33 -04:00
|
|
|
logvar = DataFlash.ReadInt();
|
2011-03-09 02:37:09 -04:00
|
|
|
if(y < 8) logvar = logvar/100.f;
|
|
|
|
if(y == 9) logvar = logvar/10000.f;
|
2010-12-19 12:40:33 -04:00
|
|
|
Serial.print(logvar);
|
|
|
|
Serial.print(comma);
|
|
|
|
}
|
|
|
|
Serial.println(" ");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read a nav tuning packet
|
|
|
|
void Log_Read_Nav_Tuning()
|
|
|
|
{
|
2011-03-09 02:37:09 -04:00
|
|
|
Serial.printf_P(PSTR("NTUN: %4.4f, %d, %4.4f, %4.4f, %4.4f, %4.4f, %4.4f,\n"),
|
|
|
|
(float)((uint16_t)DataFlash.ReadInt())/100.0,
|
|
|
|
DataFlash.ReadInt(),
|
|
|
|
(float)((uint16_t)DataFlash.ReadInt())/100.0,
|
|
|
|
(float)((uint16_t)DataFlash.ReadInt())/100.0,
|
|
|
|
(float)DataFlash.ReadInt()/100.0,
|
|
|
|
(float)DataFlash.ReadInt()/100.0,
|
|
|
|
(float)DataFlash.ReadInt()/1000.0);
|
2010-12-19 12:40:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Read a performance packet
|
|
|
|
void Log_Read_Performance()
|
2011-02-19 03:44:44 -04:00
|
|
|
{
|
2010-12-19 12:40:33 -04:00
|
|
|
long pm_time;
|
|
|
|
int logvar;
|
|
|
|
|
|
|
|
Serial.print("PM:");
|
|
|
|
pm_time = DataFlash.ReadLong();
|
|
|
|
Serial.print(pm_time);
|
|
|
|
Serial.print(comma);
|
2011-03-09 02:37:09 -04:00
|
|
|
for (int y = 1; y < 9; y++) {
|
|
|
|
if(y < 3 || y > 7){
|
|
|
|
logvar = DataFlash.ReadInt();
|
|
|
|
}else{
|
|
|
|
logvar = DataFlash.ReadByte();
|
|
|
|
}
|
2010-12-19 12:40:33 -04:00
|
|
|
Serial.print(logvar);
|
|
|
|
Serial.print(comma);
|
|
|
|
}
|
|
|
|
Serial.println(" ");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read a command processing packet
|
|
|
|
void Log_Read_Cmd()
|
2011-02-19 03:44:44 -04:00
|
|
|
{
|
2010-12-19 12:40:33 -04:00
|
|
|
byte logvarb;
|
|
|
|
long logvarl;
|
|
|
|
|
|
|
|
Serial.print("CMD:");
|
2011-03-09 02:37:09 -04:00
|
|
|
for(int i = 1; i < 4; i++) {
|
2010-12-19 12:40:33 -04:00
|
|
|
logvarb = DataFlash.ReadByte();
|
2011-03-09 02:37:09 -04:00
|
|
|
Serial.print(logvarb, DEC);
|
2010-12-19 12:40:33 -04:00
|
|
|
Serial.print(comma);
|
|
|
|
}
|
2011-03-09 02:37:09 -04:00
|
|
|
for(int i = 1; i < 4; i++) {
|
2010-12-19 12:40:33 -04:00
|
|
|
logvarl = DataFlash.ReadLong();
|
2011-03-09 02:37:09 -04:00
|
|
|
Serial.print(logvarl, DEC);
|
2010-12-19 12:40:33 -04:00
|
|
|
Serial.print(comma);
|
|
|
|
}
|
|
|
|
Serial.println(" ");
|
|
|
|
}
|
|
|
|
|
|
|
|
void Log_Read_Startup()
|
|
|
|
{
|
|
|
|
byte logbyte = DataFlash.ReadByte();
|
2011-03-09 02:37:09 -04:00
|
|
|
|
2011-02-19 03:44:44 -04:00
|
|
|
if (logbyte == TYPE_AIRSTART_MSG)
|
2011-03-09 02:37:09 -04:00
|
|
|
Serial.printf_P(PSTR("AIR START - "));
|
2010-12-19 12:40:33 -04:00
|
|
|
else if (logbyte == TYPE_GROUNDSTART_MSG)
|
2011-03-09 02:37:09 -04:00
|
|
|
Serial.printf_P(PSTR("GROUND START - "));
|
2010-12-19 12:40:33 -04:00
|
|
|
else
|
2011-03-09 02:37:09 -04:00
|
|
|
Serial.printf_P(PSTR("UNKNOWN STARTUP - "));
|
|
|
|
|
|
|
|
Serial.printf_P(PSTR(" %d commands in memory\n"),(int)DataFlash.ReadByte());
|
2010-12-19 12:40:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Read an attitude packet
|
|
|
|
void Log_Read_Attitude()
|
2011-02-19 03:44:44 -04:00
|
|
|
{
|
2011-03-09 02:37:09 -04:00
|
|
|
Serial.printf_P(PSTR("ATT: %d, %d, %d\n"),
|
|
|
|
DataFlash.ReadInt(),
|
|
|
|
DataFlash.ReadInt(),
|
|
|
|
(uint16_t)DataFlash.ReadInt());
|
2010-12-19 12:40:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Read a mode packet
|
|
|
|
void Log_Read_Mode()
|
2011-02-24 01:56:59 -04:00
|
|
|
{
|
2010-12-19 12:40:33 -04:00
|
|
|
Serial.print("MOD:");
|
2011-01-29 22:36:03 -04:00
|
|
|
Serial.println(flight_mode_strings[DataFlash.ReadByte()]);
|
2010-12-19 12:40:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Read a GPS packet
|
|
|
|
void Log_Read_GPS()
|
|
|
|
{
|
2011-03-09 02:37:09 -04:00
|
|
|
Serial.printf_P(PSTR("GPS: %ld, %d, %d, %4.7f, %4.7f, %4.4f, %4.4f, %4.4f, %4.4f\n"),
|
|
|
|
DataFlash.ReadLong(),
|
|
|
|
(int)DataFlash.ReadByte(),
|
|
|
|
(int)DataFlash.ReadByte(),
|
|
|
|
(float)DataFlash.ReadLong() / t7,
|
|
|
|
(float)DataFlash.ReadLong() / t7,
|
|
|
|
(float)DataFlash.ReadLong() / 100.0,
|
|
|
|
(float)DataFlash.ReadLong() / 100.0,
|
|
|
|
(float)DataFlash.ReadLong() / 100.0,
|
|
|
|
(float)DataFlash.ReadLong() / 100.0);
|
2011-02-19 03:44:44 -04:00
|
|
|
|
2010-12-19 12:40:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Read a raw accel/gyro packet
|
|
|
|
void Log_Read_Raw()
|
2011-01-29 22:36:03 -04:00
|
|
|
{
|
2010-12-19 12:40:33 -04:00
|
|
|
float logvar;
|
|
|
|
Serial.print("RAW:");
|
2011-03-09 02:37:09 -04:00
|
|
|
for (int y = 0; y < 6; y++) {
|
|
|
|
logvar = (float)DataFlash.ReadLong() / t7;
|
2010-12-19 12:40:33 -04:00
|
|
|
Serial.print(logvar);
|
|
|
|
Serial.print(comma);
|
|
|
|
}
|
|
|
|
Serial.println(" ");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read the DataFlash log memory : Packet Parser
|
|
|
|
void Log_Read(int start_page, int end_page)
|
|
|
|
{
|
|
|
|
byte data;
|
2011-03-09 02:37:09 -04:00
|
|
|
byte log_step;
|
|
|
|
int packet_count;
|
2010-12-19 12:40:33 -04:00
|
|
|
int page = start_page;
|
|
|
|
|
|
|
|
|
|
|
|
DataFlash.StartRead(start_page);
|
2011-03-09 02:37:09 -04:00
|
|
|
while (page < end_page && page != -1){
|
2010-12-19 12:40:33 -04:00
|
|
|
data = DataFlash.ReadByte();
|
2011-03-09 02:37:09 -04:00
|
|
|
switch(log_step) // This is a state machine to read the packets
|
2010-12-19 12:40:33 -04:00
|
|
|
{
|
2011-02-19 03:44:44 -04:00
|
|
|
case 0:
|
2011-03-09 02:37:09 -04:00
|
|
|
if(data == HEAD_BYTE1) // Head byte 1
|
2010-12-19 12:40:33 -04:00
|
|
|
log_step++;
|
2011-01-29 01:55:23 -04:00
|
|
|
break;
|
2010-12-19 12:40:33 -04:00
|
|
|
case 1:
|
2011-03-09 02:37:09 -04:00
|
|
|
if(data == HEAD_BYTE2) // Head byte 2
|
2010-12-19 12:40:33 -04:00
|
|
|
log_step++;
|
|
|
|
else
|
|
|
|
log_step = 0;
|
2011-01-29 01:55:23 -04:00
|
|
|
break;
|
2010-12-19 12:40:33 -04:00
|
|
|
case 2:
|
2011-03-09 02:37:09 -04:00
|
|
|
if(data == LOG_ATTITUDE_MSG){
|
2010-12-19 12:40:33 -04:00
|
|
|
Log_Read_Attitude();
|
|
|
|
log_step++;
|
2011-02-19 03:44:44 -04:00
|
|
|
|
2011-03-09 02:37:09 -04:00
|
|
|
}else if(data == LOG_MODE_MSG){
|
2010-12-19 12:40:33 -04:00
|
|
|
Log_Read_Mode();
|
|
|
|
log_step++;
|
|
|
|
|
2011-03-09 02:37:09 -04:00
|
|
|
}else if(data == LOG_CONTROL_TUNING_MSG){
|
2010-12-19 12:40:33 -04:00
|
|
|
Log_Read_Control_Tuning();
|
|
|
|
log_step++;
|
|
|
|
|
2011-03-09 02:37:09 -04:00
|
|
|
}else if(data == LOG_NAV_TUNING_MSG){
|
2010-12-19 12:40:33 -04:00
|
|
|
Log_Read_Nav_Tuning();
|
|
|
|
log_step++;
|
2011-02-19 03:44:44 -04:00
|
|
|
|
2011-03-09 02:37:09 -04:00
|
|
|
}else if(data == LOG_PERFORMANCE_MSG){
|
2010-12-19 12:40:33 -04:00
|
|
|
Log_Read_Performance();
|
|
|
|
log_step++;
|
2011-02-19 03:44:44 -04:00
|
|
|
|
2011-03-09 02:37:09 -04:00
|
|
|
}else if(data == LOG_RAW_MSG){
|
2010-12-19 12:40:33 -04:00
|
|
|
Log_Read_Raw();
|
|
|
|
log_step++;
|
2011-02-19 03:44:44 -04:00
|
|
|
|
2011-03-09 02:37:09 -04:00
|
|
|
}else if(data == LOG_CMD_MSG){
|
2010-12-19 12:40:33 -04:00
|
|
|
Log_Read_Cmd();
|
|
|
|
log_step++;
|
2011-02-24 01:56:59 -04:00
|
|
|
|
2011-03-09 02:37:09 -04:00
|
|
|
}else if(data == LOG_CURRENT_MSG){
|
2011-01-29 01:55:23 -04:00
|
|
|
Log_Read_Current();
|
|
|
|
log_step++;
|
2011-02-24 01:56:59 -04:00
|
|
|
|
2011-03-09 02:37:09 -04:00
|
|
|
}else if(data == LOG_STARTUP_MSG){
|
2010-12-19 12:40:33 -04:00
|
|
|
Log_Read_Startup();
|
|
|
|
log_step++;
|
|
|
|
}else {
|
2011-01-29 01:55:23 -04:00
|
|
|
if(data == LOG_GPS_MSG){
|
2010-12-19 12:40:33 -04:00
|
|
|
Log_Read_GPS();
|
|
|
|
log_step++;
|
2011-02-24 01:56:59 -04:00
|
|
|
}else{
|
2011-03-09 02:37:09 -04:00
|
|
|
Serial.printf_P(PSTR("Error Reading Packet: %d\n"),packet_count);
|
2011-01-29 01:55:23 -04:00
|
|
|
log_step = 0; // Restart, we have a problem...
|
2010-12-19 12:40:33 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 3:
|
2011-03-09 02:37:09 -04:00
|
|
|
if(data == END_BYTE){
|
2010-12-19 12:40:33 -04:00
|
|
|
packet_count++;
|
|
|
|
}else{
|
2011-03-09 02:37:09 -04:00
|
|
|
Serial.printf_P(PSTR("Error Reading END_BYTE: %d\n"),data);
|
2010-12-19 12:40:33 -04:00
|
|
|
}
|
2011-03-09 02:37:09 -04:00
|
|
|
log_step = 0; // Restart sequence: new packet...
|
2010-12-19 12:40:33 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
page = DataFlash.GetPage();
|
|
|
|
}
|
2011-03-26 03:35:52 -03:00
|
|
|
Serial.printf_P(PSTR("# of packets read: %d\n"), packet_count);
|
2010-12-19 12:40:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|