Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

epic compass gaming!!1! #4

Merged
merged 13 commits into from
May 6, 2024
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,6 @@ build/*

# Ignore vscode
.vscode/*

# .vim session files
*.vim
68 changes: 62 additions & 6 deletions hhs-system-engineer-project.ino
Original file line number Diff line number Diff line change
@@ -1,20 +1,37 @@
#include <Arduino.h>
bool debug = 1;

#include "work/headers.hpp"
#include "work/ran-func.cpp"
#include "work/serial/serial.cpp"
#include "work/sensor/compass/accelerometer/accel.h"
#include "work/sensor/compass/gyroscope/gyro.h"
#include "work/sensor/compass/magnetometer/magnet.h"

int test = 0;
int speed = 0;

Accelerometer* accel;
Gyroscope* gyro;
Magnetometer* magnet;

Vector3 accelData;
Vector3 gyroData;
Vector3 magnetData;

char imuOutBuffer[139];

#define RECHTS OCR1A
#define LINKS OCR1B

auto& xbee = Serial;
//auto& xbee = Serial;

void setup() {
Wire.begin();

Serial1.begin(4800);
Serial1.println("Zumo Active, Serial1 Output");

xbee.begin(4800);
// xbee.begin(4800);

pinMode(A10, OUTPUT);
pinMode(A9, OUTPUT);
Expand All @@ -25,11 +42,50 @@ void setup() {
SetupTimer1();

Serial.println();

// check if compass can be initialized properly
Zumo32U4IMU* compassInterfacePTR = new Zumo32U4IMU();
if (compassInterfacePTR->init()){
Serial1.println("Compass interface initialized successfully!");
compassInterfacePTR->enableDefault();
*accel = Accelerometer(compassInterfacePTR);
*gyro = Gyroscope(compassInterfacePTR);
*magnet = Magnetometer(compassInterfacePTR);
if (debug) {
accel->PrintDebugInfo();
gyro->PrintDebugInfo();
magnet->PrintDebugInfo();
}
} else {
Serial1.println("Compass interface probably not initialized successfully?");
}
// end of compass initialization test
}

void loop() {
if(Serial1.available()){
readserial(test,speed);
}
//if(Serial1.available()){
//readserial(test,speed);
//}
accelData = accel->Values();
gyroData = gyro->Values();
magnetData = magnet->Values();

printCompassValues();
}

void printCompassValues() {
sprintf(imuOutBuffer, "%s\r\n%s%d\r\n%s%d\r\n%s%d\r\n%s%d\r\n%s%d\r\n%s%d\r\n%s%d\r\n%s%d\r\n%s%d\r\n%s",
"--------------------",
"aX: ", accelData.x,
"aY: ", accelData.y,
"aZ: ", accelData.z,
"gX: ", gyroData.x,
"gY: ", gyroData.y,
"gZ: ", gyroData.z,
"mX: ", magnetData.x,
"mY: ", magnetData.y,
"mZ: ", magnetData.z,
"--------------------");

Serial1.println(imuOutBuffer);
}
15 changes: 15 additions & 0 deletions work/sensor/compass/accelerometer/accel.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
void Accelerometer::pvt_RetrieveData() {
// if new data ready
if (m_InterfacePTR->accDataReady())
{
// get new reading
m_InterfacePTR->readAcc();

// copy reading to m_Vector
m_Vector.x = m_InterfacePTR->a.x;
m_Vector.y = m_InterfacePTR->a.y;
m_Vector.z = m_InterfacePTR->a.z;
}
}

// written by: Erynn 'foorpyxof' Scholtes | 2024 NSE Zumo project
28 changes: 28 additions & 0 deletions work/sensor/compass/accelerometer/accel.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include "../../../headers.hpp"
#include "../compassbase.h"

#ifndef ACCEL_H
#define ACCEL_H

/** Class that interfaces with
* the Accelerometer sensor in the IMU
* to read its data
*/
class Accelerometer : private CompassBase {
private:
/**
* Private method that checks if the Accelerometer
* has new data available, and if so, it copies it into m_Vector
*/
void pvt_RetrieveData() override;
public:
using CompassBase::CompassBase;
using CompassBase::Values;
using CompassBase::PrintDebugInfo;
};

#include "accel.cpp"

#endif

// written by: Erynn 'foorpyxof' Scholtes | 2024 NSE Zumo project
15 changes: 15 additions & 0 deletions work/sensor/compass/compassbase.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
CompassBase::CompassBase(Zumo32U4IMU* zumoInterfacePTR): m_Vector{0, 0, 0}, m_InterfacePTR(zumoInterfacePTR) { }

Vector3 CompassBase::Values() {

// call pvt_RetrieveData to place sensor reading in m_Vector
this->pvt_RetrieveData();

return m_Vector;
}

void CompassBase::PrintDebugInfo() {
Serial1.println((uint8_t)m_InterfacePTR->getType());
}

