summaryrefslogtreecommitdiff
path: root/tools
diff options
context:
space:
mode:
Diffstat (limited to 'tools')
-rw-r--r--tools/CMakeLists.txt50
-rw-r--r--tools/alt_test.cpp50
-rw-r--r--tools/imu_calib.cpp222
-rw-r--r--tools/read_flash.c78
-rw-r--r--tools/servo_test.cpp27
5 files changed, 427 insertions, 0 deletions
diff --git a/tools/CMakeLists.txt b/tools/CMakeLists.txt
new file mode 100644
index 0000000..8806fb0
--- /dev/null
+++ b/tools/CMakeLists.txt
@@ -0,0 +1,50 @@
+if (COMPILE_TOOLS)
+ add_executable(read_flash
+ read_flash.c
+ ../src/spi_flash.c
+ )
+
+ add_executable(servo_test
+ servo_test.cpp
+ ../src/pwm.cpp
+ )
+
+ add_executable(alt_test
+ alt_test.cpp
+ )
+
+ add_executable(imu_calib
+ imu_calib.cpp
+ )
+
+ # pull in common dependencies
+ target_link_libraries(read_flash pico_stdlib hardware_spi)
+ target_include_directories(read_flash PUBLIC ../include)
+
+ target_link_libraries(servo_test pico_stdlib hardware_pwm hardware_i2c)
+ target_include_directories(servo_test PUBLIC ../include)
+
+ target_link_libraries(alt_test pico_stdlib hardware_i2c hardware_gpio)
+
+ target_link_libraries(imu_calib pico_stdlib hardware_i2c hardware_gpio)
+ target_include_directories(imu_calib PUBLIC ../include)
+
+ pico_enable_stdio_usb(read_flash 1)
+ pico_enable_stdio_uart(read_flash 0)
+
+ pico_enable_stdio_usb(servo_test 1)
+ pico_enable_stdio_uart(servo_test 0)
+
+ pico_enable_stdio_usb(alt_test 1)
+ pico_enable_stdio_uart(alt_test 0)
+
+ pico_enable_stdio_usb(imu_calib 1)
+ pico_enable_stdio_uart(imu_calib 0)
+
+ # create map/bin/hex file etc.
+ pico_add_extra_outputs(read_flash)
+ pico_add_extra_outputs(servo_test)
+ pico_add_extra_outputs(alt_test)
+ pico_add_extra_outputs(imu_calib)
+endif()
+
diff --git a/tools/alt_test.cpp b/tools/alt_test.cpp
new file mode 100644
index 0000000..a5c8849
--- /dev/null
+++ b/tools/alt_test.cpp
@@ -0,0 +1,50 @@
+#include <stdio.h>
+
+#include "hardware/gpio.h"
+#include "boards/pico_w.h"
+#include "hardware/i2c.h"
+#include "pico/stdio.h"
+#include "pico/time.h"
+
+#define ALT_ADDR 0x60
+#define MAX_SCL 400000
+#define DATA_RATE_HZ 15
+
+float altitude = 0.0f;
+float get_altitude();
+
+int main() {
+ stdio_init_all();
+
+ i2c_init(i2c_default, MAX_SCL);
+ gpio_set_function(PICO_DEFAULT_I2C_SDA_PIN, GPIO_FUNC_I2C);
+ gpio_set_function(PICO_DEFAULT_I2C_SCL_PIN, GPIO_FUNC_I2C);
+ gpio_pull_up(PICO_DEFAULT_I2C_SDA_PIN);
+ gpio_pull_up(PICO_DEFAULT_I2C_SCL_PIN);
+
+ uint8_t config[2] = {0};
+
+ // Select control register(0x26)
+ // Active mode, OSR = 16, altimeter mode(0xB8)
+ config[0] = 0x26;
+ config[1] = 0xB9;
+ i2c_write_blocking(i2c_default, ALT_ADDR, config, 2, true);
+ sleep_ms(1500);
+
+ while (1) {
+ sleep_ms(1000);
+ altitude = get_altitude();
+ printf("Altitude: %4.2f\n", altitude);
+ }
+}
+
+float get_altitude() {
+ uint8_t reg = 0x01;
+ uint8_t data[5];
+ i2c_write_blocking(i2c_default, ALT_ADDR, &reg, 1, true);
+ i2c_read_blocking(i2c_default, ALT_ADDR, data, 5, false);
+ // Exactly how MPL3115A2 datasheet says to retrieve altitude
+ float altitude = (float) ((int16_t) ((data[0] << 8) | data[1])) + (float) (data[2] >> 4) * 0.0625;
+ return altitude;
+}
+
diff --git a/tools/imu_calib.cpp b/tools/imu_calib.cpp
new file mode 100644
index 0000000..dc45f39
--- /dev/null
+++ b/tools/imu_calib.cpp
@@ -0,0 +1,222 @@
+#include <stdio.h>
+#include <stdint.h>
+#include <inttypes.h>
+#include <Eigen/Geometry>
+
+#include "pico/stdio.h"
+#include "hardware/gpio.h"
+#include "hardware/i2c.h"
+
+#define MAX_SCL 400000
+
+#define BNO055_OPR_MODE_ADDR 0x3D
+#define BNO055_OPR_MODE_CONFIG 0x00
+#define BNO055_SYS_TRIGGER_ADDR 0x3F
+#define BNO055_ADDRESS 0x28
+#define BNO055_CHIP_ID_ADDR 0x00
+#define BNO055_CHIP_ID 0xA0
+#define BNO055_OPR_MODE_NDOF 0x0C
+#define BNO055_CALIB_STAT_ADDR 0x35
+#define ACCEL_OFFSET_X_LSB_ADDR 0x55
+#define BNO055_LINEAR_ACCEL_DATA_X_LSB_ADDR 0x28
+#define BNO055_QUATERNION_DATA_W_LSB_ADDR 0x20
+#define UNIT_SELECTION 0x3B
+
+void get_calibration(uint8_t *sys, uint8_t *gyro, uint8_t *accel, uint8_t *mag);
+
+int main() {
+ stdio_init_all();
+
+ getchar();
+
+ i2c_init(i2c_default, MAX_SCL);
+ gpio_set_function(PICO_DEFAULT_I2C_SDA_PIN, GPIO_FUNC_I2C);
+ gpio_set_function(PICO_DEFAULT_I2C_SCL_PIN, GPIO_FUNC_I2C);
+ gpio_pull_up(PICO_DEFAULT_I2C_SDA_PIN);
+ gpio_pull_up(PICO_DEFAULT_I2C_SCL_PIN);
+
+ uint8_t buf[2] = {BNO055_CHIP_ID_ADDR};
+
+ uint8_t id = 0x00;
+ sleep_ms(1000);
+ i2c_write_blocking(i2c_default, BNO055_ADDRESS, buf, 1, false);
+ i2c_read_blocking(i2c_default, BNO055_ADDRESS, &id, 1, false);
+ while (id != BNO055_CHIP_ID) {
+ i2c_write_blocking(i2c_default, BNO055_ADDRESS, buf, 1, false);
+ i2c_read_blocking(i2c_default, BNO055_ADDRESS, &id, 1, false);
+ printf("Id not correct!, seeing: %" PRIu8 "\n", id);
+ sleep_ms(10);
+ }
+
+ buf[0] = BNO055_OPR_MODE_ADDR;
+ buf[1] = BNO055_OPR_MODE_CONFIG;
+ i2c_write_blocking(i2c_default, BNO055_ADDRESS, buf, 2, false);
+
+ buf[0] = BNO055_SYS_TRIGGER_ADDR;
+ buf[1] = 0x20; // RESET
+ i2c_write_blocking(i2c_default, BNO055_ADDRESS, buf, 2, false);
+ sleep_ms(30);
+
+ buf[0] = BNO055_CHIP_ID_ADDR;
+ id = 0x00;
+ while (id != BNO055_CHIP_ID) {
+ i2c_write_blocking(i2c_default, BNO055_ADDRESS, buf, 1, false);
+ i2c_read_blocking(i2c_default, BNO055_ADDRESS, &id, 1, false);
+ printf("Id not correct!, seeing: %" PRIu8 "\n", id);
+ sleep_ms(10);
+ }
+
+ buf[0] = BNO055_SYS_TRIGGER_ADDR;
+ buf[1] = 0x00; // RESET
+ i2c_write_blocking(i2c_default, BNO055_ADDRESS, buf, 2, false);
+ sleep_ms(30);
+
+ // Set units to m/s^2
+ buf[0] = UNIT_SELECTION;
+ buf[1] = 0x00; // Windows, Celsius, Degrees, DPS, m/s^2
+ i2c_write_blocking(i2c_default, BNO055_ADDRESS, buf, 2, false);
+ sleep_ms(50);
+
+ buf[0] = BNO055_OPR_MODE_ADDR;
+ buf[1] = BNO055_OPR_MODE_NDOF;
+ i2c_write_blocking(i2c_default, BNO055_ADDRESS, buf, 2, false);
+
+ uint8_t gyro = 0x00, accel = 0x00, mag = 0x00;
+
+ printf("Magnetometer: Perform the figure-eight calibration dance.\n");
+ while (mag != 3) {
+ // Calibration Dance Step One: Magnetometer
+ // Move sensor away from magnetic interference or shields
+ // Perform the figure-eight until calibrated
+ get_calibration(NULL, NULL, NULL, &mag);
+ printf("Mag Calib Status: %3.0f\n", (100 / 3 * mag));
+ sleep_ms(1000);
+ }
+ printf("... CALIBRATED\n");
+ sleep_ms(1000);
+
+ printf("Accelerometer: Perform the six-step calibration dance.\n");
+ while (accel != 3) {
+ // Calibration Dance Step Two: Accelerometer
+ // Place sensor board into six stable positions for a few seconds each:
+ // 1) x-axis right, y-axis up, z-axis away
+ // 2) x-axis up, y-axis left, z-axis away
+ // 3) x-axis left, y-axis down, z-axis away
+ // 4) x-axis down, y-axis right, z-axis away
+ // 5) x-axis left, y-axis right, z-axis up
+ // 6) x-axis right, y-axis left, z-axis down
+ // Repeat the steps until calibrated
+ get_calibration(NULL, NULL, &accel, NULL);
+ printf("Accel Calib Status: %3.0f\n", (100 / 3 * accel));
+ sleep_ms(1000);
+ }
+ printf("... CALIBRATED\n");
+ sleep_ms(1000);
+
+ printf("Gyroscope: Perform the hold-in-place calibration dance.\n");
+ while (gyro != 3) {
+ // Calibration Dance Step Three: Gyroscope
+ // Place sensor in any stable position for a few seconds
+ // (Accelerometer calibration may also calibrate the gyro)
+ get_calibration(NULL, &gyro, NULL, NULL);
+ printf("Gyro Calib Status: %3.0f\n", (100 / 3 * gyro));
+ sleep_ms(1000);
+ }
+ printf("... CALIBRATED\n");
+ sleep_ms(1000);
+ printf("CALIBRATION COMPLETED\n");
+
+ // Get Sensor Offsets
+ buf[0] = BNO055_OPR_MODE_ADDR;
+ buf[1] = BNO055_OPR_MODE_CONFIG;
+ uint8_t sensor_offsets[22];
+ i2c_write_blocking(i2c_default, BNO055_ADDRESS, buf, 2, false);
+ sleep_ms(30);
+
+ buf[0] = ACCEL_OFFSET_X_LSB_ADDR;
+ i2c_write_blocking(i2c_default, BNO055_ADDRESS, buf, 1, false);
+ i2c_read_blocking(i2c_default, BNO055_ADDRESS, sensor_offsets, 18, false);
+ for (uint8_t i = 0; i < 18; i++) {
+ printf("sensor_offsets[%" PRIu8 "] = 0x%" PRIx8 ";\r\n", i + 1, sensor_offsets[i]);
+ }
+ sleep_ms(5000);
+
+ buf[0] = BNO055_OPR_MODE_ADDR;
+ buf[1] = BNO055_OPR_MODE_NDOF;
+ i2c_write_blocking(i2c_default, BNO055_ADDRESS, buf, 2, false);
+ sleep_ms(5000);
+
+ getchar();
+
+ uint8_t lin_accel[6];
+ uint8_t quat[8];
+ float accel_x, accel_y, accel_z;
+ float abs_lin_accel_x, abs_lin_accel_y, abs_lin_accel_z;
+ float abs_quaternion_w, abs_quaternion_x, abs_quaternion_y, abs_quaternion_z;
+ while (1) {
+ uint8_t lin_accel_reg = BNO055_LINEAR_ACCEL_DATA_X_LSB_ADDR;
+ i2c_write_blocking(i2c_default, BNO055_ADDRESS, &lin_accel_reg, 1, true);
+ i2c_read_blocking(i2c_default, BNO055_ADDRESS, lin_accel, 6, false);
+ int16_t x, y, z;
+ x = y = z = 0;
+ x = ((int16_t)lin_accel[0]) | (((int16_t)lin_accel[1]) << 8);
+ y = ((int16_t)lin_accel[2]) | (((int16_t)lin_accel[3]) << 8);
+ z = ((int16_t)lin_accel[4]) | (((int16_t)lin_accel[5]) << 8);
+ accel_x = ((float)x) / 100.0;
+ accel_y = ((float)y) / 100.0;
+ accel_z = ((float)z) / 100.0;
+
+ uint8_t quat_reg = BNO055_QUATERNION_DATA_W_LSB_ADDR;
+ i2c_write_blocking(i2c_default, BNO055_ADDRESS, &quat_reg, 1, true);
+ i2c_read_blocking(i2c_default, BNO055_ADDRESS, quat, 8, false);
+ int16_t w;
+ w = x = y = z = 0;
+ w = ((int16_t)quat[0]) | (((int16_t)quat[1]) << 8);
+ x = ((int16_t)quat[2]) | (((int16_t)quat[3]) << 8);
+ y = ((int16_t)quat[4]) | (((int16_t)quat[5]) << 8);
+ z = ((int16_t)quat[6]) | (((int16_t)quat[7]) << 8);
+ abs_quaternion_w = ((float)w) / 16384.0; // 2^14 LSB
+ abs_quaternion_x = ((float)x) / 16384.0;
+ abs_quaternion_y = ((float)y) / 16384.0;
+ abs_quaternion_z = ((float)z) / 16384.0;
+
+ Eigen::Quaternion<float> q;
+ q.w() = abs_quaternion_w;
+ q.x() = abs_quaternion_x;
+ q.y() = abs_quaternion_y;
+ q.z() = abs_quaternion_z;
+ // q.normalize();
+ Eigen::Matrix3f rotation_matrix = q.toRotationMatrix();
+ Eigen::Vector3f lin_accel;
+ abs_lin_accel_x = accel_x* rotation_matrix(0, 0) + accel_y * rotation_matrix(0, 1) + accel_z* rotation_matrix(0, 2);
+ abs_lin_accel_y = accel_x * rotation_matrix(1, 0) + accel_y * rotation_matrix(1, 1) + accel_z * rotation_matrix(1, 2);
+ abs_lin_accel_z = -1.0f * (accel_x * rotation_matrix(2, 0) + accel_y * rotation_matrix(2, 1) + accel_z * rotation_matrix(2, 2));
+
+ printf("Acceleration Vector: %4.2f, %4.2f, %4.2f\n", accel_x, accel_y, accel_z);
+ printf("Abs Acceleration Vector: %4.2f, %4.2f, %4.2f\n", abs_lin_accel_x, abs_lin_accel_y, abs_lin_accel_z);
+ printf("Quaternion: %4.2f, %4.2f, %4.2f, %4.2f\n\n\n", abs_quaternion_w, abs_quaternion_x, abs_quaternion_y, abs_quaternion_z);
+ sleep_ms(1000);
+ }
+
+ return 0;
+}
+
+void get_calibration(uint8_t *sys, uint8_t *gyro, uint8_t *accel, uint8_t *mag) {
+ uint8_t buf[1] = {BNO055_CALIB_STAT_ADDR};
+ uint8_t cal_data = 0x00;
+ i2c_write_blocking(i2c_default, BNO055_ADDRESS, buf, 1, false);
+ i2c_read_blocking(i2c_default, BNO055_ADDRESS, &cal_data, 1, false);
+ if (sys != NULL) {
+ *sys = (cal_data >> 6) & 0x03;
+ }
+ if (gyro != NULL) {
+ *gyro = (cal_data >> 4) & 0x03;
+ }
+ if (accel != NULL) {
+ *accel = (cal_data >> 2) & 0x03;
+ }
+ if (mag != NULL) {
+ *mag = cal_data & 0x03;
+ }
+}
+
diff --git a/tools/read_flash.c b/tools/read_flash.c
new file mode 100644
index 0000000..91c75fb
--- /dev/null
+++ b/tools/read_flash.c
@@ -0,0 +1,78 @@
+#include <stdio.h>
+#include <inttypes.h>
+#include "boards/pico_w.h"
+#include "hardware/spi.h"
+#include "spi_flash.h"
+
+int main() {
+ stdio_init_all();
+ getchar();
+ // Enable SPI 0 at 1 MHz and connect to GPIOs
+ spi_init(spi_default, 1000 * 1000 * 60);
+ gpio_set_function(PICO_DEFAULT_SPI_RX_PIN, GPIO_FUNC_SPI);
+ gpio_set_function(PICO_DEFAULT_SPI_TX_PIN, GPIO_FUNC_SPI);
+ gpio_set_function(PICO_DEFAULT_SPI_SCK_PIN, GPIO_FUNC_SPI);
+
+ // Chip select is active-low, so we'll initialise it to a driven-high state
+ gpio_init(PICO_DEFAULT_SPI_CSN_PIN);
+ gpio_set_dir(PICO_DEFAULT_SPI_CSN_PIN, GPIO_OUT);
+ gpio_put(PICO_DEFAULT_SPI_CSN_PIN, 1);
+
+ uint8_t entry[PACKET_SIZE];
+
+ // flash_erase(spi_default, PICO_DEFAULT_SPI_CSN_PIN);
+ flash_read(spi_default, PICO_DEFAULT_SPI_CSN_PIN, base_addr, page_buffer, FLASH_PAGE_SIZE);
+ for (uint16_t i = 0; i < FLASH_PAGE_SIZE; i += PACKET_SIZE) {
+ if (page_buffer[i] == 0xFF) {
+ base_addr += i;
+ break;
+ }
+ if ((i + PACKET_SIZE) == FLASH_PAGE_SIZE) {
+ base_addr += FLASH_PAGE_SIZE;
+ flash_read(spi_default, PICO_DEFAULT_SPI_CSN_PIN, base_addr, page_buffer, FLASH_PAGE_SIZE);
+ i = 0;
+ }
+ }
+
+ printf("\nRead Data:\n");
+ printf("time,state,board_temp,deploy_percent,altitude,velocity,lin_ax,lin_ay,lin_az,quat_w,quat_x,quat_y,quat_z\n");
+ for (uint32_t i = 0; i < base_addr; i += PACKET_SIZE) {
+ flash_read(spi_default, PICO_DEFAULT_SPI_CSN_PIN, i, entry, PACKET_SIZE);
+ uint64_t now_us = (((uint64_t)entry[0] << 56) | ((uint64_t)entry[1] << 48) | \
+ ((uint64_t)entry[2] << 40) | ((uint64_t)entry[3] << 32) | \
+ ((uint64_t)entry[4] << 24) | ((uint64_t)entry[5] << 16) | \
+ ((uint64_t)entry[6] << 8) | ((uint64_t)entry[7]));
+
+ uint8_t state = entry[8] >> 4;
+ uint16_t temperature_data = ((uint16_t)(entry[8] & 0x0F) << 8) | ((uint16_t)entry[9]);
+ const float conversionFactor = 3.3f / (1 << 12);
+ float tempC = 27.0f - (((float)(temperature_data) * conversionFactor) - 0.706f) / 0.001721f;
+
+ uint8_t deploy_percent = entry[10];
+
+ float altitude = (float) ((int16_t) ((entry[11] << 8) | entry[12])) + (float) (entry[13] >> 4) * 0.0625;
+ uint32_t vel_bits = (entry[14] << 24) | (entry[15] << 16) | (entry[16] << 8) | (entry[17]);
+ float velocity = *(float *)(&vel_bits);
+
+ int16_t ax = ((int16_t)entry[18]) | (((int16_t)entry[19]) << 8);
+ int16_t ay = ((int16_t)entry[20]) | (((int16_t)entry[21]) << 8);
+ int16_t az = ((int16_t)entry[22]) | (((int16_t)entry[23]) << 8);
+ float lax = ((float)ax) / 100.0;
+ float lay = ((float)ay) / 100.0;
+ float laz = ((float)az) / 100.0;
+
+ int16_t w, x, y, z;
+ w = x = y = z = 0;
+ w = ((int16_t)entry[24]) | (((int16_t)entry[25]) << 8);
+ x = ((int16_t)entry[26]) | (((int16_t)entry[27]) << 8);
+ y = ((int16_t)entry[28]) | (((int16_t)entry[29]) << 8);
+ z = ((int16_t)entry[30]) | (((int16_t)entry[31]) << 8);
+ float qw = ((float)w) / 16384.0;
+ float qx = ((float)x) / 16384.0;
+ float qy = ((float)y) / 16384.0;
+ float qz = ((float)z) / 16384.0;
+
+ printf("%"PRIu64",%"PRIu8",%04.2f,%"PRIu8",%04.2f,%04.2f,%04.2f,%04.2f,%04.2f,%04.2f,%04.2f,%04.2f,%04.2f\r\n", \
+ now_us, state, tempC, deploy_percent, altitude, velocity, lax, lay, laz, qw, qx, qy, qz);
+ }
+}
diff --git a/tools/servo_test.cpp b/tools/servo_test.cpp
new file mode 100644
index 0000000..c5e8e6e
--- /dev/null
+++ b/tools/servo_test.cpp
@@ -0,0 +1,27 @@
+#include <stdio.h>
+#include <inttypes.h>
+#include "pico/stdio.h"
+#include "pwm.hpp"
+
+#define MOSFET_PIN 1
+
+PWM pwm;
+
+int main() {
+ stdio_init_all();
+ // Initialize MOSFET
+ gpio_init(MOSFET_PIN);
+ gpio_set_dir(MOSFET_PIN, GPIO_OUT);
+ gpio_put(MOSFET_PIN, 1);
+ pwm.init();
+ uint8_t duty_cycle = 13;
+ while (1) {
+ getchar();
+ if (duty_cycle == 2) {
+ duty_cycle = 13;
+ }
+ pwm.set_duty_cycle(duty_cycle);
+ printf("Currenty Duty Cycle: %" PRIu8 "\n", duty_cycle);
+ duty_cycle--;
+ }
+}