88 lines
1.4 KiB
Markdown
88 lines
1.4 KiB
Markdown
# USB Command Handler
|
|
|
|
Implementation of command handler for this project.
|
|
|
|
|
|
### ACK
|
|
|
|
Every command returns either an ACK or NACK.
|
|
|
|
### Commands
|
|
```c
|
|
typedef enum {
|
|
COMMAND_ACK,
|
|
COMMAND_NACK,
|
|
LED_TOGGLE,
|
|
LED_SET,
|
|
SERVO_SET,
|
|
SERVO_SET_ALL,
|
|
ADC_READ_RAW,
|
|
ADC_READ_RAW_ALL,
|
|
ADC_READ,
|
|
ADC_READ_ALL,
|
|
ADC_SET_READ,
|
|
DIGITAL_IN,
|
|
DIGITAL_LEG_OUT,
|
|
POWER_MONITOR_SET_READ,
|
|
POWER_MONITOR_READ,
|
|
SETTINGS_TEST_SAVE,
|
|
SETTINGS_TEST_READ,
|
|
|
|
MOVE_COMMAND_START,
|
|
// All the commands going to robot thread go here
|
|
TEST_COMMAND, // 16
|
|
|
|
MOVE_COMMAND_END
|
|
|
|
} commands_e;
|
|
```
|
|
|
|
### Command Structure
|
|
|
|
```c
|
|
#define COMMAND_PREFIX 0x69
|
|
#define COMMAND_ID 0x00
|
|
#define COMMAND_DATA_SIZE 160
|
|
#define COMMAND_HEADER_SIZE 13
|
|
|
|
struct command_message_t {
|
|
uint8_t prefix;
|
|
uint8_t length;
|
|
uint8_t id;
|
|
uint8_t command;
|
|
int64_t tick;
|
|
uint8_t crc;
|
|
uint8_t data[COMMAND_DATA_SIZE];
|
|
} __attribute__((packed));
|
|
```
|
|
|
|
### CRC Calculation Function
|
|
|
|
```c
|
|
uint8_t command_calculate_crc(struct command_message_t *msg) {
|
|
uint32_t sum = 0;
|
|
uint8_t crc = 0;
|
|
|
|
uint8_t *byte_ptr = (uint8_t *)msg;
|
|
int loop_length = (sizeof(struct command_message_t) - sizeof(msg->data) + msg->length);
|
|
|
|
for (int i = 0; i < loop_length; i++) {
|
|
if (i == (COMMAND_HEADER_SIZE - 1)) { continue; }
|
|
sum += byte_ptr[i];
|
|
}
|
|
|
|
crc = 0x100 - (sum & 0xff);
|
|
|
|
return crc;
|
|
}
|
|
```
|
|
|
|
### Python test script (AI generated)
|
|
|
|
**Command Usage:**
|
|
|
|
```bash
|
|
python3 scripts/send_cmd.py --help
|
|
```
|
|
|
|
This script sends/reads commands |