CAN Protocol With MPU6050 || Drivers Behavior Analysis Reading Data||#CA...


MASTER:-
#include <SPI.h>
#include <mcp2515.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
//LiquidCrystal_I2C LCD(0x27,16,2);//Default address is 0x27
LiquidCrystal_I2C LCD(0x3F,16,2);//Default address is 0x27


struct can_frame canMsg;
MCP2515 mcp2515(10);


int FAN = A0;

void setup() {
  Serial.begin(115200);
  LCD.init();
  LCD.backlight();
  
  SPI.begin();
  mcp2515.reset();
  mcp2515.setBitrate(CAN_500KBPS, MCP_8MHZ);
  mcp2515.setNormalMode();

  pinMode(FAN, OUTPUT);

}

void loop() 
{
  if (mcp2515.readMessage(&canMsg) == MCP2515::ERROR_OK) 
  {   
    if(canMsg.can_id == 0x0F6)
    {
      const int X_AXIS = canMsg.data[0];
      const int Y_AXIS = canMsg.data[1];
      const int Z_AXIS = canMsg.data[2];
      const int TEMPERATURE = canMsg.data[3];

//      Serial.print("******************************");
//      Serial.print("X-AXIS:");Serial.println(X_AXIS);
//      Serial.print("Y-AXIS:");Serial.println(Y_AXIS);
//      Serial.print("Z-AXIS:");Serial.println(Z_AXIS);
//      Serial.print("TEMPERATURE:");Serial.println(TEMPERATURE);
//      delay(100);

      if(X_AXIS == 3 || X_AXIS == 4 || X_AXIS == 5 || X_AXIS == 6 || X_AXIS == 7 || X_AXIS == 8 || X_AXIS == 9){ 
        Serial.print("VEHICLE IS LEANING RIGHT:");Serial.println(X_AXIS);
         LCD.setCursor(0,0);
         LCD.print("   VEHICLE IS   ");
         LCD.setCursor(0,1);
         LCD.print(" LEANING RIGHT ");
        }
      else if(X_AXIS == 253 || X_AXIS == 252 || X_AXIS == 251 || X_AXIS == 250 || X_AXIS == 249 || X_AXIS == 248){ 
        Serial.print("VEHICLE IS LEANING LEFT:");Serial.println(X_AXIS);
         LCD.setCursor(0,0);
         LCD.print("   VEHICLE IS   ");
         LCD.setCursor(0,1);
         LCD.print("  LEANING LEFT  ");
         }
      else if(Z_AXIS > 10){ 
        Serial.print("VEHICLE MOTION DETECTED:");Serial.println(Z_AXIS);
         LCD.setCursor(0,0);
         LCD.print("    VEHICLE    ");
         LCD.setCursor(0,1);
         LCD.print("    MOTION     ");
         }
         
      else if(TEMPERATURE > 33){ 
        Serial.print("TEMPERATURE IS HIGH:");Serial.println(TEMPERATURE);
        digitalWrite(FAN, HIGH);
         LCD.setCursor(0,0);
         LCD.print("TEMPERATURE  IS");
         LCD.setCursor(0,1);
         LCD.print("     HIGH      ");
         }   
      else{
        Serial.println("******************************"); 
        Serial.print("VEHICLE IS STABLE: ");Serial.println(X_AXIS);
        Serial.print("TEMPERATURE IS :");Serial.println(TEMPERATURE);
        digitalWrite(FAN, LOW);
         
         LCD.setCursor(0,0);
//         LCD.print(" X:");LCD.print(X_AXIS);LCD.print("  Y:");LCD.print(Y_AXIS);
         LCD.print(" TEMP = ");LCD.print(TEMPERATURE);LCD.print(" degC"); 
         LCD.setCursor(0,1);
         LCD.print(" VEHICLE STABLE");
        }  
    }    
  }
}



SLAVE:-
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <Wire.h>
#include <SPI.h>
#include <mcp2515.h>


struct can_frame canMsg;
MCP2515 mcp2515(10);


Adafruit_MPU6050 mpu;

void setup() {
  Serial.begin(115200);
  SPI.begin();
  mcp2515.reset();
  mcp2515.setBitrate(CAN_500KBPS, MCP_8MHZ);
  mcp2515.setNormalMode();
  

  canMsg.can_id  = 0x0F6;
  canMsg.can_dlc = 8;



  // Try to initialize!
  if (!mpu.begin()) {
    Serial.println("Failed to find MPU6050 chip");
    while (1) {
      delay(10);
    }
  }
  Serial.println("MPU6050 Found!");

  mpu.setAccelerometerRange(MPU6050_RANGE_8_G); 
  mpu.setGyroRange(MPU6050_RANGE_500_DEG);
  mpu.setFilterBandwidth(MPU6050_BAND_5_HZ);
  Serial.println("");
  delay(100); 
}

void loop() {
  sensors_event_t a, g, temp;
  mpu.getEvent(&a, &g, &temp);


  canMsg.data[0] = a.acceleration.x;
  canMsg.data[1] = a.acceleration.y;
  canMsg.data[2] = a.acceleration.z;
  canMsg.data[3] = temp.temperature;

  Serial.println("*****Acceleration*****");
  Serial.print("X axis: ");Serial.println(a.acceleration.x);
  Serial.print("Y axis: ");Serial.println(a.acceleration.y);
  Serial.print("Z axis: ");Serial.println(a.acceleration.z);
  Serial.print("TEMP  : ");Serial.println(temp.temperature);

 
  mcp2515.sendMessage(&canMsg);
  delay(100);
}

Post a Comment

0 Comments