diff --git a/projects/MPU/.gitignore b/projects/MPU/.gitignore new file mode 100644 index 0000000..5fd7389 --- /dev/null +++ b/projects/MPU/.gitignore @@ -0,0 +1,2 @@ +.pio +.vscode/* \ No newline at end of file diff --git a/projects/MPU/.vscode/extensions.json b/projects/MPU/.vscode/extensions.json new file mode 100644 index 0000000..080e70d --- /dev/null +++ b/projects/MPU/.vscode/extensions.json @@ -0,0 +1,10 @@ +{ + // See http://go.microsoft.com/fwlink/?LinkId=827846 + // for the documentation about the extensions.json format + "recommendations": [ + "platformio.platformio-ide" + ], + "unwantedRecommendations": [ + "ms-vscode.cpptools-extension-pack" + ] +} diff --git a/projects/MPU/include/Adafruit_LIS3DH-1.2.6/Adafruit_LIS3DH.cpp b/projects/MPU/include/Adafruit_LIS3DH-1.2.6/Adafruit_LIS3DH.cpp new file mode 100644 index 0000000..16a863e --- /dev/null +++ b/projects/MPU/include/Adafruit_LIS3DH-1.2.6/Adafruit_LIS3DH.cpp @@ -0,0 +1,468 @@ +/*! + * @file Adafruit_LIS3DH.cpp + * + * @mainpage Adafruit LIS3DH breakout board + * + * @section intro_sec Introduction + * + * This is a library for the Adafruit LIS3DH Accel breakout board + * + * Designed specifically to work with the Adafruit LIS3DH Accel breakout board. + * + * Pick one up today in the adafruit shop! + * ------> https://www.adafruit.com/product/2809 + * + * This sensor communicates over I2C or SPI (our library code supports both) so + * you can share it with a bunch of other sensors on the same I2C bus. + * + * Adafruit invests time and resources providing this open source code, + * please support Adafruit andopen-source hardware by purchasing products + * from Adafruit! + * + * @section author Author + * + * K. Townsend / Limor Fried (Adafruit Industries) + * + * @section license License + * + * BSD license, all text above must be included in any redistribution + */ + +#include "Arduino.h" + +#include +#include + +/*! + * @brief Instantiates a new LIS3DH class in I2C + * @param Wi + * optional wire object + */ +Adafruit_LIS3DH::Adafruit_LIS3DH(TwoWire *Wi) + : _cs(-1), _mosi(-1), _miso(-1), _sck(-1), _sensorID(-1) { + I2Cinterface = Wi; +} + +/*! + * @brief Instantiates a new LIS3DH class using hardware SPI + * @param cspin + * number of CSPIN (Chip Select) + * @param *theSPI + * optional parameter contains spi object + */ +Adafruit_LIS3DH::Adafruit_LIS3DH(int8_t cspin, SPIClass *theSPI) { + _cs = cspin; + _mosi = -1; + _miso = -1; + _sck = -1; + _sensorID = -1; + SPIinterface = theSPI; +} + +/*! + * @brief Instantiates a new LIS3DH class using software SPI + * @param cspin + * number of CSPIN (Chip Select) + * @param mosipin + * number of pin used for MOSI (Master Out Slave In)) + * @param misopin + * number of pin used for MISO (Master In Slave Out) + * @param sckpin + * number of pin used for CLK (clock pin) + */ +Adafruit_LIS3DH::Adafruit_LIS3DH(int8_t cspin, int8_t mosipin, int8_t misopin, + int8_t sckpin) { + _cs = cspin; + _mosi = mosipin; + _miso = misopin; + _sck = sckpin; + _sensorID = -1; +} + +/*! + * @brief Setups the HW (reads coefficients values, etc.) + * @param i2caddr + * i2c address (optional, fallback to default) + * @param nWAI + * Who Am I register value - defaults to 0x33 (LIS3DH) + * @return true if successful + */ +bool Adafruit_LIS3DH::begin(uint8_t i2caddr, uint8_t nWAI) { + _i2caddr = i2caddr; + _wai = nWAI; + if (I2Cinterface) { + i2c_dev = new Adafruit_I2CDevice(_i2caddr, I2Cinterface); + + if (!i2c_dev->begin()) { + return false; + } + } else if (_cs != -1) { + + // SPIinterface->beginTransaction(SPISettings(500000, MSBFIRST, SPI_MODE0)); + if (_sck == -1) { + spi_dev = new Adafruit_SPIDevice(_cs, + 500000, // frequency + SPI_BITORDER_MSBFIRST, // bit order + SPI_MODE0, // data mode + SPIinterface); + } else { + spi_dev = new Adafruit_SPIDevice(_cs, _sck, _miso, _mosi, + 500000, // frequency + SPI_BITORDER_MSBFIRST, // bit order + SPI_MODE0); // data mode + } + + if (!spi_dev->begin()) { + return false; + } + } + + /* Check connection */ + if (getDeviceID() != _wai) { + /* No LIS3DH detected ... return false */ + // Serial.println(deviceid, HEX); + return false; + } + Adafruit_BusIO_Register _ctrl1 = Adafruit_BusIO_Register( + i2c_dev, spi_dev, ADDRBIT8_HIGH_TOREAD, LIS3DH_REG_CTRL1, 1); + _ctrl1.write(0x07); // enable all axes, normal mode + + // 400Hz rate + setDataRate(LIS3DH_DATARATE_400_HZ); + + Adafruit_BusIO_Register _ctrl4 = Adafruit_BusIO_Register( + i2c_dev, spi_dev, ADDRBIT8_HIGH_TOREAD, LIS3DH_REG_CTRL4, 1); + _ctrl4.write(0x88); // High res & BDU enabled + + enableDRDY(true, 1); + + // Turn on orientation config + + Adafruit_BusIO_Register _tmp_cfg = Adafruit_BusIO_Register( + i2c_dev, spi_dev, ADDRBIT8_HIGH_TOREAD, LIS3DH_REG_TEMPCFG, 1); + _tmp_cfg.write(0x80); // enable adcs + + return true; +} + +/*! + * @brief Get Device ID from LIS3DH_REG_WHOAMI + * @return WHO AM I value + */ +uint8_t Adafruit_LIS3DH::getDeviceID(void) { + Adafruit_BusIO_Register _chip_id = Adafruit_BusIO_Register( + i2c_dev, spi_dev, ADDRBIT8_HIGH_TOREAD, LIS3DH_REG_WHOAMI, 1); + + return _chip_id.read(); +} +/*! + * @brief Check to see if new data available + * @return true if there is new data available, false otherwise + */ +bool Adafruit_LIS3DH::haveNewData(void) { + Adafruit_BusIO_Register status_2 = Adafruit_BusIO_Register( + i2c_dev, spi_dev, ADDRBIT8_HIGH_TOREAD, LIS3DH_REG_STATUS2, 1); + Adafruit_BusIO_RegisterBits zyx_data_available = + Adafruit_BusIO_RegisterBits(&status_2, 1, 3); + return zyx_data_available.read(); +} + +/*! + * @brief Reads x y z values at once + */ +void Adafruit_LIS3DH::read(void) { + + uint8_t register_address = LIS3DH_REG_OUT_X_L; + if (i2c_dev) { + register_address |= 0x80; // set [7] for auto-increment + } else { + register_address |= 0x40; // set [6] for auto-increment + register_address |= 0x80; // set [7] for read + } + + Adafruit_BusIO_Register xl_data = Adafruit_BusIO_Register( + i2c_dev, spi_dev, ADDRBIT8_HIGH_TOREAD, register_address, 6); + + uint8_t buffer[6]; + xl_data.read(buffer, 6); + + x = buffer[0]; + x |= ((uint16_t)buffer[1]) << 8; + y = buffer[2]; + y |= ((uint16_t)buffer[3]) << 8; + z = buffer[4]; + z |= ((uint16_t)buffer[5]) << 8; + + uint8_t range = getRange(); + + // this scaling process accounts for the shift due to actually being 10 bits + // (normal mode) as well as the lsb=> mg conversion and the mg=> g conversion + // final value is raw_lsb => 10-bit lsb -> milli-gs -> gs + + // regardless of the range, we'll always convert the value to 10 bits and g's + // so we'll always divide by LIS3DH_LSB16_TO_KILO_LSB10 (16000): + + // then we can then multiply the resulting value by the lsb value to get the + // value in g's + + uint8_t lsb_value = 1; + if (range == LIS3DH_RANGE_2_G) + lsb_value = 4; + if (range == LIS3DH_RANGE_4_G) + lsb_value = 8; + if (range == LIS3DH_RANGE_8_G) + lsb_value = 16; + if (range == LIS3DH_RANGE_16_G) + lsb_value = 48; + x_g = lsb_value * ((float)x / LIS3DH_LSB16_TO_KILO_LSB10); + y_g = lsb_value * ((float)y / LIS3DH_LSB16_TO_KILO_LSB10); + z_g = lsb_value * ((float)z / LIS3DH_LSB16_TO_KILO_LSB10); +} + +/*! + * @brief Read the auxilary ADC + * @param adc + * adc index. possible values (1, 2, 3). + * @return auxilary ADC value + */ +int16_t Adafruit_LIS3DH::readADC(uint8_t adc) { + if ((adc < 1) || (adc > 3)) + return 0; + adc--; // switch to 0 indexed + + uint16_t value; + uint8_t reg = LIS3DH_REG_OUTADC1_L + (adc * 2); + + if (i2c_dev) { + reg |= 0x80; // set [7] for auto-increment + } else { + reg |= 0x40; // set [6] for auto-increment + reg |= 0x80; // set [7] for read + } + + uint8_t buffer[2]; + Adafruit_BusIO_Register adc_data = + Adafruit_BusIO_Register(i2c_dev, spi_dev, ADDRBIT8_HIGH_TOREAD, reg, 2); + + adc_data.read(buffer, 2); + + value = buffer[0]; + value |= ((uint16_t)buffer[1]) << 8; + + return value; +} + +/*! + * @brief Set INT to output for single or double click + * @param c + * 0 = turn off I1_CLICK + * 1 = turn on all axes & singletap + * 2 = turn on all axes & doubletap + * @param clickthresh + * CLICK threshold value + * @param timelimit + * sets time limit (default 10) + * @param timelatency + * sets time latency (default 20) + * @param timewindow + * sets time window (default 255) + */ + +void Adafruit_LIS3DH::setClick(uint8_t c, uint8_t clickthresh, + uint8_t timelimit, uint8_t timelatency, + uint8_t timewindow) { + + Adafruit_BusIO_Register ctrl3 = Adafruit_BusIO_Register( + i2c_dev, spi_dev, ADDRBIT8_HIGH_TOREAD, LIS3DH_REG_CTRL3, 1); + Adafruit_BusIO_RegisterBits i1_click = + Adafruit_BusIO_RegisterBits(&ctrl3, 1, 7); + + Adafruit_BusIO_Register click_cfg = Adafruit_BusIO_Register( + i2c_dev, spi_dev, ADDRBIT8_HIGH_TOREAD, LIS3DH_REG_CLICKCFG, 1); + + if (!c) { + // disable int + i1_click.write(0); // disable i1 click + click_cfg.write(0); + return; + } + // else... + + i1_click.write(1); // enable i1 click + + Adafruit_BusIO_Register ctrl5 = Adafruit_BusIO_Register( + i2c_dev, spi_dev, ADDRBIT8_HIGH_TOREAD, LIS3DH_REG_CTRL5, 1); + + Adafruit_BusIO_RegisterBits int1_latch_bit = + Adafruit_BusIO_RegisterBits(&ctrl5, 1, 3); + int1_latch_bit.write(true); + + if (c == 1) + click_cfg.write(0x15); // turn on all axes & singletap + if (c == 2) + click_cfg.write(0x2A); // turn on all axes & doubletap + + Adafruit_BusIO_Register click_ths = Adafruit_BusIO_Register( + i2c_dev, spi_dev, ADDRBIT8_HIGH_TOREAD, LIS3DH_REG_CLICKTHS, 1); + click_ths.write(clickthresh); // arbitrary + + Adafruit_BusIO_Register time_limit = Adafruit_BusIO_Register( + i2c_dev, spi_dev, ADDRBIT8_HIGH_TOREAD, LIS3DH_REG_TIMELIMIT, 1); + time_limit.write(timelimit); // arbitrary + + Adafruit_BusIO_Register time_latency = Adafruit_BusIO_Register( + i2c_dev, spi_dev, ADDRBIT8_HIGH_TOREAD, LIS3DH_REG_TIMELATENCY, 1); + time_latency.write(timelatency); // arbitrary + + Adafruit_BusIO_Register time_window = Adafruit_BusIO_Register( + i2c_dev, spi_dev, ADDRBIT8_HIGH_TOREAD, LIS3DH_REG_TIMEWINDOW, 1); + time_window.write(timewindow); // arbitrary +} + +/*! + * @brief Get uint8_t for single or double click + * @return register LIS3DH_REG_CLICKSRC + */ +uint8_t Adafruit_LIS3DH::getClick(void) { + Adafruit_BusIO_Register click_reg = Adafruit_BusIO_Register( + i2c_dev, spi_dev, ADDRBIT8_HIGH_TOREAD, LIS3DH_REG_CLICKSRC, 1); + + return click_reg.read(); +} + +/*! + * @brief Get uint8_t for INT1 source and clear interrupt + * @return register LIS3DH_REG_INT1SRC + */ +uint8_t Adafruit_LIS3DH::readAndClearInterrupt(void) { + Adafruit_BusIO_Register int_reg = Adafruit_BusIO_Register( + i2c_dev, spi_dev, ADDRBIT8_HIGH_TOREAD, LIS3DH_REG_INT1SRC, 1); + + return int_reg.read(); +} + +/** + * @brief Enable or disable the Data Ready interupt + * + * @param enable_drdy true to enable the given Data Ready interrupt on INT1, + * false to disable it + * @param int_pin which DRDY interrupt to enable; 1 for DRDY1, 2 for DRDY2 + * @return true: success false: failure + */ +bool Adafruit_LIS3DH::enableDRDY(bool enable_drdy, uint8_t int_pin) { + Adafruit_BusIO_Register _ctrl3 = Adafruit_BusIO_Register( + i2c_dev, spi_dev, ADDRBIT8_HIGH_TOREAD, LIS3DH_REG_CTRL3, 1); + Adafruit_BusIO_RegisterBits _drdy1_int_enable = + Adafruit_BusIO_RegisterBits(&_ctrl3, 1, 4); + Adafruit_BusIO_RegisterBits _drdy2_int_enable = + Adafruit_BusIO_RegisterBits(&_ctrl3, 1, 3); + + if (int_pin == 1) { + return _drdy1_int_enable.write(enable_drdy); + } else if (int_pin == 2) { + return _drdy2_int_enable.write(enable_drdy); + } else { + return false; + } +} + +/*! + * @brief Sets the g range for the accelerometer + * @param range + * range value + */ +void Adafruit_LIS3DH::setRange(lis3dh_range_t range) { + + Adafruit_BusIO_Register _ctrl4 = Adafruit_BusIO_Register( + i2c_dev, spi_dev, ADDRBIT8_HIGH_TOREAD, LIS3DH_REG_CTRL4, 1); + + Adafruit_BusIO_RegisterBits range_bits = + Adafruit_BusIO_RegisterBits(&_ctrl4, 2, 4); + range_bits.write(range); + delay(15); // delay to let new setting settle +} + +/*! + * @brief Gets the g range for the accelerometer + * @return Returns g range value + */ +lis3dh_range_t Adafruit_LIS3DH::getRange(void) { + Adafruit_BusIO_Register _ctrl4 = Adafruit_BusIO_Register( + i2c_dev, spi_dev, ADDRBIT8_HIGH_TOREAD, LIS3DH_REG_CTRL4, 1); + + Adafruit_BusIO_RegisterBits range_bits = + Adafruit_BusIO_RegisterBits(&_ctrl4, 2, 4); + return (lis3dh_range_t)range_bits.read(); +} + +/*! + * @brief Sets the data rate for the LIS3DH (controls power consumption) + * @param dataRate + * data rate value + */ +void Adafruit_LIS3DH::setDataRate(lis3dh_dataRate_t dataRate) { + Adafruit_BusIO_Register _ctrl1 = Adafruit_BusIO_Register( + i2c_dev, spi_dev, ADDRBIT8_HIGH_TOREAD, LIS3DH_REG_CTRL1, 1); + Adafruit_BusIO_RegisterBits data_rate_bits = + Adafruit_BusIO_RegisterBits(&_ctrl1, 4, 4); + + data_rate_bits.write(dataRate); +} + +/*! + * @brief Gets the data rate for the LIS3DH (controls power consumption) + * @return Returns Data Rate value + */ +lis3dh_dataRate_t Adafruit_LIS3DH::getDataRate(void) { + Adafruit_BusIO_Register _ctrl1 = Adafruit_BusIO_Register( + i2c_dev, spi_dev, ADDRBIT8_HIGH_TOREAD, LIS3DH_REG_CTRL1, 1); + Adafruit_BusIO_RegisterBits data_rate_bits = + Adafruit_BusIO_RegisterBits(&_ctrl1, 4, 4); + + return (lis3dh_dataRate_t)data_rate_bits.read(); +} + +/*! + * @brief Gets the most recent sensor event + * @param *event + * sensor event that we want to read + * @return true if successful + */ +bool Adafruit_LIS3DH::getEvent(sensors_event_t *event) { + /* Clear the event */ + memset(event, 0, sizeof(sensors_event_t)); + + event->version = sizeof(sensors_event_t); + event->sensor_id = _sensorID; + event->type = SENSOR_TYPE_ACCELEROMETER; + event->timestamp = 0; + + read(); + + event->acceleration.x = x_g * SENSORS_GRAVITY_STANDARD; + event->acceleration.y = y_g * SENSORS_GRAVITY_STANDARD; + event->acceleration.z = z_g * SENSORS_GRAVITY_STANDARD; + + return true; +} + +/*! + * @brief Gets the sensor_t data + * @param *sensor + * sensor that we want to write data into + */ +void Adafruit_LIS3DH::getSensor(sensor_t *sensor) { + /* Clear the sensor_t object */ + memset(sensor, 0, sizeof(sensor_t)); + + /* Insert the sensor name in the fixed length char array */ + strncpy(sensor->name, "LIS3DH", sizeof(sensor->name) - 1); + sensor->name[sizeof(sensor->name) - 1] = 0; + sensor->version = 1; + sensor->sensor_id = _sensorID; + sensor->type = SENSOR_TYPE_ACCELEROMETER; + sensor->min_delay = 0; + sensor->max_value = 0; + sensor->min_value = 0; + sensor->resolution = 0; +} diff --git a/projects/MPU/include/Adafruit_LIS3DH-1.2.6/Adafruit_LIS3DH.h b/projects/MPU/include/Adafruit_LIS3DH-1.2.6/Adafruit_LIS3DH.h new file mode 100644 index 0000000..acd066e --- /dev/null +++ b/projects/MPU/include/Adafruit_LIS3DH-1.2.6/Adafruit_LIS3DH.h @@ -0,0 +1,404 @@ +/*! + * @file Adafruit_LIS3DH.h + * + * This is a library for the Adafruit LIS3DH Accel breakout board + * + * Designed specifically to work with the Adafruit LIS3DH Triple-Axis + *Accelerometer + * (+-2g/4g/8g/16g) + * + * Pick one up today in the adafruit shop! + * ------> https://www.adafruit.com/product/2809 + * + * This sensor communicates over I2C or SPI (our library code supports + *both) so you can share it with a bunch of other sensors on the same I2C bus. + * There's an address selection pin so you can have two accelerometers share an + *I2C bus. + * + * Adafruit invests time and resources providing this open source code, + * please support Adafruit andopen-source hardware by purchasing products + * from Adafruit! + * + * K. Townsend / Limor Fried (Ladyada) - (Adafruit Industries). + * + * BSD license, all text above must be included in any redistribution + */ + +#ifndef ADAFRUIT_LIS3DH_H +#define ADAFRUIT_LIS3DH_H + +#include "Arduino.h" + +#include +#include + +#include +#include +#include +#include + +/** I2C ADDRESS/BITS **/ +#define LIS3DH_DEFAULT_ADDRESS (0x18) // if SDO/SA0 is 3V, its 0x19 + +/*! + * STATUS_REG_AUX register + * 321OR 1, 2 and 3 axis data overrun. Default value: 0 + * (0: no overrun has occurred; 1: a new set of data has overwritten + * the previous ones) 3OR 3 axis data overrun. Default value: 0 (0: no + * overrun has occurred; 1: a new data for the 3-axis has overwritten the + * previous one) 2OR 2 axis data overrun. Default value: 0 (0: no overrun has + * occurred; 1: a new data for the 4-axis has overwritten the previous one) 1OR + * 1 axis data overrun. Default value: 0 (0: no overrun has occurred; 1: a new + * data for the 1-axis has overwritten the previous one) 321DA 1, 2 and 3 axis + * new data available. Default value: 0 (0: a new set of data is not yet + * available; 1: a new set of data is available) 3DA: 3 axis new data + * available. Default value: 0 (0: a new data for the 3-axis is not yet + * available; 1: a new data for the 3-axis is available) 2DA: 2 axis new data + * available. Default value: 0 (0: a new data for the 2-axis is not yet + * available; 1: a new data for the 2-axis is available) 1DA 1 axis new data + * available. Default value: 0 (0: a new data for the 1-axis is not yet + * available; 1: a new data for the 1-axis is available) + */ +#define LIS3DH_REG_STATUS1 0x07 +#define LIS3DH_REG_OUTADC1_L 0x08 /**< 1-axis acceleration data. Low value */ +#define LIS3DH_REG_OUTADC1_H 0x09 /**< 1-axis acceleration data. High value */ +#define LIS3DH_REG_OUTADC2_L 0x0A /**< 2-axis acceleration data. Low value */ +#define LIS3DH_REG_OUTADC2_H 0x0B /**< 2-axis acceleration data. High value */ +#define LIS3DH_REG_OUTADC3_L 0x0C /**< 3-axis acceleration data. Low value */ +#define LIS3DH_REG_OUTADC3_H 0x0D /**< 3-axis acceleration data. High value */ +#define LIS3DH_REG_INTCOUNT \ + 0x0E /**< INT_COUNTER register [IC7, IC6, IC5, IC4, IC3, IC2, IC1, IC0] */ +#define LIS3DH_REG_WHOAMI \ + 0x0F /**< Device identification register. [0, 0, 1, 1, 0, 0, 1, 1] */ +/*! + * TEMP_CFG_REG + * Temperature configuration register. + * ADC_PD ADC enable. Default value: 0 + * (0: ADC disabled; 1: ADC enabled) + * TEMP_EN Temperature sensor (T) enable. Default value: 0 + * (0: T disabled; 1: T enabled) + */ +#define LIS3DH_REG_TEMPCFG 0x1F +/*! + * CTRL_REG1 + * [ODR3, ODR2, ODR1, ODR0, LPen, Zen, Yen, Xen] + * ODR3-0 Data rate selection. Default value: 00 + * (0000:50 Hz; Others: Refer to Datasheet Table 26, “Data rate + * configuration”) LPen Low power mode enable. Default value: 0 (0: normal + * mode, 1: low power mode) Zen Z axis enable. Default value: 1 (0: Z axis + * disabled; 1: Z axis enabled) Yen Y axis enable. Default value: 1 (0: Y + * axis disabled; 1: Y axis enabled) Xen X axis enable. Default value: 1 (0: + * X axis disabled; 1: X axis enabled) + */ +#define LIS3DH_REG_CTRL1 0x20 +/*! + * CTRL_REG2 + * [HPM1, HPM0, HPCF2, HPCF1, FDS, HPCLICK, HPIS2, HPIS1] + * HPM1-0 High pass filter mode selection. Default value: 00 + * Refer to Table 29, "High pass filter mode configuration" + * HPCF2-1 High pass filter cut off frequency selection + * FDS Filtered data selection. Default value: 0 + * (0: internal filter bypassed; 1: data + *from internal filter sent to output register and FIFO) HPCLICK High pass + *filter enabled for CLICK function. (0: filter bypassed; 1: filter enabled) + *HPIS2 X axis enable. Default value: 1 (0: X axis disabled; 1: X axis + *enabled) HPIS1 High pass filter enabled for AOI function on interrupt 1, (0: + *filter bypassed; 1: filter enabled) + */ +#define LIS3DH_REG_CTRL2 0x21 +/*! + * CTRL_REG3 + * [I1_CLICK, I1_AOI1, I1_AOI2, I1_DRDY1, I1_DRDY2, I1_WTM, I1_OVERRUN, --] + * I1_CLICK CLICK interrupt on INT1. Default value 0. + * (0: Disable; 1: Enable) + * I1_AOI1 AOI1 interrupt on INT1. Default value 0. + * (0: Disable; 1: Enable) + * I1_AOI2 AOI2 interrupt on INT1. Default value 0. + * (0: Disable; 1: Enable) + * I1_DRDY1 DRDY1 interrupt on INT1. Default value 0. + * (0: Disable; 1: Enable) + * I1_DRDY2 DRDY2 interrupt on INT1. Default value 0. + * (0: Disable; 1: Enable) + * I1_WTM FIFO Watermark interrupt on INT1. Default value 0. + * (0: Disable; 1: Enable) + * I1_OVERRUN FIFO Overrun interrupt on INT1. Default value 0. + * (0: Disable; 1: Enable) + */ +#define LIS3DH_REG_CTRL3 0x22 +/*! + * CTRL_REG4 + * [BDU, BLE, FS1, FS0, HR, ST1, ST0, SIM] + * BDU Block data update. Default value: 0 + * (0: continuos update; 1: output registers not updated until MSB + * and LSB reading) BLE Big/little endian data selection. Default value 0. + * (0: Data LSB @ lower address; 1: Data MSB @ lower address) + * FS1-FS0 Full scale selection. default value: 00 + * (00: +/- 2G; 01: +/- 4G; 10: +/- 8G; 11: +/- 16G) + * HR High resolution output mode: Default value: 0 + * (0: High resolution disable; 1: High resolution Enable) + * ST1-ST0 Self test enable. Default value: 00 + * (00: Self test disabled; Other: See Table 34) + * SIM SPI serial interface mode selection. Default value: 0 + * (0: 4-wire interface; 1: 3-wire interface). + */ +#define LIS3DH_REG_CTRL4 0x23 +/*! + * CTRL_REG5 + * [BOOT, FIFO_EN, --, --, LIR_INT1, D4D_INT1, 0, 0] + * BOOT Reboot memory content. Default value: 0 + * (0: normal mode; 1: reboot memory content) + * FIFO_EN FIFO enable. Default value: 0 + * (0: FIFO disable; 1: FIFO Enable) + * LIR_INT1 Latch interrupt request on INT1_SRC register, with INT1_SRC + * register cleared by reading INT1_SRC itself. Default value: 0. (0: interrupt + * request not latched; 1: interrupt request latched) D4D_INT1 4D enable: 4D + * detection is enabled on INT1 when 6D bit on INT1_CFG is set to 1. + */ +#define LIS3DH_REG_CTRL5 0x24 + +/*! + * CTRL_REG6 + * [I2_CLICKen, I2_INT1, 0, BOOT_I1, 0, --, H_L, -] + */ +#define LIS3DH_REG_CTRL6 0x25 +#define LIS3DH_REG_REFERENCE 0x26 /**< REFERENCE/DATACAPTURE **/ +/*! + * STATUS_REG + * [ZYXOR, ZOR, YOR, XOR, ZYXDA, ZDA, YDA, XDA] + * ZYXOR X, Y and Z axis data overrun. Default value: 0 + * (0: no overrun has occurred; 1: a new set of data has overwritten + * the previous ones) ZOR Z axis data overrun. Default value: 0 (0: no + * overrun has occurred; 1: a new data for the Z-axis has overwritten the + * previous one) YOR Y axis data overrun. Default value: 0 (0: no overrun + * has occurred; 1: a new data for the Y-axis has overwritten the previous one) + * XOR X axis data overrun. Default value: 0 + * (0: no overrun has occurred; 1: a new data for the X-axis has + * overwritten the previous one) ZYXDA X, Y and Z axis new data available. + * Default value: 0 (0: a new set of data is not yet available; 1: a new set of + * data is available) ZDA Z axis new data available. Default value: 0 (0: a + * new data for the Z-axis is not yet available; 1: a new data for the Z-axis is + * available) YDA Y axis new data available. Default value: 0 (0: a new + * data for the Y-axis is not yet available; 1: a new data for the Y-axis is + * available) + */ +#define LIS3DH_REG_STATUS2 0x27 +#define LIS3DH_REG_OUT_X_L 0x28 /**< X-axis acceleration data. Low value */ +#define LIS3DH_REG_OUT_X_H 0x29 /**< X-axis acceleration data. High value */ +#define LIS3DH_REG_OUT_Y_L 0x2A /**< Y-axis acceleration data. Low value */ +#define LIS3DH_REG_OUT_Y_H 0x2B /**< Y-axis acceleration data. High value */ +#define LIS3DH_REG_OUT_Z_L 0x2C /**< Z-axis acceleration data. Low value */ +#define LIS3DH_REG_OUT_Z_H 0x2D /**< Z-axis acceleration data. High value */ +/*! + * FIFO_CTRL_REG + * [FM1, FM0, TR, FTH4, FTH3, FTH2, FTH1, FTH0] + * FM1-FM0 FIFO mode selection. Default value: 00 (see Table 44) + * TR Trigger selection. Default value: 0 + * 0: Trigger event liked to trigger signal on INT1 + * 1: Trigger event liked to trigger signal on INT2 + * FTH4:0 Default value: 0 + */ +#define LIS3DH_REG_FIFOCTRL 0x2E +#define LIS3DH_REG_FIFOSRC \ + 0x2F /**< FIFO_SRC_REG [WTM, OVRN_FIFO, EMPTY, FSS4, FSS3, FSS2, FSS1, FSS0] \ + */ +/*! + * INT1_CFG + * [AOI, 6D, ZHIE/ZUPE, ZLIE/ZDOWNE, YHIE/YUPE, XHIE/XUPE, XLIE/XDOWNE] + * AOI And/Or combination of Interrupt events. Default value: 0. Refer + * to Datasheet Table 48, "Interrupt mode" 6D 6 direction detection + * function enabled. Default value: 0. Refer to Datasheet Table 48, "Interrupt + * mode" ZHIE/ZUPE Enable interrupt generation on Z high event or on Direction + * recognition. Default value: 0. (0: disable interrupt request; 1: enable + * interrupt request) ZLIE/ZDOWNE Enable interrupt generation on Z low event or + * on Direction recognition. Default value: 0. YHIE/YUPE Enable interrupt + * generation on Y high event or on Direction recognition. Default value: 0. (0: + * disable interrupt request; 1: enable interrupt request.) YLIE/YDOWNE Enable + * interrupt generation on Y low event or on Direction recognition. Default + * value: 0. (0: disable interrupt request; 1: enable interrupt request.) + * XHIE/XUPE Enable interrupt generation on X high event or on Direction + * recognition. Default value: 0. (0: disable interrupt request; 1: enable + * interrupt request.) XLIE/XDOWNE Enable interrupt generation on X low event or + * on Direction recognition. Default value: 0. (0: disable interrupt request; 1: + * enable interrupt request.) + */ +#define LIS3DH_REG_INT1CFG 0x30 +/*! + * INT1_SRC + * [0, IA, ZH, ZL, YH, YL, XH, XL] + * IA Interrupt active. Default value: 0 + * (0: no interrupt has been generated; 1: one or more interrupts have + * been generated) ZH Z high. Default value: 0 (0: no interrupt, 1: Z High + * event has occurred) ZL Z low. Default value: 0 (0: no interrupt; 1: Z Low + * event has occurred) YH Y high. Default value: 0 (0: no interrupt, 1: Y High + * event has occurred) YL Y low. Default value: 0 (0: no interrupt, 1: Y Low + * event has occurred) XH X high. Default value: 0 (0: no interrupt, 1: X High + * event has occurred) XL X low. Default value: 0 (0: no interrupt, 1: X Low + * event has occurred) + * + * Interrupt 1 source register. Read only register. + * Reading at this address clears INT1_SRC IA bit (and the interrupt signal + * on INT 1 pin) and allows the refreshment of data in the INT1_SRC register if + * the latched option was chosen. + */ +#define LIS3DH_REG_INT1SRC 0x31 +#define LIS3DH_REG_INT1THS \ + 0x32 /**< INT1_THS register [0, THS6, THS5, THS4, THS3, THS1, THS0] */ +#define LIS3DH_REG_INT1DUR \ + 0x33 /**< INT1_DURATION [0, D6, D5, D4, D3, D2, D1, D0] */ +/*! + * CLICK_CFG + * [--, --, ZD, ZS, YD, YS, XD, XS] + * ZD Enable interrupt double CLICK-CLICK on Z axis. Default value: 0 + * (0: disable interrupt request; + * 1: enable interrupt request on measured accel. value higher than + * preset threshold) ZS Enable interrupt single CLICK-CLICK on Z axis. Default + * value: 0 (0: disable interrupt request; 1: enable interrupt request on + * measured accel. value higher than preset threshold) YD Enable interrupt + * double CLICK-CLICK on Y axis. Default value: 0 (0: disable interrupt request; + * 1: enable interrupt request on measured accel. value higher than + * preset threshold) YS Enable interrupt single CLICK-CLICK on Y axis. Default + * value: 0 (0: disable interrupt request; 1: enable interrupt request on + * measured accel. value higher than preset threshold) XD Enable interrupt + * double CLICK-CLICK on X axis. Default value: 0 (0: disable interrupt request; + * 1: enable interrupt request on measured accel. value higher than preset + * threshold) XS Enable interrupt single CLICK-CLICK on X axis. Default value: + * 0 (0: disable interrupt request; 1: enable interrupt request on measured + * accel. value higher than preset threshold) + */ +#define LIS3DH_REG_CLICKCFG 0x38 +/*! + * CLICK_SRC + * [-, IA, DCLICK, SCLICK, Sign, Z, Y, X] + * IA Interrupt active. Default value: 0 + * (0: no interrupt has been generated; 1: one or more interrupts have + * been generated) DCLICK Double CLICK-CLICK enable. Default value: 0 (0:double + * CLICK-CLICK detection disable, 1: double CLICK-CLICK detection enable) SCLICK + * Single CLICK-CLICK enable. Default value: 0 (0:Single CLICK-CLICK detection + * disable, 1: single CLICK-CLICK detection enable) Sign CLICK-CLICK Sign. + * (0: positive detection, 1: negative detection) + * Z Z CLICK-CLICK detection. Default value: 0 + * (0: no interrupt, 1: Z High event has occurred) + * Y Y CLICK-CLICK detection. Default value: 0 + * (0: no interrupt, 1: Y High event has occurred) + * X X CLICK-CLICK detection. Default value: 0 + * (0: no interrupt, 1: X High event has occurred) + */ +#define LIS3DH_REG_CLICKSRC 0x39 +/*! + * CLICK_THS + * [-, Ths6, Ths5, Ths4, Ths3, Ths2, Ths1, Ths0] + * Ths6-Ths0 CLICK-CLICK threshold. Default value: 000 0000 + */ +#define LIS3DH_REG_CLICKTHS 0x3A +/*! + * TIME_LIMIT + * [-, TLI6, TLI5, TLI4, TLI3, TLI2, TLI1, TLI0] + * TLI7-TLI0 CLICK-CLICK Time Limit. Default value: 000 0000 + */ +#define LIS3DH_REG_TIMELIMIT 0x3B +/*! + * TIME_LATANCY + * [-, TLA6, TLIA5, TLA4, TLA3, TLA2, TLA1, TLA0] + * TLA7-TLA0 CLICK-CLICK Time Latency. Default value: 000 0000 + */ +#define LIS3DH_REG_TIMELATENCY 0x3C +/*! + * TIME_WINDOW + * [TW7, TW6, TW5, TW4, TW3, TW2, TW1, TW0] + * TW7-TW0 CLICK-CLICK Time window + */ +#define LIS3DH_REG_TIMEWINDOW 0x3D + +#define LIS3DH_LSB16_TO_KILO_LSB10 \ + 64000 ///< Scalar to convert from 16-bit lsb to 10-bit and divide by 1k to + ///< convert from milli-gs to gs + +/** A structure to represent scales **/ +typedef enum { + LIS3DH_RANGE_16_G = 0b11, // +/- 16g + LIS3DH_RANGE_8_G = 0b10, // +/- 8g + LIS3DH_RANGE_4_G = 0b01, // +/- 4g + LIS3DH_RANGE_2_G = 0b00 // +/- 2g (default value) +} lis3dh_range_t; + +/** A structure to represent axes **/ +typedef enum { + LIS3DH_AXIS_X = 0x0, + LIS3DH_AXIS_Y = 0x1, + LIS3DH_AXIS_Z = 0x2, +} lis3dh_axis_t; + +/** Used with register 0x2A (LIS3DH_REG_CTRL_REG1) to set bandwidth **/ +typedef enum { + LIS3DH_DATARATE_400_HZ = 0b0111, // 400Hz + LIS3DH_DATARATE_200_HZ = 0b0110, // 200Hz + LIS3DH_DATARATE_100_HZ = 0b0101, // 100Hz + LIS3DH_DATARATE_50_HZ = 0b0100, // 50Hz + LIS3DH_DATARATE_25_HZ = 0b0011, // 25Hz + LIS3DH_DATARATE_10_HZ = 0b0010, // 10 Hz + LIS3DH_DATARATE_1_HZ = 0b0001, // 1 Hz + LIS3DH_DATARATE_POWERDOWN = 0, + LIS3DH_DATARATE_LOWPOWER_1K6HZ = 0b1000, + LIS3DH_DATARATE_LOWPOWER_5KHZ = 0b1001, + +} lis3dh_dataRate_t; + +/*! + * @brief Class that stores state and functions for interacting with + * Adafruit_LIS3DH + */ +class Adafruit_LIS3DH : public Adafruit_Sensor { +public: + Adafruit_LIS3DH(TwoWire *Wi = &Wire); + Adafruit_LIS3DH(int8_t cspin, SPIClass *theSPI = &SPI); + Adafruit_LIS3DH(int8_t cspin, int8_t mosipin, int8_t misopin, int8_t sckpin); + + bool begin(uint8_t addr = LIS3DH_DEFAULT_ADDRESS, uint8_t nWAI = 0x33); + + uint8_t getDeviceID(void); + bool haveNewData(void); + bool enableDRDY(bool enable_drdy = true, uint8_t int_pin = 1); + + void read(void); + int16_t readADC(uint8_t a); + + void setRange(lis3dh_range_t range); + lis3dh_range_t getRange(void); + + void setDataRate(lis3dh_dataRate_t dataRate); + lis3dh_dataRate_t getDataRate(void); + + bool getEvent(sensors_event_t *event); + void getSensor(sensor_t *sensor); + + void setClick(uint8_t c, uint8_t clickthresh, uint8_t timelimit = 10, + uint8_t timelatency = 20, uint8_t timewindow = 255); + uint8_t getClick(void); + + uint8_t readAndClearInterrupt(void); + + int16_t x; /**< x axis value */ + int16_t y; /**< y axis value */ + int16_t z; /**< z axis value */ + + float x_g; /**< x_g axis value (calculated by selected range) */ + float y_g; /**< y_g axis value (calculated by selected range) */ + float z_g; /**< z_g axis value (calculated by selected range) */ + +private: + TwoWire *I2Cinterface; + SPIClass *SPIinterface; + + Adafruit_I2CDevice *i2c_dev = NULL; ///< Pointer to I2C bus interface + Adafruit_SPIDevice *spi_dev = NULL; ///< Pointer to SPI bus interface + + uint8_t _wai; + + int8_t _cs, _mosi, _miso, _sck; + + int8_t _i2caddr; + + int32_t _sensorID; +}; + +#endif diff --git a/projects/MPU/include/Adafruit_LIS3DH-1.2.6/README.md b/projects/MPU/include/Adafruit_LIS3DH-1.2.6/README.md new file mode 100644 index 0000000..dd1fc40 --- /dev/null +++ b/projects/MPU/include/Adafruit_LIS3DH-1.2.6/README.md @@ -0,0 +1,17 @@ +# Adafruit LIS3DH [![Build Status](https://github.com/adafruit/Adafruit_LIS3DH/workflows/Arduino%20Library%20CI/badge.svg)](https://github.com/adafruit/Adafruit_LIS3DH/actions)[![Documentation](https://github.com/adafruit/ci-arduino/blob/master/assets/doxygen_badge.svg)](http://adafruit.github.io/Adafruit_LIS3DH/html/index.html) + + + +This is the Adafruit LIS3DH breakout board library. +* https://www.adafruit.com/products/2809 + +This sensor communicates over I2C or SPI (our library code supports both) so you can share it with a bunch of other sensors on the same I2C bus. +There's an address selection pin so you can have two accelerometers share an I2C bus. + +Adafruit invests time and resources providing this open source code, please support Adafruit and open-source hardware by purchasing products from Adafruit! + +Written by Kevin Townsend/Limor Fried for Adafruit Industries. +BSD license, check license.txt for more information +All text above must be included in any redistribution + +To install, use the Arduino Library Manager and search for "Adafruit LIS3DH" and install the library. diff --git a/projects/MPU/include/Adafruit_LIS3DH-1.2.6/assets/image.jpg b/projects/MPU/include/Adafruit_LIS3DH-1.2.6/assets/image.jpg new file mode 100644 index 0000000..39b87ff Binary files /dev/null and b/projects/MPU/include/Adafruit_LIS3DH-1.2.6/assets/image.jpg differ diff --git a/projects/MPU/include/Adafruit_LIS3DH-1.2.6/code-of-conduct.md b/projects/MPU/include/Adafruit_LIS3DH-1.2.6/code-of-conduct.md new file mode 100644 index 0000000..8ee6e44 --- /dev/null +++ b/projects/MPU/include/Adafruit_LIS3DH-1.2.6/code-of-conduct.md @@ -0,0 +1,127 @@ +# Adafruit Community Code of Conduct + +## Our Pledge + +In the interest of fostering an open and welcoming environment, we as +contributors and leaders pledge to making participation in our project and +our community a harassment-free experience for everyone, regardless of age, body +size, disability, ethnicity, gender identity and expression, level or type of +experience, education, socio-economic status, nationality, personal appearance, +race, religion, or sexual identity and orientation. + +## Our Standards + +We are committed to providing a friendly, safe and welcoming environment for +all. + +Examples of behavior that contributes to creating a positive environment +include: + +* Be kind and courteous to others +* Using welcoming and inclusive language +* Being respectful of differing viewpoints and experiences +* Collaborating with other community members +* Gracefully accepting constructive criticism +* Focusing on what is best for the community +* Showing empathy towards other community members + +Examples of unacceptable behavior by participants include: + +* The use of sexualized language or imagery and sexual attention or advances +* The use of inappropriate images, including in a community member's avatar +* The use of inappropriate language, including in a community member's nickname +* Any spamming, flaming, baiting or other attention-stealing behavior +* Excessive or unwelcome helping; answering outside the scope of the question + asked +* Trolling, insulting/derogatory comments, and personal or political attacks +* Public or private harassment +* Publishing others' private information, such as a physical or electronic + address, without explicit permission +* Other conduct which could reasonably be considered inappropriate + +The goal of the standards and moderation guidelines outlined here is to build +and maintain a respectful community. We ask that you don’t just aim to be +"technically unimpeachable", but rather try to be your best self. + +We value many things beyond technical expertise, including collaboration and +supporting others within our community. Providing a positive experience for +other community members can have a much more significant impact than simply +providing the correct answer. + +## Our Responsibilities + +Project leaders are responsible for clarifying the standards of acceptable +behavior and are expected to take appropriate and fair corrective action in +response to any instances of unacceptable behavior. + +Project leaders have the right and responsibility to remove, edit, or +reject messages, comments, commits, code, issues, and other contributions +that are not aligned to this Code of Conduct, or to ban temporarily or +permanently any community member for other behaviors that they deem +inappropriate, threatening, offensive, or harmful. + +## Moderation + +Instances of behaviors that violate the Adafruit Community Code of Conduct +may be reported by any member of the community. Community members are +encouraged to report these situations, including situations they witness +involving other community members. + +You may report in the following ways: + +In any situation, you may send an email to . + +On the Adafruit Discord, you may send an open message from any channel +to all Community Helpers by tagging @community helpers. You may also send an +open message from any channel, or a direct message to @kattni#1507, +@tannewt#4653, @Dan Halbert#1614, @cater#2442, @sommersoft#0222, or +@Andon#8175. + +Email and direct message reports will be kept confidential. + +In situations on Discord where the issue is particularly egregious, possibly +illegal, requires immediate action, or violates the Discord terms of service, +you should also report the message directly to Discord. + +These are the steps for upholding our community’s standards of conduct. + +1. Any member of the community may report any situation that violates the +Adafruit Community Code of Conduct. All reports will be reviewed and +investigated. +2. If the behavior is an egregious violation, the community member who +committed the violation may be banned immediately, without warning. +3. Otherwise, moderators will first respond to such behavior with a warning. +4. Moderators follow a soft "three strikes" policy - the community member may +be given another chance, if they are receptive to the warning and change their +behavior. +5. If the community member is unreceptive or unreasonable when warned by a +moderator, or the warning goes unheeded, they may be banned for a first or +second offense. Repeated offenses will result in the community member being +banned. + +## Scope + +This Code of Conduct and the enforcement policies listed above apply to all +Adafruit Community venues. This includes but is not limited to any community +spaces (both public and private), the entire Adafruit Discord server, and +Adafruit GitHub repositories. Examples of Adafruit Community spaces include +but are not limited to meet-ups, audio chats on the Adafruit Discord, or +interaction at a conference. + +This Code of Conduct applies both within project spaces and in public spaces +when an individual is representing the project or its community. As a community +member, you are representing our community, and are expected to behave +accordingly. + +## Attribution + +This Code of Conduct is adapted from the [Contributor Covenant][homepage], +version 1.4, available at +, +and the [Rust Code of Conduct](https://www.rust-lang.org/en-US/conduct.html). + +For other projects adopting the Adafruit Community Code of +Conduct, please contact the maintainers of those projects for enforcement. +If you wish to use this code of conduct for your own project, consider +explicitly mentioning your moderation policy or making a copy with your +own moderation policy so as to avoid confusion. diff --git a/projects/MPU/include/Adafruit_LIS3DH-1.2.6/examples/acceldemo/acceldemo.ino b/projects/MPU/include/Adafruit_LIS3DH-1.2.6/examples/acceldemo/acceldemo.ino new file mode 100644 index 0000000..8906ead --- /dev/null +++ b/projects/MPU/include/Adafruit_LIS3DH-1.2.6/examples/acceldemo/acceldemo.ino @@ -0,0 +1,77 @@ + +// Basic demo for accelerometer readings from Adafruit LIS3DH + +#include +#include +#include +#include + +// Used for software SPI +#define LIS3DH_CLK 13 +#define LIS3DH_MISO 12 +#define LIS3DH_MOSI 11 +// Used for hardware & software SPI +#define LIS3DH_CS 10 + +// software SPI +//Adafruit_LIS3DH lis = Adafruit_LIS3DH(LIS3DH_CS, LIS3DH_MOSI, LIS3DH_MISO, LIS3DH_CLK); +// hardware SPI +//Adafruit_LIS3DH lis = Adafruit_LIS3DH(LIS3DH_CS); +// I2C +Adafruit_LIS3DH lis = Adafruit_LIS3DH(); + +void setup(void) { + Serial.begin(115200); + while (!Serial) delay(10); // will pause Zero, Leonardo, etc until serial console opens + + Serial.println("LIS3DH test!"); + + if (! lis.begin(0x18)) { // change this to 0x19 for alternative i2c address + Serial.println("Couldnt start"); + while (1) yield(); + } + Serial.println("LIS3DH found!"); + + // lis.setRange(LIS3DH_RANGE_4_G); // 2, 4, 8 or 16 G! + + Serial.print("Range = "); Serial.print(2 << lis.getRange()); + Serial.println("G"); + + // lis.setDataRate(LIS3DH_DATARATE_50_HZ); + Serial.print("Data rate set to: "); + switch (lis.getDataRate()) { + case LIS3DH_DATARATE_1_HZ: Serial.println("1 Hz"); break; + case LIS3DH_DATARATE_10_HZ: Serial.println("10 Hz"); break; + case LIS3DH_DATARATE_25_HZ: Serial.println("25 Hz"); break; + case LIS3DH_DATARATE_50_HZ: Serial.println("50 Hz"); break; + case LIS3DH_DATARATE_100_HZ: Serial.println("100 Hz"); break; + case LIS3DH_DATARATE_200_HZ: Serial.println("200 Hz"); break; + case LIS3DH_DATARATE_400_HZ: Serial.println("400 Hz"); break; + + case LIS3DH_DATARATE_POWERDOWN: Serial.println("Powered Down"); break; + case LIS3DH_DATARATE_LOWPOWER_5KHZ: Serial.println("5 Khz Low Power"); break; + case LIS3DH_DATARATE_LOWPOWER_1K6HZ: Serial.println("16 Khz Low Power"); break; + } +} + +void loop() { + lis.read(); // get X Y and Z data at once + // Then print out the raw data + Serial.print("X: "); Serial.print(lis.x); + Serial.print(" \tY: "); Serial.print(lis.y); + Serial.print(" \tZ: "); Serial.print(lis.z); + + /* Or....get a new sensor event, normalized */ + sensors_event_t event; + lis.getEvent(&event); + + /* Display the results (acceleration is measured in m/s^2) */ + Serial.print("\t\tX: "); Serial.print(event.acceleration.x); + Serial.print(" \tY: "); Serial.print(event.acceleration.y); + Serial.print(" \tZ: "); Serial.print(event.acceleration.z); + Serial.println(" m/s^2 "); + + Serial.println(); + + delay(200); +} diff --git a/projects/MPU/include/Adafruit_LIS3DH-1.2.6/examples/adcdemo/adcdemo.ino b/projects/MPU/include/Adafruit_LIS3DH-1.2.6/examples/adcdemo/adcdemo.ino new file mode 100644 index 0000000..700f878 --- /dev/null +++ b/projects/MPU/include/Adafruit_LIS3DH-1.2.6/examples/adcdemo/adcdemo.ino @@ -0,0 +1,65 @@ +// Basic demo for tap/doubletap readings from Adafruit LIS3DH + +#include +#include +#include +#include + +// Used for software SPI +#define LIS3DH_CLK 13 +#define LIS3DH_MISO 12 +#define LIS3DH_MOSI 11 +// Used for hardware & software SPI +#define LIS3DH_CS 10 + +// software SPI +//Adafruit_LIS3DH lis = Adafruit_LIS3DH(LIS3DH_CS, LIS3DH_MOSI, LIS3DH_MISO, LIS3DH_CLK); +// hardware SPI +//Adafruit_LIS3DH lis = Adafruit_LIS3DH(LIS3DH_CS); +// I2C +Adafruit_LIS3DH lis = Adafruit_LIS3DH(); + +void setup(void) { +#ifndef ESP8266 + while (!Serial) yield(); // will pause Zero, Leonardo, etc until serial console opens +#endif + + Serial.begin(9600); + Serial.println("Adafruit LIS3DH ADC Test!"); + + if (! lis.begin(0x18)) { // change this to 0x19 for alternative i2c address + Serial.println("Couldnt start"); + while (1) yield(); + } + Serial.println("LIS3DH found!"); + + lis.setRange(LIS3DH_RANGE_2_G); // 2, 4, 8 or 16 G! + + Serial.print("Range = "); Serial.print(2 << lis.getRange()); + Serial.println("G"); +} + + +void loop() { + int16_t adc; + uint16_t volt; + + // read the ADCs + adc = lis.readADC(1); + volt = map(adc, -32512, 32512, 1800, 900); + Serial.print("ADC1:\t"); Serial.print(adc); + Serial.print(" ("); Serial.print(volt); Serial.print(" mV) "); + + adc = lis.readADC(2); + volt = map(adc, -32512, 32512, 1800, 900); + Serial.print("ADC2:\t"); Serial.print(adc); + Serial.print(" ("); Serial.print(volt); Serial.print(" mV) "); + + adc = lis.readADC(3); + volt = map(adc, -32512, 32512, 1800, 900); + Serial.print("ADC3:\t"); Serial.print(adc); + Serial.print(" ("); Serial.print(volt); Serial.print(" mV)"); + + Serial.println(); + delay(200); +} diff --git a/projects/MPU/include/Adafruit_LIS3DH-1.2.6/examples/tapdemo/tapdemo.ino b/projects/MPU/include/Adafruit_LIS3DH-1.2.6/examples/tapdemo/tapdemo.ino new file mode 100644 index 0000000..d3da489 --- /dev/null +++ b/projects/MPU/include/Adafruit_LIS3DH-1.2.6/examples/tapdemo/tapdemo.ino @@ -0,0 +1,66 @@ +// Basic demo for tap/doubletap readings from Adafruit LIS3DH + +#include +#include +#include +#include + +// Used for software SPI +#define LIS3DH_CLK 13 +#define LIS3DH_MISO 12 +#define LIS3DH_MOSI 11 +// Used for hardware & software SPI +#define LIS3DH_CS 10 + +// software SPI +//Adafruit_LIS3DH lis = Adafruit_LIS3DH(LIS3DH_CS, LIS3DH_MOSI, LIS3DH_MISO, LIS3DH_CLK); +// hardware SPI +//Adafruit_LIS3DH lis = Adafruit_LIS3DH(LIS3DH_CS); +// I2C +Adafruit_LIS3DH lis = Adafruit_LIS3DH(); + +// Adjust this number for the sensitivity of the 'click' force +// this strongly depend on the range! for 16G, try 5-10 +// for 8G, try 10-20. for 4G try 20-40. for 2G try 40-80 +#define CLICKTHRESHHOLD 80 + +void setup(void) { +#ifndef ESP8266 + while (!Serial) yield(); // will pause Zero, Leonardo, etc until serial console opens +#endif + + Serial.begin(9600); + Serial.println("Adafruit LIS3DH Tap Test!"); + + if (! lis.begin(0x18)) { // change this to 0x19 for alternative i2c address + Serial.println("Couldnt start"); + while (1) yield(); + } + Serial.println("LIS3DH found!"); + + lis.setRange(LIS3DH_RANGE_2_G); // 2, 4, 8 or 16 G! + + Serial.print("Range = "); Serial.print(2 << lis.getRange()); + Serial.println("G"); + + // 0 = turn off click detection & interrupt + // 1 = single click only interrupt output + // 2 = double click only interrupt output, detect single click + // Adjust threshhold, higher numbers are less sensitive + lis.setClick(2, CLICKTHRESHHOLD); + delay(100); +} + + +void loop() { + uint8_t click = lis.getClick(); + if (click == 0) return; + if (! (click & 0x30)) return; + Serial.print("Click detected (0x"); Serial.print(click, HEX); Serial.print("): "); + if (click & 0x10) Serial.print(" single click"); + if (click & 0x20) Serial.print(" double click"); + Serial.println(); + + delay(100); + return; +} diff --git a/projects/MPU/include/Adafruit_LIS3DH-1.2.6/library.properties b/projects/MPU/include/Adafruit_LIS3DH-1.2.6/library.properties new file mode 100644 index 0000000..f1e6539 --- /dev/null +++ b/projects/MPU/include/Adafruit_LIS3DH-1.2.6/library.properties @@ -0,0 +1,10 @@ +name=Adafruit LIS3DH +version=1.2.6 +author=Adafruit +maintainer=Adafruit +sentence=Library for the Adafruit LIS3DH Accelerometer. +paragraph=Designed specifically to work with the Adafruit LIS3DH Breakout, and is based on Adafruit's Unified Sensor Library. +category=Sensors +url=https://github.com/adafruit/Adafruit_LIS3DH +architectures=* +depends=Adafruit Unified Sensor, Adafruit BusIO diff --git a/projects/MPU/include/Adafruit_LIS3DH-1.2.6/license.txt b/projects/MPU/include/Adafruit_LIS3DH-1.2.6/license.txt new file mode 100644 index 0000000..f6a0f22 --- /dev/null +++ b/projects/MPU/include/Adafruit_LIS3DH-1.2.6/license.txt @@ -0,0 +1,26 @@ +Software License Agreement (BSD License) + +Copyright (c) 2012, Adafruit Industries +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: +1. Redistributions of source code must retain the above copyright +notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +3. Neither the name of the copyright holders nor the +names of its contributors may be used to endorse or promote products +derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY +EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY +DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/projects/MPU/include/README b/projects/MPU/include/README new file mode 100644 index 0000000..194dcd4 --- /dev/null +++ b/projects/MPU/include/README @@ -0,0 +1,39 @@ + +This directory is intended for project header files. + +A header file is a file containing C declarations and macro definitions +to be shared between several project source files. You request the use of a +header file in your project source file (C, C++, etc) located in `src` folder +by including it, with the C preprocessing directive `#include'. + +```src/main.c + +#include "header.h" + +int main (void) +{ + ... +} +``` + +Including a header file produces the same results as copying the header file +into each source file that needs it. Such copying would be time-consuming +and error-prone. With a header file, the related declarations appear +in only one place. If they need to be changed, they can be changed in one +place, and programs that include the header file will automatically use the +new version when next recompiled. The header file eliminates the labor of +finding and changing all the copies as well as the risk that a failure to +find one copy will result in inconsistencies within a program. + +In C, the usual convention is to give header files names that end with `.h'. +It is most portable to use only letters, digits, dashes, and underscores in +header file names, and at most one dot. + +Read more about using header files in official GCC documentation: + +* Include Syntax +* Include Operation +* Once-Only Headers +* Computed Includes + +https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html diff --git a/projects/MPU/lib/README b/projects/MPU/lib/README new file mode 100644 index 0000000..6debab1 --- /dev/null +++ b/projects/MPU/lib/README @@ -0,0 +1,46 @@ + +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 +#include + +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 diff --git a/projects/MPU/platformio.ini b/projects/MPU/platformio.ini new file mode 100644 index 0000000..608447e --- /dev/null +++ b/projects/MPU/platformio.ini @@ -0,0 +1,6 @@ +[env:lolin32] +platform = espressif32 +board = lolin32 +framework = arduino +monitor_speed = 9600 +lib_deps = MPU6050 diff --git a/projects/MPU/src/main.cpp b/projects/MPU/src/main.cpp new file mode 100644 index 0000000..89d95ef --- /dev/null +++ b/projects/MPU/src/main.cpp @@ -0,0 +1,51 @@ +#include +#include +#include // Dodane dla funkcji sqrt i atan2 + +MPU6050 mpu; + +void setup() { + Serial.begin(9600); + Wire.begin(23, 19); + + mpu.initialize(); + + Serial.println("Testing MPU6050 connections..."); + Serial.println(mpu.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed"); +} + +void loop() { + int16_t ax, ay, az; + int16_t gx, gy, gz; + + mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz); + + // Display Accelerometer data + Serial.print("Akcelerometr (in m/s^2): "); + Serial.print("X = "); Serial.print((float)ax / 16384.0 * 9.81); // Convert raw data to m/s^2 (1g = 9.81 m/s^2) + Serial.print(", Y = "); Serial.print((float)ay / 16384.0 * 9.81); + Serial.print(", Z = "); Serial.println((float)az / 16384.0 * 9.81); + + // Display Gyroscope data + Serial.print("Zyroskop (in degrees per second): "); + Serial.print("X = "); Serial.print((float)gx / 131.0); // Convert raw data to degrees per second (sensitivity scale factor) + Serial.print(", Y = "); Serial.print((float)gy / 131.0); + Serial.print(", Z = "); Serial.println((float)gz / 131.0); + + // Calculate linear acceleration in m/s^2 + float acceleration = sqrt(pow((float)ax / 16384.0 * 9.81, 2) + pow((float)ay / 16384.0 * 9.81, 2) + pow((float)az / 16384.0 * 9.81, 2)); + Serial.print("Przyspieszenie liniowe (in m/s^2): "); + Serial.println(acceleration); + + // Calculate angular velocity (speed) in radians/s + float angular_speed = sqrt(pow((float)gx / 131.0, 2) + pow((float)gy / 131.0, 2) + pow((float)gz / 131.0, 2)); + Serial.print("Predkosc katowa (in radians per second): "); + Serial.println(angular_speed); + + // Calculate rotation angle in radians + float rotation_angle = atan2((float)ay / 16384.0, (float)ax / 16384.0); + Serial.print("Kat obrotu (in radians): "); + Serial.println(rotation_angle); + + delay(5000); +} diff --git a/projects/MPU/test.txt b/projects/MPU/test.txt new file mode 100644 index 0000000..1bd28dc --- /dev/null +++ b/projects/MPU/test.txt @@ -0,0 +1 @@ +coś diff --git a/projects/MPU/test/README b/projects/MPU/test/README new file mode 100644 index 0000000..9b1e87b --- /dev/null +++ b/projects/MPU/test/README @@ -0,0 +1,11 @@ + +This directory is intended for PlatformIO Test Runner and project tests. + +Unit Testing is a software testing method by which individual units of +source code, sets of one or more MCU program modules together with associated +control data, usage procedures, and operating procedures, are tested to +determine whether they are fit for use. Unit testing finds problems early +in the development cycle. + +More information about PlatformIO Unit Testing: +- https://docs.platformio.org/en/latest/advanced/unit-testing/index.html