// written by: Erynn 'foorpyxof' Scholtes | 2024 NSE Zumo project
88 changes: 88 additions & 0 deletions work/sensor/compass/compassbase.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#include "../../headers.hpp"
#include <Zumo32U4IMU.h>

#ifndef COMPASSBASE_H
#define COMPASSBASE_H

/**
* the struct containing three
* 16bit integers that will hold
* the values read by the sensor
* attached to the child-class
* of CompassBase
*/
struct Vector3 { int16_t x,y,z; };

/**
* This is a class meant as a template only!!!
* Is abstract class! :3c
*
* This is the base class for all Zumo IMU-interfacing classes.
* The Accelerometer, Gyroscope and Magnetometer classes
* are all derived from this class (CompassBase).
*/
class CompassBase {
protected:
/**
* the Vector3 struct (x,y,z) that holds
* the data that gets read from the sensor
*/
Vector3 m_Vector;

/**
* The object that allows us to interface with the IMU-sensors.
* It contains methods to read raw values
* as well as three containers to
* temporarily store them in;
*
*
* Container names:
* 'a' for accel readings -
* 'g' for gyro readings -
* 'm' for magnet readings
*
* These containers will only be used by
* their respective derived-classes
*/
Zumo32U4IMU* m_InterfacePTR;


private:
/**
* Template method that will make
* the derived class read from
* its respective sensor
*/
virtual void pvt_RetrieveData() = 0;

public:
/**
* Constructor that places a pointer to
* the pre-initialized Zumo32U4IMU sensor class
* in the m_InterfacePTR member variable
*/
CompassBase(Zumo32U4IMU*);

/**
* Public method that allows the Zumo to read
* whatever is currently stored in 'm_Vector'
*
* Basically: This method returns the current x,y,z (Vector3)
* for the sensor you call this method on.
*/
Vector3 Values();

/**
* Prints debug info.
* Currently only the type of sensor that is detected.
*/
void PrintDebugInfo();

// methods are defined in attached file, 'compassbase.cpp'
};

#include "compassbase.cpp"

#endif

// written by: Erynn 'foorpyxof' Scholtes | 2024 NSE Zumo project
15 changes: 15 additions & 0 deletions work/sensor/compass/gyroscope/gyro.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
void Gyroscope::pvt_RetrieveData() {
// if new data ready
if (m_InterfacePTR->gyroDataReady())
{
// get new reading
m_InterfacePTR->readGyro();

// copy reading to m_Vector
m_Vector.x = m_InterfacePTR->g.x;
m_Vector.y = m_InterfacePTR->g.y;
m_Vector.z = m_InterfacePTR->g.z;
}
}

// written by: Erynn 'foorpyxof' Scholtes | 2024 NSE Zumo project
28 changes: 28 additions & 0 deletions work/sensor/compass/gyroscope/gyro.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include "../../../headers.hpp"
#include "../compassbase.h"

#ifndef GYRO_H
#define GYRO_H

/** Class that interfaces with
* the Gyroscope sensor in the IMU
* to read its data
*/
class Gyroscope : private CompassBase {
private:
/**
* Private method that checks if the Gyroscope
* has new data available, and if so, it copies it into m_Vector
*/
void pvt_RetrieveData() override;
public:
using CompassBase::CompassBase;
using CompassBase::Values;
using CompassBase::PrintDebugInfo;
};

#include "gyro.cpp"

#endif

// written by: Erynn 'foorpyxof' Scholtes | 2024 NSE Zumo project
15 changes: 15 additions & 0 deletions work/sensor/compass/magnetometer/magnet.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
void Magnetometer::pvt_RetrieveData() {
// if new data ready
if (m_InterfacePTR->magDataReady())
{
// get new reading
m_InterfacePTR->readMag();

// copy reading to m_Vector
m_Vector.x = m_InterfacePTR->m.x;
m_Vector.y = m_InterfacePTR->m.y;
m_Vector.z = m_InterfacePTR->m.z;
}
}

// written by: Erynn 'foorpyxof' Scholtes | 2024 NSE Zumo project
28 changes: 28 additions & 0 deletions work/sensor/compass/magnetometer/magnet.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include "../../../headers.hpp"
#include "../compassbase.h"

#ifndef MAGNET_H
#define MAGNET_H

/** Class that interfaces with
* the Magnetometer sensor in the IMU
* to read its data
*/
class Magnetometer : private CompassBase {
private:
/**
* Private method that checks if the Magnetometer
* has new data available, and if so, it copies it into m_Vector
*/
void pvt_RetrieveData() override;
public:
using CompassBase::CompassBase;
using CompassBase::Values;
using CompassBase::PrintDebugInfo;
};

#include "magnet.cpp"

#endif

// written by: Erynn 'foorpyxof' Scholtes | 2024 NSE Zumo project