F1 in Schools Car - Safety Modulation
Programming Adafruit Feather 32u4 to write acceleration and gyroscopic variables onto an SD card.
Components and supplies
1
Arduino UNO
Tools and machines
1
Computer
Apps and platforms
1
Arduino IDE
Project description
Code
roy_boy.ino
c_cpp
The code for the accelerometer and gyroscope
1// (c) Michael Schoeffler 2017, http://www.mschoeffler.de 2#include "Wire.h" // This library allows you to communicate with I2C devices. 3#include <SPI.h> 4#include <SD.h> 5const int MPU_ADDR = 0x68; // I2C address of the MPU-6050. If AD0 pin is set to HIGH, the I2C address will be 0x69. 6int16_t accelerometer_x, accelerometer_y, accelerometer_z; // variables for accelerometer raw data 7int16_t gyro_x, gyro_y, gyro_z; // variables for gyro raw data 8int16_t temperature; // variables for temperature data 9char tmp_str[7]; // temporary variable used in convert function 10char* convert_int16_to_str(int16_t i) { // converts int16 to string. Moreover, resulting strings will have the same length in the debug monitor. 11sprintf(tmp_str, "%6d", i); 12return tmp_str; 13const int chipSelect = 4; 14} 15void setup() { 16Serial.begin(9600); 17while (!Serial) { 18; // wait for serial port to connect. Needed for native USB port only 19} 20 21 22Serial.print("Initializing SD card..."); 23 24// see if the card is present and can be initialized: 25if (!SD.begin(4)) { 26Serial.println("Card failed, or not present"); 27// don't do anything more: 28while (1); 29} 30Serial.println("card initialized."); 31Wire.begin(); 32Wire.beginTransmission(MPU_ADDR); // Begins a transmission to the I2C slave (GY-521 board) 33Wire.write(0x6B); // PWR_MGMT_1 register 34Wire.write(0); // set to zero (wakes up the MPU-6050) 35Wire.endTransmission(true); 36} 37 38void loop() { 39// make a string for assembling the data to log: 40String dataString = ""; 41 42// read three sensors and append to the string: 43for (int analogPin = 0; analogPin < 3; analogPin++) { 44int sensor = analogRead(analogPin); 45dataString += String(sensor); 46if (analogPin < 2) { 47dataString += ","; 48} 49} 50 51// open the file. note that only one file can be open at a time, 52// so you have to close this one before opening another. 53File dataFile = SD.open("datalog.txt", FILE_WRITE); 54 55// if the file is available, write to it: 56if (dataFile) { 57dataFile.println(dataString); 58dataFile.close(); 59// print to the serial port too: 60Serial.println(dataString); 61} 62// if the file isn't open, pop up an error: 63else { 64Serial.println("error opening datalog.txt"); 65} 66Wire.beginTransmission(MPU_ADDR); 67Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H) [MPU-6000 and MPU-6050 Register Map and Descriptions Revision 4.2, p.40] 68Wire.endTransmission(false); // the parameter indicates that the Arduino will send a restart. As a result, the connection is kept active. 69Wire.requestFrom(MPU_ADDR, 7*2, true); // request a total of 7*2=14 registers 70 71// "Wire.read()<<8 | Wire.read();" means two registers are read and stored in the same variable 72accelerometer_x = Wire.read()<<8 | Wire.read(); // reading registers: 0x3B (ACCEL_XOUT_H) and 0x3C (ACCEL_XOUT_L) 73accelerometer_y = Wire.read()<<8 | Wire.read(); // reading registers: 0x3D (ACCEL_YOUT_H) and 0x3E (ACCEL_YOUT_L) 74accelerometer_z = Wire.read()<<8 | Wire.read(); // reading registers: 0x3F (ACCEL_ZOUT_H) and 0x40 (ACCEL_ZOUT_L) 75temperature = Wire.read()<<8 | Wire.read(); // reading registers: 0x41 (TEMP_OUT_H) and 0x42 (TEMP_OUT_L) 76gyro_x = Wire.read()<<8 | Wire.read(); // reading registers: 0x43 (GYRO_XOUT_H) and 0x44 (GYRO_XOUT_L) 77gyro_y = Wire.read()<<8 | Wire.read(); // reading registers: 0x45 (GYRO_YOUT_H) and 0x46 (GYRO_YOUT_L) 78gyro_z = Wire.read()<<8 | Wire.read(); // reading registers: 0x47 (GYRO_ZOUT_H) and 0x48 (GYRO_ZOUT_L) 79 80// print out data 81Serial.print("aX = "); Serial.print(convert_int16_to_str(accelerometer_x)); 82Serial.print(" | aY = "); Serial.print(convert_int16_to_str(accelerometer_y)); 83Serial.print(" | aZ = "); Serial.print(convert_int16_to_str(accelerometer_z)); 84// the following equation was taken from the documentation [MPU-6000/MPU-6050 Register Map and Description, p.30] 85Serial.print(" | tmp = "); Serial.print(temperature/340.00+36.53); 86Serial.print(" | gX = "); Serial.print(convert_int16_to_str(gyro_x)); 87Serial.print(" | gY = "); Serial.print(convert_int16_to_str(gyro_y)); 88Serial.print(" | gZ = "); Serial.print(convert_int16_to_str(gyro_z)); 89Serial.println(); 90 91// delay 92delay(50); 93} 94
roy_boy.ino
c_cpp
The code for the accelerometer and gyroscope
1// (c) Michael Schoeffler 2017, http://www.mschoeffler.de 2#include "Wire.h" // This library allows you to communicate with I2C devices. 3#include <SPI.h> 4#include <SD.h> 5const int MPU_ADDR = 0x68; // I2C address of the MPU-6050. If AD0 pin is set to HIGH, the I2C address will be 0x69. 6int16_t accelerometer_x, accelerometer_y, accelerometer_z; // variables for accelerometer raw data 7int16_t gyro_x, gyro_y, gyro_z; // variables for gyro raw data 8int16_t temperature; // variables for temperature data 9char tmp_str[7]; // temporary variable used in convert function 10char* convert_int16_to_str(int16_t i) { // converts int16 to string. Moreover, resulting strings will have the same length in the debug monitor. 11sprintf(tmp_str, "%6d", i); 12return tmp_str; 13const int chipSelect = 4; 14} 15void setup() { 16Serial.begin(9600); 17while (!Serial) { 18; // wait for serial port to connect. Needed for native USB port only 19} 20 21 22Serial.print("Initializing SD card..."); 23 24// see if the card is present and can be initialized: 25if (!SD.begin(4)) { 26Serial.println("Card failed, or not present"); 27// don't do anything more: 28while (1); 29} 30Serial.println("card initialized."); 31Wire.begin(); 32Wire.beginTransmission(MPU_ADDR); // Begins a transmission to the I2C slave (GY-521 board) 33Wire.write(0x6B); // PWR_MGMT_1 register 34Wire.write(0); // set to zero (wakes up the MPU-6050) 35Wire.endTransmission(true); 36} 37 38void loop() { 39// make a string for assembling the data to log: 40String dataString = ""; 41 42// read three sensors and append to the string: 43for (int analogPin = 0; analogPin < 3; analogPin++) { 44int sensor = analogRead(analogPin); 45dataString += String(sensor); 46if (analogPin < 2) { 47dataString += ","; 48} 49} 50 51// open the file. note that only one file can be open at a time, 52// so you have to close this one before opening another. 53File dataFile = SD.open("datalog.txt", FILE_WRITE); 54 55// if the file is available, write to it: 56if (dataFile) { 57dataFile.println(dataString); 58dataFile.close(); 59// print to the serial port too: 60Serial.println(dataString); 61} 62// if the file isn't open, pop up an error: 63else { 64Serial.println("error opening datalog.txt"); 65} 66Wire.beginTransmission(MPU_ADDR); 67Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H) [MPU-6000 and MPU-6050 Register Map and Descriptions Revision 4.2, p.40] 68Wire.endTransmission(false); // the parameter indicates that the Arduino will send a restart. As a result, the connection is kept active. 69Wire.requestFrom(MPU_ADDR, 7*2, true); // request a total of 7*2=14 registers 70 71// "Wire.read()<<8 | Wire.read();" means two registers are read and stored in the same variable 72accelerometer_x = Wire.read()<<8 | Wire.read(); // reading registers: 0x3B (ACCEL_XOUT_H) and 0x3C (ACCEL_XOUT_L) 73accelerometer_y = Wire.read()<<8 | Wire.read(); // reading registers: 0x3D (ACCEL_YOUT_H) and 0x3E (ACCEL_YOUT_L) 74accelerometer_z = Wire.read()<<8 | Wire.read(); // reading registers: 0x3F (ACCEL_ZOUT_H) and 0x40 (ACCEL_ZOUT_L) 75temperature = Wire.read()<<8 | Wire.read(); // reading registers: 0x41 (TEMP_OUT_H) and 0x42 (TEMP_OUT_L) 76gyro_x = Wire.read()<<8 | Wire.read(); // reading registers: 0x43 (GYRO_XOUT_H) and 0x44 (GYRO_XOUT_L) 77gyro_y = Wire.read()<<8 | Wire.read(); // reading registers: 0x45 (GYRO_YOUT_H) and 0x46 (GYRO_YOUT_L) 78gyro_z = Wire.read()<<8 | Wire.read(); // reading registers: 0x47 (GYRO_ZOUT_H) and 0x48 (GYRO_ZOUT_L) 79 80// print out data 81Serial.print("aX = "); Serial.print(convert_int16_to_str(accelerometer_x)); 82Serial.print(" | aY = "); Serial.print(convert_int16_to_str(accelerometer_y)); 83Serial.print(" | aZ = "); Serial.print(convert_int16_to_str(accelerometer_z)); 84// the following equation was taken from the documentation [MPU-6000/MPU-6050 Register Map and Description, p.30] 85Serial.print(" | tmp = "); Serial.print(temperature/340.00+36.53); 86Serial.print(" | gX = "); Serial.print(convert_int16_to_str(gyro_x)); 87Serial.print(" | gY = "); Serial.print(convert_int16_to_str(gyro_y)); 88Serial.print(" | gZ = "); Serial.print(convert_int16_to_str(gyro_z)); 89Serial.println(); 90 91// delay 92delay(50); 93} 94
Downloadable files
The code, if u couldnt get it before
The code, if u couldnt get it before
Comments
Only logged in users can leave comments