APM:Libraries
Util.cpp
Go to the documentation of this file.
1 
2 #include <AP_HAL/AP_HAL.h>
3 #if CONFIG_HAL_BOARD == HAL_BOARD_VRBRAIN
4 #include <stdio.h>
5 #include <stdarg.h>
6 #include <unistd.h>
7 #include <stdlib.h>
8 #include <errno.h>
9 #include <apps/nsh.h>
10 #include <fcntl.h>
11 #include "UARTDriver.h"
12 #include <uORB/uORB.h>
13 #include <uORB/topics/safety.h>
14 #include <systemlib/board_serial.h>
15 #include <drivers/drv_gpio.h>
16 #include <AP_Math/AP_Math.h>
17 
18 extern const AP_HAL::HAL& hal;
19 
20 #include "Util.h"
21 using namespace VRBRAIN;
22 
23 extern bool _vrbrain_thread_should_exit;
24 
25 /*
26  constructor
27  */
29 {
30  _safety_handle = orb_subscribe(ORB_ID(safety));
31 }
32 
33 
34 /*
35  start an instance of nsh
36  */
38 {
39  VRBRAINUARTDriver *uart = (VRBRAINUARTDriver *)stream;
40  int fd;
41 
42  // trigger exit in the other threads. This stops use of the
43  // various driver handles, and especially the px4io handle,
44  // which otherwise would cause a crash if px4io is stopped in
45  // the shell
47 
48  // take control of stream fd
49  fd = uart->_get_fd();
50 
51  // mark it blocking (nsh expects a blocking fd)
52  unsigned v;
53  v = fcntl(fd, F_GETFL, 0);
54  fcntl(fd, F_SETFL, v & ~O_NONBLOCK);
55 
56  // setup the UART on stdin/stdout/stderr
57  close(0);
58  close(1);
59  close(2);
60  dup2(fd, 0);
61  dup2(fd, 1);
62  dup2(fd, 2);
63 
64  nsh_consolemain(0, nullptr);
65 
66  // this shouldn't happen
67  hal.console->printf("shell exited\n");
68  return true;
69 }
70 
71 /*
72  return state of safety switch
73  */
75 {
76 #if !HAL_HAVE_SAFETY_SWITCH
78 #endif
79 
80  if (_safety_handle == -1) {
81  _safety_handle = orb_subscribe(ORB_ID(safety));
82  }
83  if (_safety_handle == -1) {
85  }
86  struct safety_s safety;
87  if (orb_copy(ORB_ID(safety), _safety_handle, &safety) != OK) {
89  }
90  if (!safety.safety_switch_available) {
92  }
93  if (safety.safety_off) {
95  }
97 }
98 
99 void VRBRAINUtil::set_system_clock(uint64_t time_utc_usec)
100 {
101  timespec ts;
102  ts.tv_sec = time_utc_usec/1000000ULL;
103  ts.tv_nsec = (time_utc_usec % 1000000ULL) * 1000ULL;
104  clock_settime(CLOCK_REALTIME, &ts);
105 }
106 
107 /*
108  display VRBRAIN system identifer - board type and serial number
109  */
110 bool VRBRAINUtil::get_system_id(char buf[40])
111 {
112  uint8_t serialid[12];
113  memset(serialid, 0, sizeof(serialid));
114  get_board_serial(serialid);
115 #if defined(CONFIG_ARCH_BOARD_VRBRAIN_V45)
116  const char *board_type = "VRBRAINv45";
117 #elif defined(CONFIG_ARCH_BOARD_VRBRAIN_V51)
118  const char *board_type = "VRBRAINv51";
119 #elif defined(CONFIG_ARCH_BOARD_VRBRAIN_V52)
120  const char *board_type = "VRBRAINv52";
121 #elif defined(CONFIG_ARCH_BOARD_VRBRAIN_V52E)
122  const char *board_type = "VRBRAINv52E";
123 #elif defined(CONFIG_ARCH_BOARD_VRUBRAIN_V51)
124  const char *board_type = "VRUBRAINv51";
125 #elif defined(CONFIG_ARCH_BOARD_VRUBRAIN_V52)
126  const char *board_type = "VRUBRAINv52";
127 #elif defined(CONFIG_ARCH_BOARD_VRCORE_V10)
128  const char *board_type = "VRCOREv10";
129 #elif defined(CONFIG_ARCH_BOARD_VRBRAIN_V54)
130  const char *board_type = "VRBRAINv54";
131 #endif
132  // this format is chosen to match the human_readable_serial()
133  // function in auth.c
134  snprintf(buf, 40, "%s %02X%02X%02X%02X %02X%02X%02X%02X %02X%02X%02X%02X",
135  board_type,
136  (unsigned)serialid[0], (unsigned)serialid[1], (unsigned)serialid[2], (unsigned)serialid[3],
137  (unsigned)serialid[4], (unsigned)serialid[5], (unsigned)serialid[6], (unsigned)serialid[7],
138  (unsigned)serialid[8], (unsigned)serialid[9], (unsigned)serialid[10],(unsigned)serialid[11]);
139  return true;
140 }
141 
146 {
147  return mallinfo().fordblks;
148 }
149 
150 /*
151  AP_HAL wrapper around PX4 perf counters
152  */
154 {
155  ::perf_counter_type vrbrain_t;
156  switch (t) {
158  vrbrain_t = ::PC_COUNT;
159  break;
161  vrbrain_t = ::PC_ELAPSED;
162  break;
164  vrbrain_t = ::PC_INTERVAL;
165  break;
166  default:
167  return nullptr;
168  }
169  return (perf_counter_t)::perf_alloc(vrbrain_t, name);
170 }
171 
173 {
175 }
176 
178 {
180 }
181 
183 {
185 }
186 
187 void VRBRAINUtil::set_imu_temp(float current)
188 {
189  if (!_heater.target || *_heater.target == -1) {
190  return;
191  }
192 
193  // average over temperatures to remove noise
194  _heater.count++;
195  _heater.sum += current;
196 
197  // update once a second
198  uint32_t now = AP_HAL::millis();
199  if (now - _heater.last_update_ms < 1000) {
200  return;
201  }
202  _heater.last_update_ms = now;
203 
204  current = _heater.sum / _heater.count;
205  _heater.sum = 0;
206  _heater.count = 0;
207 
208  // experimentally tweaked for Pixhawk2
209  const float kI = 0.3f;
210  const float kP = 200.0f;
211  float target = (float)(*_heater.target);
212 
213  // limit to 65 degrees to prevent damage
214  target = constrain_float(target, 0, 65);
215 
216  float err = target - current;
217 
218  _heater.integrator += kI * err;
219  _heater.integrator = constrain_float(_heater.integrator, 0, 70);
220 
221  float output = constrain_float(kP * err + _heater.integrator, 0, 100);
222 
223  // hal.console->printf("integrator %.1f out=%.1f temp=%.2f err=%.2f\n", _heater.integrator, output, current, err);
224 
225  if (_heater.fd == -1) {
226  _heater.fd = open("/dev/px4io", O_RDWR);
227  }
228  if (_heater.fd != -1) {
229  ioctl(_heater.fd, GPIO_SET_HEATER_DUTY_CYCLE, (unsigned)output);
230  }
231 
232 }
233 
235 {
236  _heater.target = target;
237 }
238 
239 
240 extern "C" {
241  extern void *fat_dma_alloc(size_t);
242  extern void fat_dma_free(void *, size_t);
243 }
244 
245 /*
246  allocate DMA-capable memory if possible. Otherwise return normal
247  memory.
248 */
250 {
251 
252 
253 
254 
255 
256 
257 
258  return calloc(1, size);
259 
260 }
261 void VRBRAINUtil::free_type(void *ptr, size_t size, AP_HAL::Util::Memory_Type mem_type)
262 {
263 
264 
265 
266 
267 
268 
269 
270  return free(ptr);
271 
272 }
273 
274 #endif // CONFIG_HAL_BOARD == HAL_BOARD_VRBRAIN
int open(const char *pathname, int flags)
POSIX Open a file with integer mode flags.
Definition: posix.c:885
int _safety_handle
Definition: Util.h:69
AP_HAL::UARTDriver * console
Definition: HAL.h:110
struct VRBRAIN::VRBRAINUtil::@118 _heater
bool run_debug_shell(AP_HAL::BetterStream *stream)
Definition: Util.cpp:37
void get_board_serial(uint8_t *serialid)
Definition: syscalls.c:217
void perf_end(perf_counter_t) override
Definition: Util.cpp:177
void perf_begin(perf_counter_t) override
Definition: Util.cpp:172
bool get_system_id(char buf[40])
Definition: Util.cpp:110
const char * name
Definition: BusTest.cpp:11
void set_imu_temp(float current) override
Definition: Util.cpp:187
void * calloc(size_t nmemb, size_t size)
Definition: malloc.c:76
enum safety_state safety_switch_state(void)
Definition: Util.cpp:74
perf_counter_t perf_alloc(perf_counter_type t, const char *name) override
Definition: Util.cpp:153
virtual void printf(const char *,...) FMT_PRINTF(2
Definition: BetterStream.cpp:5
void * perf_counter_t
Definition: Util.h:101
void * fat_dma_alloc(size_t)
perf_counter_type
Definition: Util.h:96
uint32_t millis()
Definition: system.cpp:157
int close(int fileno)
POSIX Close a file with fileno handel.
Definition: posix.c:675
bool _vrbrain_thread_should_exit
void set_imu_target_temp(int8_t *target) override
Definition: Util.cpp:234
float v
Definition: Printf.cpp:15
safety_state
Definition: Util.h:35
const AP_HAL::HAL & hal
Definition: AC_PID_test.cpp:14
void set_system_clock(uint64_t time_utc_usec)
Definition: Util.cpp:99
void free(void *ptr)
Definition: malloc.c:81
float constrain_float(const float amt, const float low, const float high)
Definition: AP_Math.h:142
void * malloc_type(size_t size, AP_HAL::Util::Memory_Type mem_type) override
Definition: Util.cpp:249
int snprintf(char *str, size_t size, const char *format,...)
Definition: Util.cpp:44
uint32_t available_memory(void) override
Definition: Util.cpp:145
void fat_dma_free(void *, size_t)
void perf_count(perf_counter_t) override
Definition: Util.cpp:182
void free_type(void *ptr, size_t size, AP_HAL::Util::Memory_Type mem_type) override
Definition: Util.cpp:261
int8_t * target
Definition: Util.h:73