Skip to content

Commit

Permalink
Merge pull request #12 from senseshift/feature/mpu6050
Browse files Browse the repository at this point in the history
✨ Add MPU6050
  • Loading branch information
leon0399 authored Jul 21, 2024
2 parents eb089de + 00aa081 commit 9030266
Show file tree
Hide file tree
Showing 13 changed files with 891 additions and 20 deletions.
104 changes: 91 additions & 13 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,52 @@ name: PlatformIO CI

on:
pull_request:
branches:
- main
- master
- develop
paths-ignore:
- "**/*.md"
paths:
- "examples/**/*"
- "src/**/*"
push:
branches:
- main
- master
- main
- develop
paths-ignore:
- "**/*.md"

jobs:
platformio:
runs-on: ${{ matrix.os }}

# env:
# PLATFORMIO_BUILD_DIR: ./../build

strategy:
matrix:
os: [ ubuntu-latest ]
example:
- "examples/PCA9685/Servo/Servo.ino"
- "examples/PCA9685/VibroPulse/VibroPulse.ino"
- "PCA9685/Servo"
- "PCA9685/VibroPulse"
- "MPU6050/BasicReadings"
boards: [ [ uno, esp32dev ] ]

steps:
- uses: actions/checkout@v4

- name: Prepare metadata
id: metadata
run: |
# get 1st element from the example path as lowercase
DEVICE=$(echo "${{ matrix.example }}" | cut -d'/' -f1 | tr '[:upper:]' '[:lower:]')
echo "device=$DEVICE" >> "$GITHUB_OUTPUT"
# check if example folder contains a wokwi.toml file
SHOULD_UPLOAD_ARTIFACTS=$(test -f "examples/${{ matrix.example }}/wokwi.toml" && echo "true" || echo "false")
echo "upload_artifact=$SHOULD_UPLOAD_ARTIFACTS" >> "$GITHUB_OUTPUT"
ARTIFACT_NAME=$(echo "${{ matrix.example }}" | tr '/' '-')
echo "artifact_name=$ARTIFACT_NAME" >> "$GITHUB_OUTPUT"
# mkdtemp
# export PLATFORMIO_BUILD_DIR=$(mktemp -d)
# echo "PLATFORMIO_BUILD_DIR=$PLATFORMIO_BUILD_DIR" >> "$GITHUB_ENV"
- name: Cache pip
uses: actions/cache@v4
with:
Expand Down Expand Up @@ -63,6 +80,67 @@ jobs:
- name: Build example
run: |
pio ci --lib="." --board=${{ join(matrix.boards, ' --board=') }}
set -o pipefail
# for whatever reason, if we specify the build dir, it doesn't work
# mkdir -p ${{ env.PLATFORMIO_BUILD_DIR }}
pio ci --lib="." --board=${{ join(matrix.boards, ' --board=') }} --keep-build-dir 2>&1 | tee output.log
if [ $? -ne 0 ]; then exit 1; fi
export PLATFORMIO_BUILD_DIR=$(grep -oP 'The following files/directories have been created in \K.*' output.log)
echo "PLATFORMIO_BUILD_DIR=$PLATFORMIO_BUILD_DIR" >> "$GITHUB_ENV"
env:
PLATFORMIO_CI_SRC: ${{ matrix.example }}
FORCE_COLOR: 2 # Enable color output
PLATFORMIO_CI_SRC: "./examples/${{ matrix.example }}/*.ino"
PLATFORMIO_BUILD_FLAGS: "-D CORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_DEBUG"

- name: Upload artifacts
if: steps.metadata.outputs.upload_artifact == 'true'
uses: actions/upload-artifact@v4
with:
name: example-${{ steps.metadata.outputs.artifact_name }}
path: |
${{ env.PLATFORMIO_BUILD_DIR }}/.pio/build/*/firmware.*
retention-days: 1

wokwi:
runs-on: ${{ matrix.os }}

needs: platformio

strategy:
matrix:
os: [ ubuntu-latest ]

include:
- example: "MPU6050/BasicReadings"
- board: uno

steps:
- uses: actions/checkout@v4

- name: Prepare metadata
id: metadata
run: |
ARTIFACT_NAME=$(echo "${{ matrix.example }}" | tr '/' '-')
echo "artifact_name=$ARTIFACT_NAME" >> "$GITHUB_OUTPUT"
- uses: actions/download-artifact@v4
with:
name: example-${{ steps.metadata.outputs.artifact_name }}
path: build

- name: Copy compiled firmware
run: |
cp -ru build/${{ matrix.board }}/* examples/${{ matrix.example }}
- name: Run simulation
uses: leon0399/wokwi-ci-action@main
with:
token: ${{ secrets.WOKWI_CLI_TOKEN }}
timeout: 60000
path: ./examples/${{ matrix.example }}
scenario: 'scenario.yml'
diagram_file: 'diagram.${{ matrix.board }}.json'
elf: '${{ github.workspace }}/build/${{ matrix.board }}/firmware.elf'
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
.vscode/c_cpp_properties.json
.vscode/launch.json
.vscode/ipch

build/
46 changes: 46 additions & 0 deletions examples/MPU6050/BasicReadings/BasicReadings.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include <I2CDevLib.h>
#include <i2cdev/mpu6050.hpp>

i2cdev::MPU6050 mpu6050;

void setup(void) {
Serial.begin(115200);
while (!Serial)
delay(10); // will pause Zero, Leonardo, etc until serial console opens

Wire.begin();

Serial.println("I2CDevLibContrib MPU6050 test!");

// Wait for the MPU6050 to be ready
delay(1000);

if (mpu6050.check() != I2CDEV_RESULT_OK) {
Serial.println("Failed to find MPU6050 chip");
while (true) {}
}
Serial.println("MPU6050 Found!");

if (mpu6050.reset() != I2CDEV_RESULT_OK) {
Serial.println("Failed to reset MPU6050 chip");
while (true) {}
}

mpu6050.setAccelerometerRange(MPU6050_ACCEL_RANGE_8G);
mpu6050.setGyroscopeRange(MPU6050_GYRO_RANGE_500_DEG);
mpu6050.setFilterBandwidth(MPU6050_BANDWIDTH_21_HZ);

Serial.println("");
delay(100);
}

void loop() {
auto measurements = mpu6050.getAllMeasurements();

Serial.print("Accelerometer X: "); Serial.print(measurements.accel.x); Serial.print(" Y: "); Serial.print(measurements.accel.y); Serial.print(" Z: "); Serial.println(measurements.accel.z);
Serial.print("Gyroscope X: "); Serial.print(measurements.gyro.x); Serial.print(" Y: "); Serial.print(measurements.gyro.y); Serial.print(" Z: "); Serial.println(measurements.gyro.z);
Serial.print("Temperature: "); Serial.print(measurements.temperature); Serial.println(" C");
Serial.println("");

delay(500);
}
18 changes: 18 additions & 0 deletions examples/MPU6050/BasicReadings/diagram.uno.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"version": 1,
"author": "Leonid Meleshin",
"editor": "wokwi",
"parts": [
{ "type": "wokwi-arduino-uno", "id": "uno", "top": 0.6, "left": -0.6, "attrs": {} },
{
"type": "wokwi-mpu6050",
"id": "imu1",
"top": -131.42,
"left": 166.12,
"rotate": -90,
"attrs": {}
}
],
"connections": [ [ "uno:A4.2", "imu1:SDA", "green", [ "v0" ] ], [ "uno:A5.2", "imu1:SCL", "green", [ "v0" ] ] ],
"dependencies": {}
}
10 changes: 10 additions & 0 deletions examples/MPU6050/BasicReadings/scenario.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
name: Test MPU6050/BasicReadings
version: 1
author: Leonid Meleshin

steps:
- wait-serial: "MPU6050 Found!"

- wait-serial: "Accelerometer X: 0.00 Y: 0.00 Z: 1.00"
- wait-serial: "Gyroscope X: 0.00 Y: 0.00 Z: 0.00"
- wait-serial: "Temperature: 24.00 C"
6 changes: 6 additions & 0 deletions examples/MPU6050/BasicReadings/wokwi.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[wokwi]
version = 1
firmware = "./firmware.hex"
elf = "./firmware.elf"

gdbServerPort=3333
5 changes: 5 additions & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html

[env:arduino_uno]
platform = atmelavr
board = uno
framework = arduino

[env:esp32dev]
platform = espressif32
board = esp32dev
Expand Down
18 changes: 14 additions & 4 deletions src/hal/I2CDevBus_ArduinoWire.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@
#include "i2cdevbus.hpp"
#include <Wire.h>

inline __attribute__((always_inline)) void i2cdev_platform_sleep_us(uint32_t us) {
if (us > 1000) {
// delay in ms, then in us
delay(us / 1000);
delayMicroseconds(us % 1000);
} else {
delayMicroseconds(us);
}
}

class ArduinoI2CDevBus : public I2CDevBus {
public:
explicit ArduinoI2CDevBus(TwoWire* wire = &Wire) : wire_(wire) {}
Expand All @@ -19,7 +29,7 @@ class ArduinoI2CDevBus : public I2CDevBus {
uint16_t timeout = DEFAULT_READ_TIMEOUT_MS
) -> i2cdev_result_t override
{
I2CDEVLIB_LOG_D("readReg8: devAddr=0x%02X, regAddr=0x%02X, data=%s", devAddr, regAddr, i2cdevlib::hexdump(data, length).c_str());
I2CDEVLIB_LOG_D("readReg8: devAddr=0x%02X, regAddr=0x%02X, data=%s", devAddr, regAddr, i2cdevlib::hexdump(data, length));

this->wire_->beginTransmission(devAddr);
this->wire_->write(regAddr);
Expand Down Expand Up @@ -58,7 +68,7 @@ class ArduinoI2CDevBus : public I2CDevBus {
uint16_t timeout = DEFAULT_READ_TIMEOUT_MS
) -> i2cdev_result_t override
{
I2CDEVLIB_LOG_D("readReg16: devAddr=0x%02X, regAddr=0x%02X, data=%s", devAddr, regAddr, i2cdevlib::hexdump(data, length).c_str());
I2CDEVLIB_LOG_D("readReg16: devAddr=0x%02X, regAddr=0x%02X, data=%s", devAddr, regAddr, i2cdevlib::hexdump(data, length));

this->wire_->beginTransmission(devAddr);
this->wire_->write(regAddr);
Expand Down Expand Up @@ -92,7 +102,7 @@ class ArduinoI2CDevBus : public I2CDevBus {
auto writeReg8(uint8_t devAddr, uint8_t regAddr, size_t length, const uint8_t* data)
-> i2cdev_result_t override
{
I2CDEVLIB_LOG_D("writeReg8: devAddr=0x%02X, regAddr=0x%02X, data=%s", devAddr, regAddr, i2cdevlib::hexdump(data, length).c_str());
I2CDEVLIB_LOG_D("writeReg8: devAddr=0x%02X, regAddr=0x%02X, data=%s", devAddr, regAddr, i2cdevlib::hexdump(data, length));

this->wire_->beginTransmission(devAddr);

Expand All @@ -116,7 +126,7 @@ class ArduinoI2CDevBus : public I2CDevBus {
auto writeReg16(uint8_t devAddr, uint8_t regAddr, size_t length, const uint16_t* data)
-> i2cdev_result_t override
{
I2CDEVLIB_LOG_D("writeReg16: devAddr=0x%02X, regAddr=0x%02X, data=%s", devAddr, regAddr, i2cdevlib::hexdump(data, length).c_str());
I2CDEVLIB_LOG_D("writeReg16: devAddr=0x%02X, regAddr=0x%02X, data=%s", devAddr, regAddr, i2cdevlib::hexdump(data, length));

this->wire_->beginTransmission(devAddr);

Expand Down
Loading

0 comments on commit 9030266

Please sign in to comment.