Skip to content

Commit

Permalink
Control flow test modes run w/o sensors + global config
Browse files Browse the repository at this point in the history
  • Loading branch information
grwells committed Apr 5, 2022
1 parent ced63dd commit 42da834
Show file tree
Hide file tree
Showing 10 changed files with 194 additions and 106 deletions.
1 change: 1 addition & 0 deletions include/IMU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
* 3. To print debug data to the serial port there are helper functions in this file.
* Make sure you setup Serial first.
***************************************************************/
#include <global.h>
#include "ICM_20949.h" // Click here to get the library: http://librarymanager/All#SparkFun_ICM_20949_IMU
#include <Wire.h>
#include "wiring_private.h"
Expand Down
4 changes: 3 additions & 1 deletion include/comm.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef __COMM_H__
#define __COMM_H__

#include <global.h>
#include "CRC16.h"
#include <stdint.h>

Expand Down Expand Up @@ -40,7 +41,8 @@ enum Status : uint8_t
STATUS_OK = 0xaa, // "Heartbeat"
STATUS_HELLO = 0xaf, // Sent upon system init
STATUS_ADCS_ERROR = 0xf0, // Sent upon runtime error
STATUS_COMM_ERROR = 0x99 // Sent upon invalid communication
STATUS_COMM_ERROR = 0x99, // Sent upon invalid communication
STATUS_FUDGED = 0x00 // Data is not real, just test output
};

/**
Expand Down
1 change: 1 addition & 0 deletions include/commandFunctions.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define __COMMAND_FUNCTIONS_H__


#include <global.h>
#include "comm.h"
#include "sensors.h"
#include "DRV_10970.h"
Expand Down
28 changes: 28 additions & 0 deletions include/global.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#ifndef GLOBAL_H
#define GLOBAL_H

/*
* Contains all preprocessor definitions that should be held(included) commonly across all source and header
* files in the project.
*
* Notable entries will include:
* 1. Sensor configurations (number of IMUs)
* 2. Peripheral interface enables (Serial, I2C, UART)
* 3. Debug/Test defines for test modes
*/

/* SENSOR CONFIGURATIONS */
#define TWO_IMUS 0

/* PERIPHERAL DEFINITIONS */
#define SERCOM_USB Serial
#define SERCOM_UART Serial1
#define SERCOM_I2C Wire

/* DEBUG/TEST DEFINITIONS */
#define TEST_CONTROL_FLOW 1 /* if non-zero then assume satellite is connected but no sensors */
#define RTOS_TEST_SUITE 0 /* if non-zero then rtos test tasks in validation_tests.h will be created */
#define DEBUG 1 /* if non-zero then debug statements will be printed over USB serial */


#endif
1 change: 1 addition & 0 deletions include/sensors.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef __SENSORS_H__
#define __SENSORS_H__

#include <global.h>
#include "comm.h"
#include "ICM_20948.h"
#include "INA209.h"
Expand Down
4 changes: 3 additions & 1 deletion include/supportFunctions.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef __SUPPORT_FUNCTIONS_H__
#define __SUPPORT_FUNCTIONS_H__

#include <global.h>

/* These are functions that support the main command functions in one way
* or another they are called upon in the commandFunctions.cpp file */
/* I tried to make these as self-explanatory as possible by name
Expand All @@ -18,7 +20,7 @@
#define MODE_TEST_SMPLTUMBLE 6 /* starting in spin, attempt to stop spinning */
#define MODE_TEST_ORIENT 7 /* try orienting the adcs system */

// #define TWO_IMUS
//#define TWO_IMUS

//Parses cmd and calls appropriate function
void doCmd(char *cmd);
Expand Down
1 change: 1 addition & 0 deletions include/validation_tests.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* see supportFunctions.h
*/

#include <global.h>
#include <DRV_10970.h>
#include <ICM_20948.h>
#include <FreeRTOS_SAMD51.h>
Expand Down
138 changes: 73 additions & 65 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Our own headers
#include <global.h> /* definitions of interfaces and test modes */
#include "samd51_pin_definitions.h"
#include "comm.h" /* data packets and UART transmission functions */
#include "sensors.h" /* read from sensors into heartbeat packet */
Expand All @@ -7,21 +8,13 @@
#include "DRV_10970.h"
#include "validation_tests.h" // tests for functionality

