Starting your project

This commit is contained in:
Kosmo Obermayer 2024-04-04 11:29:54 +02:00
parent c5b001638e
commit d5cc9cdf9c
2 changed files with 137 additions and 1 deletions

View File

@ -12,3 +12,9 @@
platform = espressif32
board = upesy_wroom
framework = arduino
lib_deps = adafruit/Adafruit MPU6050@^2.2.6
; How to change bit-per-second rate for your project:
; Add right under [env:upesy_wroom]:
; "monitor_speed = x" (without the "")
; x == baudrate of selected choice, bsp: 115200, 8000, 300

View File

@ -1,11 +1,141 @@
#include <Arduino.h>
//--------------------------------------------------------------------------------------------------------------------------//
// Setting up the Adafruit_MPU6050 library for the accelerator chip
//--------------------------------------------------------------------------------------------------------------------------//
// Check the other guide in the .docx as to how to INSTALL the necessary library //
// Must be included for most things! //
#include <Arduino.h> // Only include once of course.
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
// Create instance of the sensor library //
Adafruit_MPU6050 mpu;
void setup()
{
Serial.begin(115200); // For Serial communication, check if the right bit-per-second rate is selected!
// If you want to change said bit-per-second, look in the .ini file for information.
// How do you know the one selected does not work / there's something wrong in the .ini file?
// Check the terminal, if that is the case everything printed is Unicode gibberish.
while(!Serial) delay(10); // Wait until the Serial communication is set up and initialized.
Serial.println("Starting...");
if(!mpu.begin()) // Exception for the case of the mpu not starting up
{
// If this is the case, check the wiring and whether or not the right pins are connected.
// Another issue that could lead to this is too little or too much current going through the chip.
Serial.println("Failed to find MPU6050 chip.");
while(1)
{
delay(10);
}
}
Serial.println("MPU6050 has been found.");
mpu.setAccelerometerRange(MPU6050_RANGE_8_G); // Set to the range of your choice for the accelerator values
// depending on the range you want your read in values to vary in.
Serial.println("Accelerator range set to: ");
switch(mpu.getAccelerometerRange()) // Check setting and print for insight.
{
case MPU6050_RANGE_2_G:
Serial.println("+-2G");
break;
case MPU6050_RANGE_4_G:
Serial.println("+-4G");
break;
case MPU6050_RANGE_8_G:
Serial.println("+-8G");
break;
case MPU6050_RANGE_16_G:
Serial.println("+-16G");
break;
}
mpu.setGyroRange(MPU6050_RANGE_500_DEG);
Serial.print("Gyro range set to: ");
switch (mpu.getGyroRange())
{
case MPU6050_RANGE_250_DEG:
Serial.println("+- 250 deg/s");
break;
case MPU6050_RANGE_500_DEG:
Serial.println("+- 500 deg/s");
break;
case MPU6050_RANGE_1000_DEG:
Serial.println("+- 1000 deg/s");
break;
case MPU6050_RANGE_2000_DEG:
Serial.println("+- 2000 deg/s");
break;
}
mpu.setFilterBandwidth(MPU6050_BAND_21_HZ);
Serial.print("Filter bandwidth set to: ");
switch (mpu.getFilterBandwidth())
{
case MPU6050_BAND_260_HZ:
Serial.println("260 Hz");
break;
case MPU6050_BAND_184_HZ:
Serial.println("184 Hz");
break;
case MPU6050_BAND_94_HZ:
Serial.println("94 Hz");
break;
case MPU6050_BAND_44_HZ:
Serial.println("44 Hz");
break;
case MPU6050_BAND_21_HZ:
Serial.println("21 Hz");
break;
case MPU6050_BAND_10_HZ:
Serial.println("10 Hz");
break;
case MPU6050_BAND_5_HZ:
Serial.println("5 Hz");
break;
}
delay(100);
// What is the difference between "void setup()" and "void loop()"? Why put all this in here? //
// The setup fuction is only initialized once, and though that's true for the entire lifetime of the programm,
// the started services continue to run in here on this threat, allowing the loop to be executed simultaniously.
// Mind, and that's IMPORTANT!!, that you should refer from using "while(1)" in this function as much as possible.
// (I'm aware there is one in here in this project, but only because it is handled correctly and carefully.)
}
// Put your variables here for testing, otherwise I'd recommend putting it in a class //
float _x_accel = 0; // x acceleration
float _y_accel = 0; // y acceleration
float _calc = 0; // for calculations
void loop()
{
// Save vals if you want to send them: //
sensors_event_t _a;
sensors_event_t _g;
sensors_event_t _temp; // Used as placeholder for now in the next function, even if you don't need temperature values.
mpu.getEvent(&_a, &_g, &_temp); // Reads in your values.
_x_accel = _a.acceleration.x; // acceleration in x.
_y_accel = _a.acceleration.y; // acceleration in y.
// Basic example for use //
_calc = _y_accel * 12.5; // Calculate postion if you want to.
if(_calc > 100) // reduce values if you only want values from (-100) - 100 for example
{
_calc = 100;
}
else if(_calc < (-100))
{
_calc = (-100);
}
// And done! //
// Mind that everything in this loop repeats itself over and over, only stopping until the programm fails to execute or //
// the current is cut. If you'd like to restart everything, press the little button titled "RST" or "BOOT" (if the first does nothing) on the ESP. //
}