Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*------------------------------------------------------------------
- * in4073.c -- test QR engines and sensors
- *
- * reads ae[0-3] uart rx queue
- * (q,w,e,r increment, a,s,d,f decrement)
- *
- * prints timestamp, ae[0-3], sensors to uart tx queue
- *
- * I. Protonotarios
- * Embedded Software Lab
- *
- * June 2016
- *------------------------------------------------------------------
- */
- #include "in4073.h"
- uint8_t qr_mode = 0;
- uint32_t flash_address_p = 0x000000; // Address can be between 0x000000 and 0x01FFFF
- /*------------------------------------------------------------------
- * process_key -- process command keys
- *------------------------------------------------------------------
- */
- void process_key(uint8_t c)
- {
- switch (c)
- {
- case 'q':
- ae_keyboard[0] += 10;
- break;
- case 'a':
- ae_keyboard[0] -= 10;
- if (ae_keyboard[0] < 0) ae_keyboard[0] = 0;
- break;
- case 'w':
- ae_keyboard[1] += 10;
- break;
- case 's':
- ae_keyboard[1] -= 10;
- if (ae_keyboard[1] < 0) ae_keyboard[1] = 0;
- break;
- case 'e':
- ae_keyboard[2] += 10;
- break;
- case 'd':
- ae_keyboard[2] -= 10;
- if (ae_keyboard[2] < 0) ae_keyboard[2] = 0;
- break;
- case 'r':
- ae_keyboard[3] += 10;
- break;
- case 'f':
- ae_keyboard[3] -= 10;
- if (ae_keyboard[3] < 0) ae_keyboard[3] = 0;
- break;
- case 27:
- demo_done = true;
- break;
- default:
- nrf_gpio_pin_toggle(RED);
- }
- }
- /* Alex: processing joystick angles */
- void process_joystick() {
- if (roll > 0) {
- ae_joystick[1] = roll * 10;
- } else {
- ae_joystick[3] = roll * 10;
- }
- if (pitch > 0) {
- ae_joystick[0] = pitch * 10;
- } else {
- ae_joystick[2] = pitch * 10;
- }
- if (yaw > 0) {
- ae_joystick[1] = yaw * 10;
- ae_joystick[3] = yaw * 10;
- } else {
- ae_joystick[0] = yaw * 10;
- ae_joystick[2] = yaw * 10;
- }
- }
- /* Alex: get a single bit from a byte
- * Return 0 or 1
- */
- uint8_t get_bit(uint8_t byte, uint8_t pos) {
- // Hurray for unsafe operations!
- return (byte & (1 << pos)) > 0 ? 1 : 0;
- }
- /* Alex: read the message buffer based on the receiving packet format
- * Package format: preamble, modes, keyboard, throttle, roll, pitch, yaw, CRC
- */
- void parse_buffer() {
- // Ignore preamble
- uint8_t mode = get_bit(msg_buf[1], 0);
- mode += get_bit(msg_buf[1], 1) * 2;
- uint8_t raw_height_mode = get_bit(msg_buf[1], 2);
- switch (mode) {
- case 0:
- if (raw_height_mode == 0)
- qr_mode = 1; // panic
- else
- qr_mode = 2; // calibration
- case 1: qr_mode = 3; // manual
- case 2: qr_mode = 4; // yaw control
- case 3: qr_mode = 5; // full control
- }
- // Skip 3 0's'
- if (get_bit(msg_buf[1], 3) == 1) {
- process_key('O');
- }
- if (get_bit(msg_buf[1], 4) == 1) {
- process_key('L');
- }
- if (get_bit(msg_buf[1], 5) == 1) {
- process_key('A');
- }
- if (get_bit(msg_buf[1], 6) == 1) {
- process_key('Z');
- }
- if (get_bit(msg_buf[1], 7) == 1) {
- process_key('Q');
- }
- if (get_bit(msg_buf[2], 0) == 1) {
- process_key('W');
- }
- if (get_bit(msg_buf[2], 1) == 1) {
- process_key('U');
- }
- if (get_bit(msg_buf[2], 2) == 1) {
- process_key('J');
- }
- if (get_bit(msg_buf[2], 3) == 1) {
- process_key('I');
- }
- if (get_bit(msg_buf[2], 4) == 1) {
- process_key('K');
- }
- throttle = (get_bit(msg_buf[2], 5) << 5) + (get_bit(msg_buf[2], 6) << 4) + (get_bit(msg_buf[2], 7) << 3) +
- (get_bit(msg_buf[3], 0) << 2) + (get_bit(msg_buf[3], 1) << 1) + get_bit(msg_buf[3], 2);
- roll = -1 * get_bit(msg_buf[3], 3) * (
- (get_bit(msg_buf[3], 4) << 5) + (get_bit(msg_buf[3], 5) << 4) + (get_bit(msg_buf[3], 6) << 3) + (get_bit(msg_buf[3], 7) << 2) +
- (get_bit(msg_buf[4], 0) << 1) + get_bit(msg_buf[4], 1)
- );
- pitch = -1 * get_bit(msg_buf[4], 2) * (
- (get_bit(msg_buf[4], 3) << 5) + (get_bit(msg_buf[4], 4) << 4) + (get_bit(msg_buf[4], 5) << 3) + (get_bit(msg_buf[4], 6) << 2) +
- (get_bit(msg_buf[5], 7) << 1) + get_bit(msg_buf[5], 0)
- );
- yaw = -1 * get_bit(msg_buf[5], 1) * (
- (get_bit(msg_buf[5], 2) << 5) + (get_bit(msg_buf[5], 3) << 4) + (get_bit(msg_buf[5], 4) << 3) + (get_bit(msg_buf[5], 5) << 2) +
- (get_bit(msg_buf[5], 6) << 1) + get_bit(msg_buf[5], 7)
- );
- }
- /* Alex & Petros: read out queue and call CRC */
- void buffer(uint8_t byte)
- {
- // if (stream_is_tracked) {
- // if (bytes_read < RECEIVE_MESSAGE_LENGTH - 1) {
- msg_buf[bytes_read] = byte;
- // } else {
- // received_crc_value = byte;
- // }
- // if (bytes_read == RECEIVE_MESSAGE_LENGTH - 1) {
- // crc_value = do_crc(msg_buf); // calculates crc once the buffer is ready
- // }
- // } else {
- // if (byte == 0xFF) {
- // stream_is_tracked = true;
- // bytes_read = 0;
- // }
- // }
- }
- void CalulateTable_CRC8()
- {
- const uint8_t generator = 0x1D;
- /* iterate over all byte values 0 - 255 */
- for (uint16_t divident = 0; divident < 256; divident++) {
- uint8_t currByte = (uint8_t)divident;
- /* calculate the CRC-8 value for current byte */
- for (uint8_t bit = 0; bit < 8; bit++) {
- if ((currByte & 0x80) != 0) {
- currByte <<= 1;
- currByte ^= generator;
- }
- else {
- currByte <<= 1;
- }
- }
- /* store CRC value in lookup table */
- crctable[divident] = currByte;
- }
- printf("Calculated CRC table");
- }
- uint8_t Compute_CRC8(uint8_t* bytes)
- {
- uint8_t crc = 0;
- for (uint8_t i = 0; i < RECEIVE_MESSAGE_LENGTH - 1; i++) {
- /* XOR-in next input byte */
- uint8_t data = (uint8_t)(bytes[i] ^ crc);
- /* get current CRC value = remainder */
- crc = (uint8_t)(crctable[data]);
- }
- return crc;
- }
- /* Petros: CRC-8, crc value generation */
- uint8_t do_crc(uint8_t* package)
- {
- uint8_t crc = Compute_CRC8(package);
- return crc;
- }
- /* Petros: check if sender's crc matches receiver's */
- // TODO: check the sizeof(crc of sent msg); possible missmatch of quad/pc_crc
- bool match_crc(uint8_t quad_crc, uint8_t pc_crc) // change if unint8_t crc
- {
- if (quad_crc == pc_crc) {
- return true;
- } else {
- printf("Bad CRC");
- return false;
- }
- }
- /* Alex: convert int16_t to uint8_t for logging purposes */
- void convert_uint32_to_uint8(uint32_t x, uint8_t* buf)
- {
- buf[0] = (x & 0x000000ff);
- buf[1] = (x & 0x0000ff00) >> 8;
- buf[2] = (x & 0x00ff0000) >> 16;
- buf[3] = (x & 0xff000000) >> 24;
- }
- /* Alex: convert int16_t to uint8_t for logging purposes */
- void convert_int16_to_uint8(int16_t x, uint8_t* buf)
- {
- buf[0]= x & 0xff; // caps at 256 (2^8)
- buf[1]= (x >> 8); // multiplier of 256
- }
- /* Alex: write bytes to flash */
- void write_log(uint8_t* bytes, uint32_t length)
- {
- // printf("Logging %lu bytes at address %lu \r\n", length, flash_address_p);
- bool success = flash_write_bytes(flash_address_p, bytes, length);
- flash_address_p += length;
- if (!success) {
- printf("Bad writing of %lu bytes at address %lu", length, flash_address_p);
- }
- }
- /* Alex: read bytes from flash, output as the logged value with printf */
- uint32_t read_log(uint32_t address, uint32_t length)
- {
- uint8_t buf[length];
- bool success = flash_read_bytes(address, buf, length);
- if (success == false) {
- printf("BAD READING!");
- return 0;
- } else {
- uint32_t val = 0;
- for (uint8_t i = 0; i < length; i++) {
- val += buf[i] << (8 * i);
- }
- // printf("%d", val);
- return val;
- }
- }
- /*------------------------------------------------------------------
- * main -- do the test
- *------------------------------------------------------------------
- */
- int main(void)
- {
- uart_init();
- gpio_init();
- timers_init();
- adc_init();
- twi_init();
- imu_init(true, 100);
- baro_init();
- spi_flash_init();
- CalulateTable_CRC8(); // It initializes the CRC-8 table
- // ble_init();
- // uint8_t dummy_msg[RECEIVE_MESSAGE_LENGTH] = {0xFF, 0x01, 0x02, 0x03, 0x04, 0x05, 123};
- uint32_t counter = 0;
- // uint8_t buf_int16[2];
- // uint8_t buf_int32[4];
- int ro,pi,ya;
- int thr;
- int mode1, mode2, mode3;
- int keypress[10];
- crc_value = 0; // Petros: most probably it is unint8_t
- demo_done = false;
- stream_is_tracked = false;
- bytes_read = 0;
- // init keyboard/joystick values
- for (uint8_t i = 0; i < 4; i++) {
- ae_keyboard[i] = 0;
- ae_joystick[i] = 0;
- }
- // dummy_msg[6] = do_crc(dummy_msg);
- while (!demo_done) {
- // Petros: Write in buffer the message received
- /* INFO: if (true) then it loads bytes to buffer until
- the length is reached. */
- if (rx_queue.count) {
- while (rx_queue.count && bytes_read != RECEIVE_MESSAGE_LENGTH) {
- buffer((uint8_t)dequeue(&rx_queue));
- ++bytes_read;
- }
- scanf(msg_buf, " \r %1d %1d %1d %1d %1d %1d %1d %1d %1d %1d %1d %1d %1d %1d %2d %2d %2d %3d",
- &mode1, &mode2, &mode3, &keypress[0], &keypress[1], &keypress[2], &keypress[3], &keypress[4],
- &keypress[5], &keypress[6], &keypress[7], &keypress[8], &keypress[9], &thr, &ro, &pi, &ya, &received_crc_value);
- for (uint8_t i = 0; i < 10; i++) {
- process_key(keypress[i]);
- }
- throttle = thr * 10;
- roll = ro * 10;
- pitch = pi * 10;
- yaw = ya * 10;
- }
- // Petros: it enters the if-statement only if matching CRCs are found
- if (check_timer_flag())
- {
- if (counter++%20 == 0)
- nrf_gpio_pin_toggle(BLUE);
- // Total throttle = keyboard + joystick + throttle
- for (uint8_t i = 0; i < 4; i++) {
- ae[i] = ae_keyboard[i] + ae_joystick[i] + throttle * 10;
- }
- adc_request_sample();
- read_baro();
- uint32_t timestamp = get_time_us();
- printf("%10ld | ", timestamp);
- printf("%3d %3d %3d %3d | ",ae[0],ae[1],ae[2],ae[3]);
- printf("%6d %6d %6d | ", phi, theta, psi);
- printf("%6d %6d %6d | ", sp, sq, sr);
- printf("%4d | %4ld | %6ld \r\n", bat_volt, temperature, pressure);
- // convert_uint32_to_uint8(timestamp, buf_int32);
- // write_log(buf_int32, 4);
- // for (uint8_t i = 0; i < 4; i++) {
- // convert_int16_to_uint8(ae[i], buf_int16);
- // write_log(buf_int16, 2);
- // }
- // convert_int16_to_uint8(phi, buf_int16);
- // write_log(buf_int16, 2);
- // convert_int16_to_uint8(theta, buf_int16);
- // write_log(buf_int16, 2);
- // convert_int16_to_uint8(psi, buf_int16);
- // write_log(buf_int16, 2);
- // convert_int16_to_uint8(sp, buf_int16);
- // write_log(buf_int16, 2);
- // convert_int16_to_uint8(sq, buf_int16);
- // write_log(buf_int16, 2);
- // convert_int16_to_uint8(sr, buf_int16);
- // write_log(buf_int16, 2);
- // convert_int16_to_uint8(bat_volt, buf_int16);
- // write_log(buf_int16, 2);
- // convert_uint32_to_uint8(temperature, buf_int32);
- // write_log(buf_int32, 4);
- // convert_uint32_to_uint8(pressure, buf_int32);
- // write_log(buf_int32, 4);
- clear_timer_flag();
- } else
- continue;
- if (check_sensor_int_flag()) {
- get_dmp_data();
- run_filters_and_control();
- clear_sensor_int_flag();
- }
- }
- // Read all stored logs
- // 30 bytes per log
- uint32_t count = 0x000000;
- while (count <= flash_address_p) {
- printf("%10ld", read_log(count, 4)); printf(" | "); count += 4;
- for (uint8_t i = 0; i < 4; i++) {
- printf("%3ld", read_log(count, 2));; printf(" "); count += 2;
- }
- printf("| ");
- printf("%6ld", read_log(count, 2)); printf(" | "); count += 2;
- printf("%6ld", read_log(count, 2)); printf(" | "); count += 2;
- printf("%6ld", read_log(count, 2)); printf(" | "); count += 2;
- printf("%6ld", read_log(count, 2)); printf(" | "); count += 2;
- printf("%6ld", read_log(count, 2)); printf(" | "); count += 2;
- printf("%6ld", read_log(count, 2)); printf(" | "); count += 2;
- printf("%4ld", read_log(count, 2)); printf(" | "); count += 2;
- printf("%4ld", read_log(count, 4)); printf(" | "); count += 4;
- printf("%6ld", read_log(count, 4)); printf(" | "); count += 4;
- printf("\r\n");
- nrf_delay_ms(10);
- }
- printf("\n\t Goodbye \n\n");
- nrf_delay_ms(100);
- NVIC_SystemReset();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement