Quantcast
Channel: Arduino Forum - Latest topics
Viewing all articles
Browse latest Browse all 15404

Arduino Uno R4 MINIMA with UM7 and CANBUS

$
0
0

I applied the example of CANBUS with MINIMA with IMU UM7. As result I cannot accept the imu.pitch value from serial.print that I saw. Because It is slow and not update the real angle as I rotate the sensor.
Program is

#include <UM7.h>
#include <Arduino_CAN.h>


/**************************************************************************************

 * CONSTANTS

 **************************************************************************************/


static uint32_t const CAN_ID = 0x20;
//Connect the RX pin on the UM7 to TX1 (pin 18) on the DUE
//Connect the TX pin on the UM7 to RX1 (pin 19) on the DUE

UM7 imu;


void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  Serial1.begin(115200);
  while (!Serial) { }
  if (!CAN.begin(CanBitRate::BR_1000k))
  {
    Serial.println("CAN.begin(...) failed.");
    for (;;) {}
  }
}

static uint32_t msg_cnt = 0;

void loop() {
  // put your main code here, to run repeatedly:
  if (Serial1.available() > 0) {
    if (imu.encode(Serial1.read())) {  // Reads byte from buffer.  Valid packet returns true.
      Serial.println(imu.pitch);
      Serial.println(imu.roll);
      Serial.println(imu.yaw);
    }
  }
  /* Assemble a CAN message with the format of
   * 0xCA 0xFE 0x00 0x00 [4 byte message counter]
   */
  uint8_t const msg_data[] = {0xCA,0xFE,0,0,0,0,0,0};
  memcpy((void *)(msg_data + 4), &msg_cnt, sizeof(msg_cnt));
  CanMsg const msg(CanStandardId(CAN_ID), sizeof(msg_data), msg_data);
  Serial.println(msg);
  /* Transmit the CAN message, capture and display an
   * error core in case of failure.
   */

  if (int const rc = CAN.write(msg); rc < 0)
  {
    Serial.print  ("CAN.write(...) failed with error code ");
    Serial.println(rc);
    for (;;) { }
  }

  /* Increase the message counter. */
  msg_cnt++;

  /* Only send one message per second. */
  delay(500);
}

Meanwhile I run IMU example alone, without CANBUS. The IMU.pitch is generated and printed via Serial monitor in real time, I rotate sensor, pitch angle is changed via Serial monitor.

#include <UM7.h>

UM7 imu;


void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  Serial1.begin(115200);

}

static uint32_t msg_cnt = 0;

void loop() {
  // put your main code here, to run repeatedly:
  if (Serial1.available() > 0) {
    if (imu.encode(Serial1.read())) {  // Reads byte from buffer.  Valid packet returns true.
      Serial.println(imu.pitch);
      Serial.println(imu.roll);
      Serial.println(imu.yaw);
    }
  }
}

The question is What is the wrong point, why it is happened, how can I solve it???

1 post - 1 participant

Read full topic


Viewing all articles
Browse latest Browse all 15404

Trending Articles