Skip to content

Commit

Permalink
Example for the LIS3MDL driver on the Nucleo F042K6
Browse files Browse the repository at this point in the history
  • Loading branch information
nesos committed Jul 7, 2020
1 parent 483eba8 commit 0994a55
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
73 changes: 73 additions & 0 deletions examples/nucleo_f042k6/lis3mdl/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright (c) 2020, Benjamin Carrick
*
* This file is part of the modm project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

#include <modm/board.hpp>
#include <modm/driver/inertial/lis3mdl.hpp>

using namespace Board;
using namespace std::chrono_literals;

using I2cSda = GpioA10;
using I2cScl = GpioA9;

int
main()
{
Board::initialize();
LedD13::setOutput();

MODM_LOG_INFO << "LIS3MDL demo" << modm::endl;

I2cMaster1::connect<I2cSda::Sda, I2cScl::Scl>();
I2cMaster1::initialize<SystemClock, 400_kBd>();

// Create a sensor object with the adress of the sensor built onto the Pololu AltIMU-10 v5
modm::Lis3mdl<I2cMaster1> sensor(0x1E);

// Turn on and configure the magnetometer
bool success = RF_CALL_BLOCKING(sensor.configure(modm::lis3mdl::DataRate::Rate_5_Hz,
modm::lis3mdl::Scale::Scale_8_gauss));


if(!success)
{
MODM_LOG_INFO << "Sensor could not be configured!" << modm::endl;
}

// Set the sensor to continous acquistion and turn on the temperature sensing
success = RF_CALL_BLOCKING(sensor.setMode(modm::lis3mdl::OperationMode::Continous));
if(!success)
{
MODM_LOG_INFO << "Sensor could not be started!" << modm::endl;
}

modm::Vector3f magVector;

while (true)
{
//Read the sensor data and print it out
success = RF_CALL_BLOCKING(sensor.readMagnetometer(magVector));

if(success)
{
MODM_LOG_INFO << "Magnetic Vector:" << modm::endl;
MODM_LOG_INFO << "X: "<< magVector.x << " gauss" << modm::endl;
MODM_LOG_INFO << "Y: "<< magVector.y << " gauss" << modm::endl;
MODM_LOG_INFO << "Z: "<< magVector.z << " gauss" << modm::endl;
MODM_LOG_INFO << modm::endl;
}
else
{
MODM_LOG_INFO << "Sensor could not be read!" << modm::endl;
}
modm::delay(1s);
}
return 0;
}
11 changes: 11 additions & 0 deletions examples/nucleo_f042k6/lis3mdl/project.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<library>
<extends>modm:nucleo-f042k6</extends>
<options>
<option name="modm:build:build.path">../../../build/nucleo_f042k6/lis3mdl</option>
</options>
<modules>
<module>modm:build:scons</module>
<module>modm:driver:lis3mdl</module>
<module>modm:platform:i2c:1</module>
</modules>
</library>

0 comments on commit 0994a55

Please sign in to comment.