Skip to content
Snippets Groups Projects
Commit 575a12e6 authored by Lukas Kretschmar's avatar Lukas Kretschmar
Browse files

Release of Ex07

parent 710346a2
Branches
No related tags found
No related merge requests found
.pio
.vscode/*
!.vscode/extensions.json
*.code-workspace
# Local History for Visual Studio Code
.history/
\ No newline at end of file
{
// See http://go.microsoft.com/fwlink/?LinkId=827846
// for the documentation about the extensions.json format
"recommendations": [
"platformio.platformio-ide"
]
}
# Sensorbox Ex - Reading Data # DigInd 07: Sensorbox - Reading Data
In this exercise, you are going to read data from the sensors built into your sensorbox.
; PlatformIO Project Configuration File
;
; Build options: build flags, source filter
; Upload options: custom upload port, speed and extra flags
; Library options: dependencies, extra library storages
; Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html
[env:huzzah]
platform = espressif8266
board = huzzah
framework = arduino
monitor_speed = 115200
lib_deps =
adafruit/Adafruit LSM9DS1 Library @ 2.0.2
#include "MinMaxAvgFilter.h"
#include "Arduino.h"
MinMaxAvgFilter::MinMaxAvgFilter(){};
void MinMaxAvgFilter::addMeasurement(float xVal, float yVal, float zVal)
{
value.x.max = (xVal < value.x.max) ? value.x.max : xVal;
value.x.min = (xVal > value.x.min) ? value.x.min : xVal;
value.x.avg = (count * value.x.avg + xVal) / (count + 1);
value.y.max = (yVal < value.y.max) ? value.y.max : yVal;
value.y.min = (yVal > value.y.min) ? value.y.min : yVal;
value.y.avg = (count * value.y.avg + yVal) / (count + 1);
value.z.max = (zVal < value.z.max) ? value.z.max : zVal;
value.z.min = (zVal > value.z.min) ? value.z.min : zVal;
value.z.avg = (count * value.z.avg + zVal) / (count + 1);
count++;
};
void MinMaxAvgFilter::reset()
{
value.x.min = 0;
value.x.max = 0;
value.x.avg = 0;
value.y.min = 0;
value.y.max = 0;
value.y.avg = 0;
value.z.min = 0;
value.z.max = 0;
value.z.avg = 0;
count = 0;
}
#pragma once
#include "dataStructures.h"
class MinMaxAvgFilter
{
public:
MinMaxAvgFilter();
void addMeasurement(float x, float y, float z);
void reset();
XYZ<float> getValue(){return value;}
private:
XYZ<float> value;
int count = 0;
};
This directory is intended for project specific (private) libraries.
PlatformIO will compile them to static libraries and link into executable file.
The source code of each library should be placed in a an own separate directory
("lib/your_library_name/[here are source files]").
For example, see a structure of the following two libraries `Foo` and `Bar`:
|--lib
| |
| |--Bar
| | |--docs
| | |--examples
| | |--src
| | |- Bar.c
| | |- Bar.h
| | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html
| |
| |--Foo
| | |- Foo.c
| | |- Foo.h
| |
| |- README --> THIS FILE
|
|- platformio.ini
|--src
|- main.c
and a contents of `src/main.c`:
```
#include <Foo.h>
#include <Bar.h>
int main (void)
{
...
}
```
PlatformIO Library Dependency Finder will find automatically dependent
libraries scanning project source files.
More information about PlatformIO Library Dependency Finder
- https://docs.platformio.org/page/librarymanager/ldf.html
#include "Sensor_LSM9DS1.h"
#include <Adafruit_Sensor.h> // not used but required!
#include <Wire.h>
Sensor_LSM9DS1::Sensor_LSM9DS1(){}
bool Sensor_LSM9DS1::begin()
{
if (!lsm_sensor.begin())
{
return false;
}
lsm_sensor.setupAccel(lsm_sensor.LSM9DS1_ACCELRANGE_2G);
lsm_sensor.setupMag(lsm_sensor.LSM9DS1_MAGGAIN_4GAUSS);
lsm_sensor.setupGyro(lsm_sensor.LSM9DS1_GYROSCALE_245DPS);
return true;
}
void Sensor_LSM9DS1::measure()
{
lsm_sensor.read();
sensors_event_t a, m, g, temp;
lsm_sensor.getEvent(&a, &m, &g, &temp);
acceleration.addMeasurement(a.acceleration.x, a.acceleration.y, a.acceleration.z);
// TODO: Call the setMeasurement() method for m.magnetic and g.gyro.
// You may have to check the header file to get the fields
// used to store the values to.
// TODO: Increment the numberOfMeasurements counter
}
void Sensor_LSM9DS1::reset()
{
// TODO: Call the reset() method on all measurement fields.
// TODO: Reset the numberOfMeasurements counter to 0.
}
#pragma once
#include "MinMaxAvgFilter.h"
#include <Arduino.h>
#include <Adafruit_LSM9DS1.h>
class Sensor_LSM9DS1
{
public:
Sensor_LSM9DS1();
bool begin();
void measure();
void reset();
unsigned int getNumberOfMeasurements(){return numberOfMeasurements;}
XYZ<float> getAcceleration(){return acceleration.getValue();}
XYZ<float> getMagneticFluxDensities(){return magneticFluxDensities.getValue();}
XYZ<float> getAngularVelocity(){return angularVelocity.getValue();}
private:
Adafruit_LSM9DS1 lsm_sensor;
MinMaxAvgFilter acceleration;
MinMaxAvgFilter magneticFluxDensities;
MinMaxAvgFilter angularVelocity;
unsigned int numberOfMeasurements = 0;
};
#pragma once
/* Debugging */
#define DEBUGMODE // Send Debug messages
#pragma once
template <typename T>
struct MinMaxAvg
{
T min {0};
T max {0};
T avg {0};
};
template <typename Z>
struct XYZ
{
MinMaxAvg<Z> x;
MinMaxAvg<Z> y;
MinMaxAvg<Z> z;
};
#pragma once
#ifdef DEBUGMODE
#define debug(...) Serial.print(__VA_ARGS__)
#else
#define debug(...)
#endif
#ifdef DEBUGMODE
#define debugLn(...); Serial.println(__VA_ARGS__)
#else
#define debugLn(...)
#endif
#include "config.h"
#include "debug.h"
#include <Arduino.h>
#include "Sensor_LSM9DS1.h"
void printData(const XYZ<float> &, const char *, const char *);
void printValue(const char *, float, const char *);
void tab();
void newLine();
// TODO: Create a new field for your sensor instance of type Sensor_LSM9DS1.
void setup()
{
#ifdef DEBUGMODE
// TODO: Set the correct baud-rate in to Serial.begin(baud-rate).
Serial.begin(0);
while (!Serial)
{
delay(1);
}
#endif
// TODO: Check if the sensor is ready to be used. Call the begin() method of the instance until it returns true.
// The return value should be used as exit criteria within the while loop.
while (true)
{
debugLn("Cannot connect to LSM9DS1-Sensor.");
delay(3000);
}
newLine();
}
void loop()
{
delay(10);
// TODO: Call the measure() method of the sensor.
// TODO: Only show the values after 50 measurements.
if (true)
{
return;
}
// TODO: Use the printData() method to show the data of each sensor.
// Use the following units for the sensor data:
// - Acceleration: m/s^2
// - Magnetic flux density: uT
// - Angular velocity: rad/s
// TODO: Print a new line so the values are shown beneath each other
// TODO: Reset the data of the sensor.
}
void printData(const XYZ<float> & data, const char * sensor, const char * unit)
{
// TODO: Print the values (x,y & z) for one sensor. Show the sensor type (sensor) first,
// and use the printValue() method from below to show one value of the sensor.
// Show the average of each data point. Split all parts with a tab. The result should
// look like the example in the exercise.
}
void printValue(const char * name, float value, const char * unit)
{
//TODO: Print the value according to the following format: "{name}: {value} {unit}".
// The { } just denote where you need to use the parameters of this method.
}
void tab()
{
Serial.print("\t");
}
void newLine()
{
Serial.println();
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment