Page 1 of 1

MCP CAN and MPU650 bite each other

Posted: Thu Aug 20, 2020 1:13 pm
by thala
Hi , im currently building a connection from stm32f4 via canbus to arduino mega. Actually it works well, i had made two different programms and programmed Can message Sending and MPU Data logging.But if i want to use them both in the same script, the MPU throws zeros insteadt of normal values. If i commentate the MCP code out, it works again. Did someone having same isues and give me a idea where to search?.

Code: Select all

#include <Wire.h>


#include <Arduino.h>
#include <TinyMPU6050.h>
#include <can.h>
#include <mcp2515.h>



#include <SPI.h>


MPU6050 mpu(Wire);

struct can_frame canMsg1;
struct can_frame canMsg2;
MCP2515 mcp2515(PC1);                                  // Set CS to pin 10
int rpm = 2;
void setup()
{SPI.setMOSI(PA7);
SPI.setMISO(PA6);
SPI.setSCLK(PA5);

   
SPI.begin();
  
  
  Serial.begin(9600);
  mcp2515.reset();
  mcp2515.setBitrate(CAN_500KBPS,MCP_8MHZ);
  mcp2515.setNormalMode();

  mpu.Initialize();

  // Calibration
  Serial.begin(9600);
  Serial.println("=====================================");
  Serial.println("Starting calibration...");
  mpu.Calibrate();
  Serial.println("Calibration complete!");

}

void loop()
{  
  mpu.Execute(); 
  delay(100);
int a = ((int)((mpu.GetAccX())));

int b = (int)((mpu.GetAccY()));

int c = (int)((mpu.GetAccZ()));
int d = (int)((mpu.GetGyroX()));
int e = (int)((mpu.GetGyroY()));
int f =  (int)((mpu.GetGyroZ()));  

delay(100);
Serial.println(a);
Serial.println(b);
Serial.println(c);
Serial.println(d);
Serial.println(e);
Serial.println(f);

 
  canMsg1.can_id  = 0x0F6;
  canMsg1.can_dlc = 8;
  canMsg1.data[0] = a;
  canMsg1.data[1] = b;
  canMsg1.data[2] = c;
  canMsg1.data[3] = d;
  canMsg1.data[4] = e;
  canMsg1.data[5] = f;
  canMsg1.data[6] = 0xBE;
  canMsg1.data[7] = 0x86;

  canMsg2.can_id  = 0x036;
  canMsg2.can_dlc = 8;
 
  canMsg2.data[2] = a;


  canMsg2.data[6] = 0x00;
  canMsg2.data[7] = 0xA0;


    mcp2515.sendMessage(&canMsg1);
  delay(1000);                       // send dat  int b = (int)a; 
    
}

if i commentate this out it works:

Code: Select all

MCP2515 mcp2515(PC1);       


Sry for my bad english, didnt wrote for a whole lot of time in english.


Would be verry thanksfull if someone has an idea

Re: MCP CAN and MPU650 bite each other

Posted: Thu Aug 20, 2020 1:48 pm
by fpiSTM
Hi,

hard to tell.
Which STMF4 exactly you used ?
Maybe a conflict with both library? Pins conflict? Interrupt ?

The program run but you get only 0 ?

Re: MCP CAN and MPU650 bite each other

Posted: Tue Aug 25, 2020 1:05 pm
by thala
Yes. If i watch it on serial interface and outcommentate the mcp line everything is fine. If i commentate it in, there are only series for the accs and yaws.

STMF407G-DISC1

What i dont understand, it is with all mpu6050 libraries. Currently im Using Tiny MPU6050 and Libmaster can


Truely im far away from knowingenought , to search errors into the library code.

what i saw, both included arduino.h. but i dindt find if somewhere the i2c is new declared.
in my opinion the two libraries couldnt bite each other, cause one goes over spi and one over i2c?

Re: MCP CAN and MPU650 bite each other

Posted: Fri Aug 28, 2020 9:56 am
by fpiSTM
Well hard to say.
Could you give us the libraries you used.

Re: MCP CAN and MPU650 bite each other

Posted: Tue Sep 01, 2020 5:49 pm
by thala
Hi i tried myself by searching.
First i threw out the MPU Librarie and hardcoded the mpu part
So actual code is:

Code: Select all

#include <can.h>
#include <mcp2515.h>




#include <SPI.h>
 

int gyro_x, gyro_y, gyro_z;
long acc_x, acc_y, acc_z, acc_total_vector;
int temperature;
long gyro_x_cal, gyro_y_cal, gyro_z_cal;
long loop_timer;
int lcd_loop_counter;
float angle_pitch, angle_roll, angle_yaw;
int angle_pitch_buffer, angle_roll_buffer, angle_yaw_buffer;
//boolean set_gyro_angles;
float angle_roll_acc, angle_pitch_acc, angle_yaw_acc;
float angle_pitch_output, angle_roll_output, angle_yaw_output;

struct can_frame canMsg1;
struct can_frame canMsg2;
MCP2515 mcp2515(PC1);                                  // Set CS to pin 10
int rpm = 2;



 

void setup() {
  wire.begin(); 
}

