APM:Libraries
DataFlash_AllTypes.cpp
Go to the documentation of this file.
1 /*
2  * Write out two logs, each containing samples of each attribute type
3  */
4 
5 #include <AP_HAL/AP_HAL.h>
6 #include <DataFlash/DataFlash.h>
8 #include <stdio.h>
9 
11 
12 // Format characters in the format string for binary log messages
13 struct PACKED log_TYP1 {
15  uint64_t time_us;
16  int16_t a[32];
17  int8_t b;
18  uint8_t B;
19  int16_t h;
20  uint16_t H;
21  int32_t i;
22  uint32_t I;
23  float f;
24  double d;
25  char n[4];
26  char N[16];
27  char Z[64];
28 };
29 static_assert(sizeof(log_TYP1) < 256, "log_TYP1 is oversize");
30 
31 struct PACKED log_TYP2 {
33  uint64_t time_us;
34  int16_t c; // int16_t * 100
35  uint16_t C; // uint16_t * 100
36  int32_t e; // int32_t * 100
37  uint32_t E; // uint32_t * 100
38  int32_t L; // latitude/longitude
39  uint8_t M; // flight mode
40  int64_t q;
41  uint64_t Q;
42 };
43 
44 
48 };
49 
50 static const struct LogStructure log_structure[] = {
51  { LOG_TYP1_MSG,
52  sizeof(log_TYP1),
53  "TYP1",
54  "QbBhHiIfdnNZ",
55  "TimeUS,b,B,h,H,i,I,f,d,n,N,Z",
56  "s-----------",
57  "F-----------"
58  },
59  { LOG_TYP2_MSG,
60  sizeof(log_TYP2),
61  "TYP2",
62  "QcCeELMqQ",
63  "TimeUS,c,C,e,E,L,M,q,Q",
64  "s--------",
65  "F--------"
66  },
68  sizeof(log_Message),
69  "MSG",
70  "QZ",
71  "TimeUS,Message",
72  "s-",
73  "F-"}
74 };
75 
76 // these are identical to the entries in the above log-structure. Not
77 // shared to maintain the visual similarity between the above
78 // structure and that in LogStructure.h
79 #define TYP1_FMT "QabBhHiIfdnNZ"
80 #define TYP1_LBL "TimeUS,b,B,h,H,i,I,f,d,n,N,Z"
81 #define TYP2_FMT "QcCeELMqQ"
82 #define TYP2_LBL "TimeUS,c,C,e,E,L,M,q,Q"
83 
84 static uint16_t log_num;
85 
87 public:
88  void setup();
89  void loop();
90 
91 private:
92 
93  AP_Int32 log_bitmask;
94  DataFlash_Class dataflash{"DF AllTypes 0.2", log_bitmask};
95  void print_mode(AP_HAL::BetterStream *port, uint8_t mode);
96 
97  void Log_Write_TypeMessages();
98  void Log_Write_TypeMessages_Log_Write();
99 
100  void flush_dataflash(DataFlash_Class &dataflash);
101 };
102 
104 {
105 #if CONFIG_HAL_BOARD == HAL_BOARD_SITL || CONFIG_HAL_BOARD == HAL_BOARD_LINUX
106  _dataflash.flush();
107 #else
108  // flush is not available on e.g. px4 as it would be a somewhat
109  // dangerous operation, but if we wait long enough (at time of
110  // writing, 2 seconds, see DataFlash_File::_io_timer) the data
111  // will go out.
112  hal.scheduler->delay(3000);
113 #endif
114 }
115 
116 
118 {
119  log_num = dataflash.find_last_log();
120  hal.console->printf("Using log number %u\n", log_num);
121 
122  struct log_TYP1 typ1 = {
125  a : { -32768, 32767, 1, -1, 0, 17 }, // int16[32]
126  b : -17, // int8_t
127  B : 42, // uint8_t
128  h : -12372, // int16_t
129  H : 19812, // uint16_t
130  i : -98234729, // int32_t
131  I : 74627293, // uint32_t
132  f : 35.87654, // float
133  d : 67.7393274658293, // double
134  n : { 'A', 'B', 'C', 'D' }, // char[4]
135  // char[16]:
136  N : { 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P' },
137  // char[64]:
138  Z : { 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P',
139  'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P',
140  'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P',
141  'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P' }
142  };
143  dataflash.WriteBlock(&typ1, sizeof(typ1));
144 
145  struct log_TYP2 typ2 = {
148  c : -9823, // int16_t * 100
149  C : 5436, // uint16_t * 100
150  e : -9209238, // int32_t * 100
151  E : 19239872, // uint32_t * 100
152  L : -3576543, // uint32_t latitude/longitude;
153  M : 5, // uint8_t; // flight mode;
154  q : -98239832498328, // int64_t
155  Q : 3432345232233432 // uint64_t
156  };
157  dataflash.WriteBlock(&typ2, sizeof(typ2));
158 
159  flush_dataflash(dataflash);
160 
161  dataflash.StopLogging();
162 }
163 
165 {
166  log_num = dataflash.find_last_log();
167  hal.console->printf("Using log number for Log_Write %u\n", log_num);
168 
169  dataflash.Log_Write("TYP3", TYP1_LBL, TYP1_FMT,
171  -17, // int8_t
172  42, // uint8_t
173  -12372, // int16_t
174  19812, // uint16_t
175  -98234729, // int32_t
176  74627293, // uint32_t
177  35.87654f, // float
178  (double)67.7393274658293, // double
179  "ABCD", // char[4]
180  // char[16]:
181  "ABCDEFGHIJKLMNOP",
182  // char[64]:
183  "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
184  );
185 
186  dataflash.Log_Write("TYP4", TYP2_LBL, TYP2_FMT,
188  -9823, // int16_t * 100
189  5436, // uint16_t * 100
190  -9209238, // int32_t * 100
191  19239872, // uint32_t * 100
192  -3576543, // uint32_t latitude/longitude;
193  5, // uint8_t; // flight mode;
194  -98239832498328, // int64_t
195  3432345232233432 // uint64_t
196  );
197 
198  // emit a message which contains NaNs:
199  dataflash.Log_Write("NANS", "f,d,bf,bd", "fdfd", dataflash.quiet_nanf(), dataflash.quiet_nan(), NAN, NAN);
200 
201  flush_dataflash(dataflash);
202 
203  dataflash.StopLogging();
204 }
205 
207 {
208  hal.console->printf("Dataflash All Types 1.0\n");
209 
210  log_bitmask = (uint32_t)-1;
211  dataflash.Init(log_structure, ARRAY_SIZE(log_structure));
212  dataflash.set_vehicle_armed(true);
213  dataflash.Log_Write_Message("DataFlash Test");
214 
215  // Test
216  hal.scheduler->delay(20);
217 
218  Log_Write_TypeMessages();
219  Log_Write_TypeMessages_Log_Write();
220 
221  hal.console->printf("tests done\n");
222 }
223 
225 {
226  hal.console->printf("all done\n");
227  hal.scheduler->delay(1000);
228 }
229 
230 const struct AP_Param::GroupInfo GCS_MAVLINK::var_info[] = {
232 };
234 
235 
237 
238 AP_HAL_MAIN_CALLBACKS(&dataflashtest);
int16_t a[32]
static const struct LogStructure log_structure[]
static DataFlashTest_AllTypes dataflashtest
#define TYP2_LBL
uint64_t time_us
AP_HAL::UARTDriver * console
Definition: HAL.h:110
#define AP_HAL_MAIN_CALLBACKS(CALLBACKS)
Definition: AP_HAL_Main.h:39
virtual void delay(uint16_t ms)=0
virtual void printf(const char *,...) FMT_PRINTF(2
Definition: BetterStream.cpp:5
#define TYP1_LBL
#define f(i)
const AP_HAL::HAL & hal
-*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
void loop()
Definition: AC_PID_test.cpp:34
uint64_t micros64()
Definition: system.cpp:162
#define TYP2_FMT
#define ARRAY_SIZE(_arr)
Definition: AP_Common.h:80
const HAL & get_HAL()
AP_Int32 log_bitmask
void flush(void)
Definition: DataFlash.cpp:582
static uint16_t log_num
#define N
#define PACKED
Definition: AP_Common.h:28
void flush_dataflash(DataFlash_Class &dataflash)
uint64_t time_us
void setup()
Definition: AC_PID_test.cpp:26
#define LOG_PACKET_HEADER_INIT(id)
Definition: LogStructure.h:8
#define TYP1_FMT
#define AP_GROUPEND
Definition: AP_Param.h:121
GCS_Dummy _gcs
AP_HAL::Scheduler * scheduler
Definition: HAL.h:114