Xbox controller( redgear actually) disconnect from usb hostshield while using BLDC motor on Arduino mega

Thread Starter

AKNO

Joined Jan 15, 2023
12
I am working with BLDC motors on my bot while using them or controlling them with a Redgear controller, the get disconnected from my host shield or Arduino mega I tried changing pins for BLDC but it didn't work i also tried Arduino DUE but the same issue arise.
Here is my code:

/*Bot Motor position
M1/-----------\M2
| |
| |
| |
M3 \-----------/M4
*/

/-----Including dependencies-----/
#include <XBOXUSB.h>
#include<Servo.h>
#include<SPI.h>
#ifdef dobogusinclude
#include <spi4teensy3.h>
#endif

Servo b1, b2;

//======PNUMATIC=======//
#define pnmCenter 33

//====LINEAR ACT. PIN====//
#define linAct_DIR 35
#define linAct_PWM 45
#define linPot A0

//===BLDC PWM Pins=====//
#define b1_PWM 44
#define b2_PWM 46
/-----Defining pinouts for Motor pins-----/
#define m2c 26 //direction
#define m2a 6 //pwm

#define m1c 28 //direction
#define m1a 7 //pwm

#define m3c 30 //directionz
#define m3a 8 //pwm

#define m4c 32 //direction
#define m4a 9 //pwm


/Defining paramters related to speed and Direction of Omni/Mechanum wheel/
//====PWM Parameters====//
#define slow_speed 150 //For allignment of chassis Press 'A' button to toggle between speeds
#define max_speed 240
int motor_maxspeed = 240; //Default set at max_speed

//==BLDC Speed constants==//
#define bldc_spd1 40
#define bldc_spd2 50
#define bldc_spd3 60
#define bldc_spd4 70

//==Direction Parameters==//
#define forward LOW
#define backward HIGH

/Defining the DeadZone values for X and Y axis(+ve and -ve)/
#define xPositive 7500
#define xNegative -7500
#define yPositive 7500
#define yNegative -7500

/Initialsing Global objects/
USB Usb;
XBOXUSB Xbox(&Usb);//Creating an object "XBOX" to access the inputs from REDGEAR


void setup() {
//Setting the BAUD rate for Serial Communication
Serial.begin(115200);//Set to max Baud rate(bits/sec)
//Also can be set to 9600 baud rate

//Check whether USB connection established
if (Usb.Init() == -1) {
Serial.println("USB communication did not initialise properly");
Serial.println("Try reseting the microcontroller using reset button");
while (1); //Infinite loop because Usb not initialised properly
}

Serial.println("XBOX Connection established!!!");

//Configuring the Direction and PWM pins as Output
pinMode(m1a, OUTPUT);
pinMode(m1c, OUTPUT);
pinMode(m2a, OUTPUT);
pinMode(m2c, OUTPUT);
pinMode(m3a, OUTPUT);
pinMode(m3c, OUTPUT);
pinMode(m4a, OUTPUT);
pinMode(m4c, OUTPUT);
pinMode(linAct_DIR, OUTPUT);
pinMode(linAct_PWM, OUTPUT);
pinMode(pnmCenter, OUTPUT);
pinMode(linPot,INPUT);

}