void loop() {
 
  delay(1000);
}
{
   Serial.begin(9600);//not needed

   
    Wire.begin();
    delay(250);
  
  
  SPI.setMOSI(PA7);
SPI.setMISO(PA6);
SPI.setSCLK(PA5);

SPI.begin();
  
 setup_mpu_6050_registers();

}
void read_mpu_6050_data() {                                             //Subroutine for reading the raw gyro and accelerometer data
    Wire.beginTransmission(0x68);                                        //Start communicating with the MPU-6050
    Wire.write(0x3B);                                                    //Send the requested starting register
    Wire.endTransmission();                                              //End the transmission
    Wire.requestFrom(0x68, 14);                                           //Request 14 bytes from the MPU-6050
    while (Wire.available() < 14);                                        //Wait until all the bytes are received
    acc_x = Wire.read() << 8 | Wire.read();                                  //Add the low and high byte to the acc_x variable
    acc_y = Wire.read() << 8 | Wire.read();                                  //Add the low and high byte to the acc_y variable
    acc_z = Wire.read() << 8 | Wire.read();                                  //Add the low and high byte to the acc_z variable
    temperature = Wire.read() << 8 | Wire.read();                            //Add the low and high byte to the temperature variable
    gyro_x = Wire.read() << 8 | Wire.read();                                 //Add the low and high byte to the gyro_x variable
    gyro_y = Wire.read() << 8 | Wire.read();                                 //Add the low and high byte to the gyro_y variable
    gyro_z = Wire.read() << 8 | Wire.read();                                 //Add the low and high byte to the gyro_z variable

}


void setup_mpu_6050_registers() {
    //Activate the MPU-6050
    wire.beginTransmission(0x68);                                        //Start communicating with the MPU-6050
    wire.write(0x6B);                                                    //Send the requested starting register
    wire.write(0x00);                                                    //Set the requested starting register
    wire.endTransmission();                                              //End the transmission
    //Configure the accelerometer (+/-8g)
    wire.beginTransmission(0x68);                                        //Start communicating with the MPU-6050
    wire.write(0x1C);                                                    //Send the requested starting register
    wire.write(0x10);                                                    //Set the requested starting register
    wire.endTransmission();                                              //End the transmission
    //Configure the gyro (500dps full scale)
    wire.beginTransmission(0x68);                                        //Start communicating with the MPU-6050
    wire.write(0x1B);                                                    //Send the requested starting register
    wire.write(0x08);                                                    //Set the requested starting register
    wire.endTransmission();                                              //End the transmission
}
void loop()
{  
  read_mpu_6050_data();
int a = (int)(acc_x);

int b = (int)(acc_y);

int c = (int)(acc_z);
int d = (int)(gyro_x);
int e = (int)(gyro_y);
int f =  (int)(gyro_z);
delay(100);
Serial.print("X = ");
    Serial.println(gyro_x);
    Serial.print("Y = ");
    Serial.println(gyro_y);
    Serial.print("Z = ");
    Serial.println();
    delay(250);

 
  canMsg1.can_id  = 0x0F6;
  canMsg1.can_dlc = 8;
  canMsg1.data[0] = a;
  canMsg1.data[1] = b;
  canMsg1.data[2] = c;
  canMsg1.data[3] = d;
  canMsg1.data[4] = e;
  canMsg1.data[5] = f;
  canMsg1.data[6] = 0xBE;
  canMsg1.data[7] = 0x86;

  canMsg2.can_id  = 0x036;
  canMsg2.can_dlc = 8;
 
  canMsg2.data[2] = a;


  canMsg2.data[6] = 0x00;
  canMsg2.data[7] = 0xA0;


    mcp2515.sendMessage(&canMsg1);
  delay(1000);                       // send dat  int b = (int)a; 
    
}
For me it looks like the mcp can library deactivates the i2c interface
Cause if i commentate the

Code: Select all

MCP2515 mcp2515(PC1);       
out, the imu is giving data which are plausible. With this in, the imu doesnt throw any changing data.

MCP Library :https://github.com/autowp/arduino-mcp2515


Or is it that kind of problem: viewtopic.php?f=18&t=630&p=4101&hilit=i2c+spi#p4101 --> I tryed to redefine , but nothing changend

Re: MCP CAN and MPU650 bite each other

Posted: Tue Sep 14, 2021 9:25 am
by fawadansari89
Hi I need
help with using MCP2515 library, this is my sketch my BLUEPILL is not responding to it
kindly guide me also what pins to use for SPI communication
#include <SPI.h> //Library for using SPI Communication
#include <mcp2515.h> //Library for using CAN Communication


struct can_frame canMsg;
MCP2515 mcp2515(PA4);


void setup()
{
while (!Serial);
Serial.begin(9600);

SPI.begin(); //Begins SPI communication

mcp2515.reset();
mcp2515.setBitrate(CAN_500KBPS,MCP_8MHZ); //Sets CAN at speed 500KBPS and Clock 8MHz
mcp2515.setNormalMode();
canMsg.can_id = 0x036; //CAN id as 0x036
canMsg.can_dlc = 1; //CAN data length as 8
}

void loop()
{

int pot = analogRead(A3);
pot = map(pot,0, 1023, 0, 255);

canMsg.data[0] = pot; //Update humidity value in [0]
mcp2515.sendMessage(&canMsg); //Sends the CAN message
delay(1000);
}