Quantcast
Channel: Sensors - Arduino Forum
Viewing all articles
Browse latest Browse all 1146

Unable to send request to roboclaw when integrating an accelerometer

$
0
0

I am creating an autonomous drive bot that contains an MPU6050, a 3x4 keypad, a dual channel roboclaw 2x60, and an Elegoo Mega 2560 R3.

The keypad is wired to pins 3-9, the MPU6050 to 20 and 21, and the roboclaw is connected to pins 10 and 11. Sufficient power is being supplied to each item (tested).

The following two codes (for the keypad and accelerometer, respectively) work independently and as intended:

#include <Keypad.h>
#include <SoftwareSerial.h>
#include "RoboClaw.h"

SoftwareSerial serial(10,11);	
RoboClaw roboclaw(&serial,10000);

#define address 0x80

const byte ROWS = 4; 
const byte COLS = 3; 
int Command = 0;


char keys[ROWS][COLS] = {
  {'1', '2', '3'},
  {'4', '5', '6'},
  {'7', '8', '9'},
  {'*', '0', '#'}
};
byte rowPins[ROWS] = {9 , 8, 7, 6};
byte colPins[COLS] = {5, 4, 3};

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

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

  char key = keypad.getKey();
  if (key) {
    Serial.println(key);
    
    if(key == '6'){
      roboclaw.SpeedM1(address, 800);
      roboclaw.SpeedM2(address, 800);
    }
    if(key == '8'){
      roboclaw.SpeedM1(address, -400);
      roboclaw.SpeedM2(address, 400);
    }
    if(key == '5'){
      roboclaw.SpeedM1(address, -800);
      roboclaw.SpeedM2(address, -800);
    }
    if(key == '2'){
      roboclaw.SpeedM1(address, 400);
      roboclaw.SpeedM2(address, -400);
    }
    if(key == '4'){
      roboclaw.SpeedM1(address, 0);
      roboclaw.SpeedM2(address, 0);
    }

  }
}
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <Wire.h>

Adafruit_MPU6050 mpu;

float angleX = 0.0;
float angleY = 0.0;
float angleZ = 0.0;

unsigned long lastTime = 0;

void setup(void) {
  Serial.begin(115200);
  while (!Serial) {
    delay(10); 
  }

  Serial.println("Adafruit MPU6050 test!");

  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_21_HZ);

  Serial.println("");
  delay(100);
}

void loop() {
  sensors_event_t a, g, temp;

  if (!mpu.getEvent(&a, &g, &temp)) {
    Serial.println("Failed to get sensor event");
    return;
  }

  unsigned long currentTime = millis();
  float deltaTime = (currentTime - lastTime) / 1000.0; // Convert to seconds
  lastTime = currentTime;

  angleX += g.gyro.x * deltaTime;
  angleY += g.gyro.y * deltaTime;
  angleZ += g.gyro.z * deltaTime;

  Serial.print("Angle X: ");
  Serial.print(angleX);
  Serial.print(", Y: ");
  Serial.print(angleY);
  Serial.print(", Z: ");
  Serial.print(angleZ);
  Serial.println(" radians");

  Serial.print("Acceleration X: ");
  Serial.print(a.acceleration.x);
  Serial.print(", Y: ");
  Serial.print(a.acceleration.y);
  Serial.print(", Z: ");
  Serial.print(a.acceleration.z);
  Serial.println(" m/s^2");

  Serial.print("Rotation X: ");
  Serial.print(g.gyro.x);
  Serial.print(", Y: ");
  Serial.print(g.gyro.y);
  Serial.print(", Z: ");
  Serial.print(g.gyro.z);
  Serial.println(" rad/s");

  Serial.print("Temperature: ");
  Serial.print(temp.temperature);
  Serial.println(" degC");

  Serial.println("");
  delay(1500); // Delay for 500 milliseconds
}

However, with the following code, when I click "4" the accelerometer returns a value and I get the debug error "Failed to send command to RoboClaw".

#include <Keypad.h>
#include "RoboClaw.h"
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <Wire.h>

RoboClaw roboclaw(&Serial1, 10000);

#define address 0x80

const byte ROWS = 4;
const byte COLS = 3;

char keys[ROWS][COLS] = {
  {'1', '2', '3'},
  {'4', '5', '6'},
  {'7', '8', '9'},
  {'*', '0', '#'}
};
byte rowPins[ROWS] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3};

Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);

Adafruit_MPU6050 mpu;

void setup() {
  Serial.begin(115200);
  Serial1.begin(38400);  // RoboClaw
  roboclaw.begin(38400);
  
  if (!mpu.begin()) {
    Serial.println("Failed to find MPU6050 chip");
    while (1) {
      delay(10);
    }
  }
  Serial.println("MPU6050 Found!");
}

void loop() {
  char key = keypad.getKey();
  
  if (key) {
    Serial.print("Key pressed: ");
    Serial.println(key);
    
    if (key == '4') {
      sensors_event_t a, g, temp;
      mpu.getEvent(&a, &g, &temp);
      
      Serial.print("Accel X: ");
      Serial.print(a.acceleration.x);
      Serial.print(", Y: ");
      Serial.print(a.acceleration.y);
      Serial.print(", Z: ");
      Serial.print(a.acceleration.z);
      Serial.println(" m/s^2");
      
      bool success = roboclaw.SpeedM1M2(address, 400, 400);
      if (success) {
        Serial.println("Motors moving forward");
      } else {
        Serial.println("Failed to send command to RoboClaw");
      }
    }
  }
  
  delay(10);
}

If there is any issues with the code, possible issues electronically, or anything else please tell.

7 posts - 2 participants

Read full topic


Viewing all articles
Browse latest Browse all 1146

Trending Articles