amiro-os / devices / DiWheelDrive / main.cpp @ b8b3a9c9
History | View | Annotate | Download (46.398 KB)
| 1 |
#include <cstdint> |
|---|---|
| 2 |
#define BL_CALLBACK_TABLE_ADDR (0x08000000 + 0x01C0) |
| 3 |
#define BL_MAGIC_NUMBER ((uint32_t)0xFF669900u) |
| 4 |
|
| 5 |
#define SHUTDOWN_NONE 0 |
| 6 |
#define SHUTDOWN_TRANSPORTATION 1 |
| 7 |
#define SHUTDOWN_DEEPSLEEP 2 |
| 8 |
#define SHUTDOWN_HIBERNATE 3 |
| 9 |
#define SHUTDOWN_RESTART 4 |
| 10 |
#define SHUTDOWN_HANDLE_REQUEST 5 |
| 11 |
|
| 12 |
#include <ch.hpp> |
| 13 |
|
| 14 |
#include <amiro/util/util.h> |
| 15 |
#include <global.hpp> |
| 16 |
#include <exti.hpp> |
| 17 |
|
| 18 |
#include <chprintf.h> |
| 19 |
#include <shell.h> |
| 20 |
|
| 21 |
#include "linefollow.hpp" |
| 22 |
#include "amiro_map.hpp" |
| 23 |
using namespace chibios_rt; |
| 24 |
|
| 25 |
Global global; |
| 26 |
|
| 27 |
struct blVersion_t {
|
| 28 |
const uint8_t identifier;
|
| 29 |
const uint8_t major;
|
| 30 |
const uint8_t minor;
|
| 31 |
const uint8_t patch;
|
| 32 |
} __attribute__((packed)); |
| 33 |
|
| 34 |
void systemShutdown() {
|
| 35 |
types::kinematic k; |
| 36 |
uint8_t i; |
| 37 |
|
| 38 |
// // make sure we assert SYS_PD_N to delay shutdown until we're done.
|
| 39 |
// boardRequestShutdown();
|
| 40 |
|
| 41 |
// stop the user thread
|
| 42 |
global.userThread.requestTerminate(); |
| 43 |
global.userThread.wait(); |
| 44 |
|
| 45 |
k.x = 0x00u;
|
| 46 |
k.w_z = 0x00u;
|
| 47 |
|
| 48 |
// stop wheels
|
| 49 |
global.robot.setTargetSpeed(k); |
| 50 |
global.robot.terminate(); |
| 51 |
|
| 52 |
for (i = 0x00; i < global.vcnl4020.size(); i++) { |
| 53 |
global.vcnl4020[i].requestTerminate(); |
| 54 |
global.vcnl4020[i].wait(); |
| 55 |
} |
| 56 |
|
| 57 |
global.ina219.requestTerminate(); |
| 58 |
global.ina219.wait(); |
| 59 |
global.hmc5883l.requestTerminate(); |
| 60 |
global.hmc5883l.wait(); |
| 61 |
global.l3g4200d.requestTerminate(); |
| 62 |
global.l3g4200d.wait(); |
| 63 |
|
| 64 |
global.motorcontrol.requestTerminate(); |
| 65 |
global.motorcontrol.wait(); |
| 66 |
global.odometry.requestTerminate(); |
| 67 |
global.odometry.wait(); |
| 68 |
|
| 69 |
// stop I²C
|
| 70 |
for (i = 0; i < global.V_I2C2.size(); ++i) |
| 71 |
global.V_I2C2[i].stop(); |
| 72 |
global.HW_I2C2.stop(); |
| 73 |
|
| 74 |
global.lis331dlh.requestTerminate(); |
| 75 |
global.lis331dlh.wait(); |
| 76 |
|
| 77 |
global.lis331dlh.configure(&global.accel_sleep_config); |
| 78 |
// global.lis331dlh.start(NORMALPRIO +4);
|
| 79 |
|
| 80 |
// boardWriteIoPower(0);
|
| 81 |
// boardStandby();
|
| 82 |
|
| 83 |
return;
|
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
//void (*shellcmd_t)(BaseSequentialStream *chp, int argc, char *argv[]);
|
| 88 |
|
| 89 |
void shellRequestShutdown(BaseSequentialStream *chp, int argc, char *argv[]) { |
| 90 |
|
| 91 |
chprintf(chp, "shellRequestShutdown\n");
|
| 92 |
|
| 93 |
/* if nor argument was given, print some help text */
|
| 94 |
if (argc == 0 || strcmp(argv[0], "help") == 0) { |
| 95 |
chprintf(chp, "\tUSAGE:\n");
|
| 96 |
chprintf(chp, "> shutdown <type>\n");
|
| 97 |
chprintf(chp, "\n");
|
| 98 |
chprintf(chp, "\ttype\n");
|
| 99 |
chprintf(chp, "The type of shutdown to perform.\n");
|
| 100 |
chprintf(chp, "Choose one of the following types:\n");
|
| 101 |
chprintf(chp, " transportation - Ultra low-power mode with all wakeups disabled.\n");
|
| 102 |
chprintf(chp, " The robot can not be charged.\n");
|
| 103 |
chprintf(chp, " deepsleep - Ultra low-power mode with several wakeups enabled.\n");
|
| 104 |
chprintf(chp, " The robot can only be charged via the power plug.\n");
|
| 105 |
chprintf(chp, " hibernate - Medium low-power mode, but with full charging capabilities.\n");
|
| 106 |
chprintf(chp, " restart - Performs a system restart.\n");
|
| 107 |
chprintf(chp, "Alternatively, you can use the shortcuts 't', 'd', 'h', and 'r' respectively.");
|
| 108 |
chprintf(chp, "\n");
|
| 109 |
return;
|
| 110 |
} |
| 111 |
|
| 112 |
if (strcmp(argv[0],"transportation") == 0 || strcmp(argv[0],"t") == 0) { |
| 113 |
shutdown_now = SHUTDOWN_TRANSPORTATION; |
| 114 |
chprintf(chp, "shutdown to transportation mode initialized\n");
|
| 115 |
} else if (strcmp(argv[0],"deepsleep") == 0 || strcmp(argv[0],"d") == 0) { |
| 116 |
shutdown_now = SHUTDOWN_DEEPSLEEP; |
| 117 |
chprintf(chp, "shutdown to deepsleep mode initialized\n");
|
| 118 |
} else if (strcmp(argv[0],"hibernate") == 0 || strcmp(argv[0],"h") == 0) { |
| 119 |
shutdown_now = SHUTDOWN_HIBERNATE; |
| 120 |
chprintf(chp, "shutdown to hibernate mode initialized\n");
|
| 121 |
} else if (strcmp(argv[0],"restart") == 0 || strcmp(argv[0],"r") == 0) { |
| 122 |
chprintf(chp, "restart initialized\n");
|
| 123 |
shutdown_now = SHUTDOWN_RESTART; |
| 124 |
} else {
|
| 125 |
chprintf(chp, "ERROR: unknown argument!\n");
|
| 126 |
shutdown_now = SHUTDOWN_NONE; |
| 127 |
} |
| 128 |
|
| 129 |
return;
|
| 130 |
} |
| 131 |
|
| 132 |
void shellRequestWakeup(BaseSequentialStream *chp, int argc, char *argv[]) { |
| 133 |
int i;
|
| 134 |
chprintf(chp, "shellRequestWakeup\n");
|
| 135 |
|
| 136 |
for (i = 0x00u; i < argc; i++) |
| 137 |
chprintf(chp, "%s\n", argv[i]);
|
| 138 |
|
| 139 |
boardWakeup(); |
| 140 |
} |
| 141 |
|
| 142 |
void shellRequestGetMemoryData(BaseSequentialStream *chp, int argc, char *argv[]) { |
| 143 |
enum Type {HEX, U8, U16, U32, S8, S16, S32};
|
| 144 |
|
| 145 |
chprintf(chp, "shellRequestReadData\n");
|
| 146 |
|
| 147 |
if (argc < 2 || strcmp(argv[0],"help") == 0) |
| 148 |
{
|
| 149 |
chprintf(chp, "Usage: %s\n","get_memory_data <type> <start> [<count>]"); |
| 150 |
chprintf(chp, "\n");
|
| 151 |
chprintf(chp, "\ttype\n");
|
| 152 |
chprintf(chp, "The data type as which to interpret the data.\n");
|
| 153 |
chprintf(chp, "Choose one of the following types:\n");
|
| 154 |
chprintf(chp, " hex - one byte as hexadecimal value\n");
|
| 155 |
chprintf(chp, " u8 - unsigned integer (8 bit)\n");
|
| 156 |
chprintf(chp, " u16 - unsigned integer (16 bit)\n");
|
| 157 |
chprintf(chp, " u32 - unsigned integer (32 bit)\n");
|
| 158 |
chprintf(chp, " s8 - signed integer (8 bit)\n");
|
| 159 |
chprintf(chp, " s16 - signed integer (16 bit)\n");
|
| 160 |
chprintf(chp, " s32 - signed integer (32 bit)\n");
|
| 161 |
chprintf(chp, "\tstart\n");
|
| 162 |
chprintf(chp, "The first byte to read from the memory.\n");
|
| 163 |
chprintf(chp, "\tcount [default = 1]\n");
|
| 164 |
chprintf(chp, "The number of elements to read.\n");
|
| 165 |
chprintf(chp, "\n");
|
| 166 |
chprintf(chp, "\tNOTE\n");
|
| 167 |
chprintf(chp, "Type conversions of this function might fail.\n");
|
| 168 |
chprintf(chp, "If so, use type=hex and convert by hand.\n");
|
| 169 |
chprintf(chp, "\n");
|
| 170 |
return;
|
| 171 |
} |
| 172 |
|
| 173 |
uint8_t type_size = 0;
|
| 174 |
Type type = HEX; |
| 175 |
if (strcmp(argv[0],"hex") == 0) { |
| 176 |
type_size = sizeof(unsigned char); |
| 177 |
type = HEX; |
| 178 |
} else if(strcmp(argv[0],"u8") == 0) { |
| 179 |
type_size = sizeof(uint8_t);
|
| 180 |
type = U8; |
| 181 |
} else if(strcmp(argv[0],"u16") == 0) { |
| 182 |
type_size = sizeof(uint16_t);
|
| 183 |
type = U16; |
| 184 |
} else if(strcmp(argv[0],"u32") == 0) { |
| 185 |
type_size = sizeof(uint32_t);
|
| 186 |
type = U32; |
| 187 |
} else if(strcmp(argv[0],"s8") == 0) { |
| 188 |
type_size = sizeof(int8_t);
|
| 189 |
type = S8; |
| 190 |
} else if(strcmp(argv[0],"s16") == 0) { |
| 191 |
type_size = sizeof(int16_t);
|
| 192 |
type = S16; |
| 193 |
} else if(strcmp(argv[0],"s32") == 0) { |
| 194 |
type_size = sizeof(int32_t);
|
| 195 |
type = S32; |
| 196 |
} else {
|
| 197 |
chprintf(chp, "First argument invalid. Use 'get_memory_data help' for help.\n");
|
| 198 |
return;
|
| 199 |
} |
| 200 |
|
| 201 |
unsigned int start_byte = atoi(argv[1]); |
| 202 |
|
| 203 |
unsigned int num_elements = 1; |
| 204 |
if (argc >= 3) |
| 205 |
num_elements = atoi(argv[2]);
|
| 206 |
|
| 207 |
const size_t eeprom_size = EEPROM::getsize(&global.at24c01);
|
| 208 |
uint8_t buffer[eeprom_size]; |
| 209 |
if (start_byte + (type_size * num_elements) > eeprom_size) {
|
| 210 |
num_elements = (eeprom_size - start_byte) / type_size; |
| 211 |
chprintf(chp, "Warning: request exceeds eeprom size -> limiting to %u values.\n", num_elements);
|
| 212 |
} |
| 213 |
|
| 214 |
chFileStreamSeek((BaseFileStream*)&global.at24c01, start_byte); |
| 215 |
|
| 216 |
// Work around, because stm32f1 cannot read a single byte
|
| 217 |
if (type_size*num_elements < 2) |
| 218 |
type_size = 2;
|
| 219 |
|
| 220 |
uint32_t bytes_read = chSequentialStreamRead((BaseFileStream*)&global.at24c01, buffer, type_size*num_elements); |
| 221 |
|
| 222 |
if (bytes_read != type_size*num_elements)
|
| 223 |
chprintf(chp, "Warning: %u of %u requested bytes were read.\n", bytes_read, type_size*num_elements);
|
| 224 |
|
| 225 |
for (unsigned int i = 0; i < num_elements; ++i) { |
| 226 |
switch (type) {
|
| 227 |
case HEX:
|
| 228 |
chprintf(chp, "%02X ", buffer[i]);
|
| 229 |
break;
|
| 230 |
case U8:
|
| 231 |
chprintf(chp, "%03u ", ((uint8_t*)buffer)[i]);
|
| 232 |
break;
|
| 233 |
case U16:
|
| 234 |
chprintf(chp, "%05u ", ((uint16_t*)buffer)[i]);
|
| 235 |
break;
|
| 236 |
case U32:
|
| 237 |
chprintf(chp, "%010u ", ((uint32_t*)buffer)[i]);
|
| 238 |
break;
|
| 239 |
case S8:
|
| 240 |
chprintf(chp, "%+03d ", ((int8_t*)buffer)[i]);
|
| 241 |
break;
|
| 242 |
case S16:
|
| 243 |
chprintf(chp, "%+05d ", ((int16_t*)buffer)[i]);
|
| 244 |
break;
|
| 245 |
case S32:
|
| 246 |
chprintf(chp, "%+010d ", ((int32_t*)buffer)[i]);
|
| 247 |
break;
|
| 248 |
default:
|
| 249 |
break;
|
| 250 |
} |
| 251 |
} |
| 252 |
chprintf(chp, "\n");
|
| 253 |
|
| 254 |
return;
|
| 255 |
} |
| 256 |
|
| 257 |
void shellRequestSetLights(BaseSequentialStream *chp, int argc, char *argv[]) { |
| 258 |
|
| 259 |
if (argc < 2 || argc == 3 ||strcmp(argv[0],"help") == 0) { |
| 260 |
chprintf(chp, "\tUSAGE:\n");
|
| 261 |
chprintf(chp, "> set_lights <led mask> <white/red> [<green> <blue>]\n");
|
| 262 |
chprintf(chp, "\n");
|
| 263 |
chprintf(chp, "\tled mask\n");
|
| 264 |
chprintf(chp, "The LEDs to be set.\n");
|
| 265 |
chprintf(chp, "You can set multiple LEDs at once by adding the following values:\n");
|
| 266 |
chprintf(chp, " 0x01 - rear left LED (SSW)\n");
|
| 267 |
chprintf(chp, " 0x02 - left rear LED (WSW)\n");
|
| 268 |
chprintf(chp, " 0x04 - left front LED (WNW)\n");
|
| 269 |
chprintf(chp, " 0x08 - front left LED (NNW)\n");
|
| 270 |
chprintf(chp, " 0x10 - front right LED (NNE)\n");
|
| 271 |
chprintf(chp, " 0x20 - right front LED (ENE)\n");
|
| 272 |
chprintf(chp, " 0x40 - right rear LED (ESE)\n");
|
| 273 |
chprintf(chp, " 0x80 - rear right LED (SSE)\n");
|
| 274 |
chprintf(chp, "\twhite/red\n");
|
| 275 |
chprintf(chp, "If no optional argument is given, this arguments sets the white value of the selected LEDs.\n");
|
| 276 |
chprintf(chp, "Otherwise this arguments sets the red color channel value.\n");
|
| 277 |
chprintf(chp, "\tgreen\n");
|
| 278 |
chprintf(chp, "Sets the green color channel value.\n");
|
| 279 |
chprintf(chp, "\tblue\n");
|
| 280 |
chprintf(chp, "Sets the blue color channel value.\n");
|
| 281 |
chprintf(chp, "\n");
|
| 282 |
chprintf(chp, "\tExample:\n");
|
| 283 |
chprintf(chp, "This line will set the two most left and two most right LEDs to bright cyan.\n");
|
| 284 |
chprintf(chp, "> set_lights 0x66 0 255 255\n");
|
| 285 |
chprintf(chp, "\n");
|
| 286 |
return;
|
| 287 |
} |
| 288 |
|
| 289 |
int arg_mask = strtol(argv[0], NULL, 0); |
| 290 |
int red = strtol(argv[1], NULL, 0); |
| 291 |
int green = red;
|
| 292 |
int blue = red;
|
| 293 |
if (argc >= 4) { |
| 294 |
green = strtol(argv[2], NULL, 0); |
| 295 |
blue = strtol(argv[3], NULL, 0); |
| 296 |
} |
| 297 |
Color color(red, green, blue); |
| 298 |
|
| 299 |
if (arg_mask & 0x01) { |
| 300 |
global.robot.setLightColor(constants::LightRing::LED_SSW, color); |