// only include this if test functions are desired
//#include <test.h>

// Arduino library headers
#include "INA209.h"
#include "ICM_20948.h"
#include "FreeRTOS_SAMD51.h"
// Standard C/C++ library headers
#include <stdint.h>

// TESTING DEFINES //////////////////////////////////////////////////////////////
#define DEBUG /* print the serial debug messages over USB connection */
#define TEST_RTOS /* if this is defined, then test tasks will be available */
/////////////////////////////////////////////////////////////////////////////////

/* NON-RTOS GLOBAL VARIABLES ================================================ */

/**
Expand Down Expand Up @@ -79,9 +72,9 @@ QueueHandle_t modeQ;
* setup function as if it were main. Since setup runs only once, it
* essentially behaves the same as main.
*/
int main(void)
void setup(void)
{
#ifdef DEBUG
#if DEBUG
/**
* Initialize USB connection to computer. Used to print debug messages.
* Baud rate: 115200
Expand All @@ -101,7 +94,7 @@ int main(void)
*/
SERCOM_UART.begin(115200, SERIAL_8O1);
while (!SERCOM_UART); // wait for initialization to complete
#ifdef DEBUG
#if DEBUG
SERCOM_USB.write("UART interface initialized\r\n");
#endif

Expand All @@ -122,8 +115,9 @@ int main(void)
data_packet.setStatus(STATUS_HELLO);
data_packet.computeCRC();
data_packet.send();


vTaskStartScheduler(); // start the RTOS
vTaskStartScheduler(); // start the RTOS

// should never be reached if everything goes right
while (1)
Expand All @@ -133,7 +127,6 @@ int main(void)
data_packet.send();
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
return 0;
}