void loop() {
Usb.Task();//polls connected usb devices for updates to their status
//NB : If there is no activity on a connected USB device, task() will block all other calls for 5 second intervals.

if (Xbox.Xbox360Connected) {
//Set Rumble ON
Xbox.setRumbleOn(0, 0);

if (Xbox.getButtonClick(UP)) {
centerShoot(bldc_spd1);
}
if (Xbox.getButtonClick(RIGHT)) {
centerShoot(bldc_spd2);
Serial.println(F("Right"));
}
if (Xbox.getButtonClick(DOWN)) {
centerShoot(bldc_spd3);
Serial.println(F("Down"));
}
if (Xbox.getButtonClick(LEFT)) {
centerShoot(bldc_spd4);
Serial.println(F("Left"));
}

if (Xbox.getButtonClick(XBOX)) {
Xbox.setLedMode(ROTATING);
Serial.println(F("Xbox"));
}
if (Xbox.getButtonClick(B)) {
centerShoot(0);
}
if (Xbox.getButtonPress(X)) {
pnmOn();
} else if (!Xbox.getButtonPress(X)) {
pnmOff();
}
if (Xbox.getButtonClick(Y)) {
calibrate();
}
if (Xbox.getButtonPress(L1)) {
linAct(1, 255);
}
if (Xbox.getButtonPress(L2)) {
linAct(0, 255);
}
else if (!Xbox.getButtonPress(L2) && !Xbox.getButtonPress(L1)) {
linAct(0, 0);
}

//Default or Initial values PWM and Direction values
int mot1PWM = 0;
bool mot1DIR = forward;
int mot2PWM = 0;
bool mot2DIR = forward;
int mot3PWM = 0;
bool mot3DIR = forward;
int mot4PWM = 0;
bool mot4DIR = forward;

//Checking for input from Right and Left Hats
if ( Xbox.getAnalogHat(RightHatX) > xPositive || Xbox.getAnalogHat(RightHatX) < xNegative || Xbox.getAnalogHat(RightHatY) > yPositive || Xbox.getAnalogHat(RightHatY) < yNegative || Xbox.getAnalogHat(LeftHatX) > xPositive || Xbox.getAnalogHat(LeftHatX) < xNegative || Xbox.getAnalogHat(LeftHatY) > yPositive || Xbox.getAnalogHat(LeftHatY) < yNegative) {
mot1PWM = -constrain(Xbox.getAnalogHat(LeftHatY), -32767, 32767) - constrain(Xbox.getAnalogHat(LeftHatX), -32767, 32767) - constrain(Xbox.getAnalogHat(RightHatX), -32767, 32767);
mot2PWM = constrain(Xbox.getAnalogHat(LeftHatY), -32767, 32767) - constrain(Xbox.getAnalogHat(LeftHatX), -32767, 32767) - constrain(Xbox.getAnalogHat(RightHatX), -32767, 32767);
mot3PWM = constrain(Xbox.getAnalogHat(LeftHatY), -32767, 32767) + constrain(Xbox.getAnalogHat(LeftHatX), -32767, 32767) - constrain(Xbox.getAnalogHat(RightHatX), -32767, 32767);
mot4PWM = -constrain(Xbox.getAnalogHat(LeftHatY), -32767, 32767) + constrain(Xbox.getAnalogHat(LeftHatX), -32767, 32767) - constrain(Xbox.getAnalogHat(RightHatX), -32767, 32767);

if (mot1PWM < 0) {
mot1PWM *= -1;
mot1DIR = backward;
}
if (mot2PWM < 0) {
mot2PWM *= -1;
mot2DIR = backward;
}
if (mot3PWM < 0) {
mot3PWM *= -1;
mot3DIR = backward;
}
if (mot4PWM < 0) {
mot4PWM *= -1;
mot4DIR = backward;
}

//Scalling the values to PWM
mot1PWM = map(mot1PWM, 0, 32767, 0, motor_maxspeed);
mot2PWM = map(mot2PWM, 0, 32767, 0, motor_maxspeed);
mot3PWM = map(mot3PWM, 0, 32767, 0, motor_maxspeed);
mot4PWM = map(mot4PWM, 0, 32767, 0, motor_maxspeed);
}

//Passing PWM and Direction to pins
analogWrite(m1a, mot1PWM);
digitalWrite(m1c, mot1DIR);
analogWrite(m2a, mot2PWM);
digitalWrite(m2c, mot2DIR);
analogWrite(m3a, mot3PWM);
digitalWrite(m3c, mot3DIR);
analogWrite(m4a, mot4PWM);
digitalWrite(m4c, mot4DIR);

//For toggling between max and slow speeds
if (Xbox.getButtonClick(A)) {
motor_maxspeed = motor_maxspeed == max_speed ? slow_speed : max_speed;
Serial.println("Setting max PWM as " + motor_maxspeed);
}

delay(1);
}
}
//===BLDC Calibration===//
void calibrate() {
b1.attach(b1_PWM);
b1.attach(b2_PWM);
b1.write(30);
b2.write(30);
delay(3000); //ESC initialization delay.
}
//==========LinAct=========//
void linAct(int a, int pwm) {
if (a == 1 && pwm > 0)Serial.println("up");
if (a == 0 && pwm > 0)Serial.println("down");
if (pwm == 0)Serial.println("stop");
Serial.print("");
if (a == 0)digitalWrite(linAct_DIR, LOW);
else digitalWrite(linAct_DIR, HIGH);
if (pwm == 255)digitalWrite(linAct_PWM, HIGH);
else digitalWrite(linAct_PWM, LOW);
//==Linear Pot reading===//

}
//=========PNEUMATIC=======//
void pnmOn() {
Serial.println("pnmOn");
digitalWrite(pnmCenter, LOW);
}

void pnmOff() {
Serial.println("pnmOFF");
digitalWrite(pnmCenter, HIGH);
}
//=========BLDC Center Shoot=========//
void centerShoot(int x) {
b1.write(x);
delay(5);
b2.write(x);
delay(5);
}
 
Last edited:
Top