|
| 1 | +/* |
| 2 | +* Copyright (c) 2025 NITK.K ROS-Team |
| 3 | +* |
| 4 | +* SPDX-License-Identifier: Apache-2.0 |
| 5 | +*/ |
| 6 | + |
| 7 | +#include <Arduino.h> |
| 8 | + |
| 9 | +#include "SparkFun_Qwiic_OTOS_Arduino_Library.h" |
| 10 | +#include "Wire.h" |
| 11 | + |
| 12 | +#include <CRC8.h> // Arduino CRC library (Rob Tillaart) |
| 13 | + |
| 14 | +QwiicOTOS myOtos; |
| 15 | +const int ledPin = 13; |
| 16 | + |
| 17 | +static const char SERIAL_HEADER = 'X'; |
| 18 | + |
| 19 | +union float2byte { |
| 20 | + float f; |
| 21 | + uint8_t b[4]; |
| 22 | +}; |
| 23 | + |
| 24 | +static void get_time(int32_t &sec, int32_t &msec) |
| 25 | +{ |
| 26 | + unsigned long ms = millis(); |
| 27 | + sec = ms / 1000; |
| 28 | + msec = ms % 1000; |
| 29 | +} |
| 30 | + |
| 31 | +void setup() |
| 32 | +{ |
| 33 | + pinMode(ledPin, OUTPUT); |
| 34 | + |
| 35 | + Serial.begin(115200); |
| 36 | + Wire.begin(); |
| 37 | + |
| 38 | + // Attempt to begin the sensor |
| 39 | + while (myOtos.begin() == false) |
| 40 | + { |
| 41 | + delay(1000); |
| 42 | + } |
| 43 | + |
| 44 | + myOtos.calibrateImu(); |
| 45 | + myOtos.resetTracking(); |
| 46 | +} |
| 47 | + |
| 48 | +void loop() |
| 49 | +{ |
| 50 | + while (Serial.available()) |
| 51 | + { |
| 52 | + int ch = Serial.read(); |
| 53 | + if (ch == 'r' || ch == 'R') |
| 54 | + { |
| 55 | + myOtos.resetTracking(); |
| 56 | + digitalWrite(ledPin, LOW); |
| 57 | + delay(30); |
| 58 | + digitalWrite(ledPin, HIGH); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + sfe_otos_pose2d_t myPosition; |
| 63 | + myOtos.getPosition(myPosition); |
| 64 | + |
| 65 | + // Send binary packet: 'X' header, sec, msec, x, y, yaw, CRC8, then newline |
| 66 | + { |
| 67 | + int32_t sec = 0, msec = 0; |
| 68 | + get_time(sec, msec); |
| 69 | + |
| 70 | + float2byte fx, fy, fyaw; |
| 71 | + fx.f = myPosition.x; |
| 72 | + fy.f = myPosition.y; |
| 73 | + fyaw.f = myPosition.h; |
| 74 | + |
| 75 | + CRC8 crc(CRC8_DALLAS_MAXIM_POLYNOME, |
| 76 | + CRC8_DALLAS_MAXIM_INITIAL, |
| 77 | + CRC8_DALLAS_MAXIM_XOR_OUT, |
| 78 | + CRC8_DALLAS_MAXIM_REV_IN, |
| 79 | + CRC8_DALLAS_MAXIM_REV_OUT); |
| 80 | + crc.restart(); |
| 81 | + crc.add((uint8_t)SERIAL_HEADER); |
| 82 | + crc.add(reinterpret_cast<const uint8_t*>(&sec), 4); |
| 83 | + crc.add(reinterpret_cast<const uint8_t*>(&msec), 4); |
| 84 | + crc.add(fx.b, 4); |
| 85 | + crc.add(fy.b, 4); |
| 86 | + crc.add(fyaw.b, 4); |
| 87 | + uint8_t crc8 = crc.calc(); |
| 88 | + |
| 89 | + Serial.write(static_cast<uint8_t>(SERIAL_HEADER)); |
| 90 | + Serial.write(reinterpret_cast<uint8_t*>(&sec), 4); |
| 91 | + Serial.write(reinterpret_cast<uint8_t*>(&msec), 4); |
| 92 | + Serial.write(fx.b, 4); |
| 93 | + Serial.write(fy.b, 4); |
| 94 | + Serial.write(fyaw.b, 4); |
| 95 | + Serial.write(crc8); |
| 96 | + Serial.write('\n'); |
| 97 | + } |
| 98 | +} |
0 commit comments