/*
Expand All @@ -144,9 +137,12 @@ int main(void)
*/
void loop(){
/* SAMD51 USBCore.cpp sets enable bit during sleep standby mode so USB needs to be disabled if in DEBUG*/
#ifdef DEBUG
USB->HOST.CTRLA.reg &= ~USB_CTRLA_ENABLE; // disable USB in standby sleep mode...
#if !TEST_CONTROL_FLOW
#if DEBUG
USB->HOST.CTRLA.reg &= ~USB_CTRLA_ENABLE; // disable USB in standby sleep mode...
#endif
#endif

// SLEEPRDY bit must be set to sleep
if(PM_INTFLAG_SLEEPRDY)
__WFI(); // enter low power mode when running Idle task to enter low power mode
Expand All @@ -157,7 +153,6 @@ void loop(){

/* Initialize all the GPIO pins and the builtin LED */
void init_hardware(void){
Serial.println("INIT HARDWARE\n");
// INIT SLEEP REG
// configure power manager (PM) to enter STANDBY mode on _WFI()
PM->SLEEPCFG.bit.SLEEPMODE = 0x4;
Expand All @@ -174,6 +169,10 @@ void init_hardware(void){

pinMode(10, OUTPUT);
analogWrite(10, 0); // set the PWM pin to 0%

#if DEBUG
SERCOM_USB.println("HARDWARE INITIALIZED");
#endif
}

/*
Expand Down Expand Up @@ -214,56 +213,62 @@ void init_PWM_50kHz(void){

TCC0->CTRLA.bit.ENABLE = 1; // Enable timer TCC0
while (TCC0->SYNCBUSY.bit.ENABLE); // Wait for synchronization

#if DEBUG
SERCOM_USB.println("PWM INITIALIZED");
#endif
}


/* Setup the sensor objects */
void init_sensors(void){
/**
* Initialize first IMU
* Address: 0x69 or 0x68
*/
IMU1.begin(SERCOM_I2C, AD0_VAL);
while (IMU1.status != ICM_20948_Stat_Ok); // wait for initialization to
// complete
#ifdef DEBUG
SERCOM_USB.write("IMU1 initialized\r\n");
#endif

#ifdef TWO_IMUS
#if !TEST_CONTROL_FLOW
/**
* Initialize second IMU
* Address: 0x68 or 0x69
*/
IMU2.begin(SERCOM_I2C, AD0_VAL^1); // initialize other IMU with opposite
// value for bit 0
while (IMU2.status != ICM_20948_Stat_Ok); // wait for initialization to
// complete
#ifdef DEBUG
SERCOM_USB.write("IMU2 initialized\r\n");
* Initialize first IMU
* Address: 0x69 or 0x68
*/
IMU1.begin(SERCOM_I2C, AD0_VAL);
while (IMU1.status != ICM_20948_Stat_Ok); // wait for initialization to
// complete
#if DEBUG
SERCOM_USB.write("IMU1 initialized\r\n");
#endif

#if TWO_IMUS
/**
* Initialize second IMU
* Address: 0x68 or 0x69
*/
IMU2.begin(SERCOM_I2C, AD0_VAL^1); // initialize other IMU with opposite
// value for bit 0
while (IMU2.status != ICM_20948_Stat_Ok); // wait for initialization to
// complete
#if DEBUG
SERCOM_USB.write("IMU2 initialized\r\n");
#endif
#endif

/**
* Write default settings to INA209
* Reset: no
// * Bus voltage range: 32V
* PGA gain: /8
* PGA range: +-320mV
* ADC resolution: 12 bits
* ADC conversion time: 532us
* Mode: shunt and bus, continuous
*/
ina209.writeCfgReg(0x399f);

/**
* Calibrate INA209
* Current LSB: 100uA
*/
ina209.writeCal(0x7fff);
#endif

/**
* Write default settings to INA209
* Reset: no
// * Bus voltage range: 32V
* PGA gain: /8
* PGA range: +-320mV
* ADC resolution: 12 bits
* ADC conversion time: 532us
* Mode: shunt and bus, continuous
*/
ina209.writeCfgReg(0x399f);

/**
* Calibrate INA209
* Current LSB: 100uA
*/
ina209.writeCal(0x7fff);

#ifdef DEBUG
SERCOM_USB.write("INA209 initialized\r\n");
#if DEBUG
SERCOM_USB.println("SENSORS INITIALIZED");
#endif

}
Expand All @@ -278,15 +283,18 @@ void init_rtos_architecture(void){

xTaskCreate(readUART, "Read UART", 2048, NULL, 1, readUART_h);
//xTaskCreate(writeUART, "Write UART", 2048, NULL, 1, NULL); // test function to send heartbeat every half-second
#if DEBUG
SERCOM_USB.println("INITIALIZED COMMAND MONITOR");
#endif

// TESTS
#ifdef TEST_RTOS
#if RTOS_TEST_SUITE
#if DEBUG
SERCOM_USB.println("REQUESTED: INITIALIZE RTOS TEST SUITE");
#endif
create_test_tasks(); // if we are in test mode, create the tasks
#endif

#ifdef DEBUG
SERCOM_USB.write("Tasks created\r\n");
#endif
}

/* RTOS TASK DEFINITIONS ==================================================== */
Expand All @@ -309,7 +317,7 @@ static void readUART(void *pvParameters)
ADCSdata response;
uint8_t mode;

#ifdef DEBUG
#if DEBUG
char cmd_str[8]; // used to print command value to serial monitor
#endif

Expand All @@ -335,7 +343,7 @@ static void readUART(void *pvParameters)
response.send();
}

#ifdef DEBUG
#if DEBUG
// convert int to string for USB monitoring
sprintf(cmd_str, "0x%02x", cmd_packet.getCommand());

Expand Down Expand Up @@ -407,15 +415,15 @@ void state_machine_transition(TEScommand cmand){

default: // do nothing
command_is_valid = false;
#ifdef DEBUG
#if DEBUG
Serial.println("HIT AN UNKNOWN OR UNIMPLEMENTED COMMAND");
#endif
}

if(command_is_valid){
xQueueOverwrite(modeQ, (void*)&mode); // enter specified mode

#ifdef DEBUG
#if DEBUG
char cmd_str[8]; // used to print command value to serial monitor
// convert int to string for USB monitoring
sprintf(cmd_str, "0x%02x", cmand.getCommand());
Expand Down
Loading

0 comments on commit 42da834

Please sign in to comment.