That is a very cool project!
I am trying to use my arduino as a sensor that sends commands to the Orion (which has 4 mecanum wheels with encoder motors). I’ve managed to get the two boards communicating with I2C, but I have a new problem. They send/receive with no issue, but once I connect the motors to ports 1 and 2, the orion “hangs” and stops requesting data from the arduino. Here’s what my code looks like:
It’s just a basic test, to see if my bot will use the byte sent from the arduino to slowly accelerate forward.
#include “MeOrion.h”
#include “SoftwareSerial.h”
#include <Wire.h>
MeEncoderNew motor1(0x09, SLOT1);
MeEncoderNew motor2(0x09, SLOT2);
MeEncoderNew motor3(0x0a, SLOT1);
MeEncoderNew motor4(0x0a, SLOT2);
#define MAXSPEED 180
#define DEFAULTSPEED 100;
typedef struct {
signed int backfront;
signed int leftright;
signed int chinaHat;
}
RCDATA;
RCDATA Rcdata;
int motor1speed=0;
int motor2speed=0;
int motor3speed=0;
int motor4speed=0;
int moveSpeed = DEFAULTSPEED;
unsigned char stop_flag=0;
int Matrix[4]={1,-1,1,-1};//this array is set to motor’s direction
void parseJoystick(int buf[])
{
Rcdata.leftright = buf[0]; //control receive data
Rcdata.backfront = buf[1];
Rcdata.chinaHat = buf[3];
}
void motorprocess()
{
if(Rcdata.chinaHat == 1)
{
Speed_up();
}
else if(Rcdata.chinaHat == 5)
{
Speed_down();
}
motor1speed = moveSpeed*Matrix[0];
motor2speed = moveSpeed*Matrix[1];
motor3speed = moveSpeed*Matrix[2];
motor4speed = moveSpeed*Matrix[3];
if(Rcdata.backfront >= 135 && Rcdata.backfront <= 150) // if in mid_y pos
{
if(Rcdata.leftright >= 105 && Rcdata.leftright <= 120) // if in mid_x pos
{ // begin x_y centered
if(Rcdata.chinaHat == 7)
{
TurnLeft_run(); //Spot Turn To Left
}
else if(Rcdata.chinaHat == 3) //china hat
{
TurnRight_run();//Spot Turn To Right
}
else
{
stop_flag++;
if(stop_flag>2) // if x_y centered and no hat input, stop in place
{
Stop_run(); //Release the keys/Stop motor
stop_flag=0;
}
}
} // end x_y centered, then check x displacement
else if(Rcdata.leftright < 105)
{
Left_run(); // if CH axis moved left, execute left run
}
else if(Rcdata.leftright > 120)
{
Right_run(); // if CH axis moved right, execute right run
}
}
else if(Rcdata.backfront < 135) // check y displaced forward
{
if(Rcdata.leftright >= 105 && Rcdata.leftright <= 120) // if x is centered, go forward
{
Forward_run();
}
else if(Rcdata.leftright < 105) // if stick left and up, go LeftUp
{
LeftUp_run();
}
else if(Rcdata.leftright > 120) // if stick right and up, go RightUp
{
RightUp_run();
}
}
else if(Rcdata.backfront > 150) // check y displaced backward
{
if(Rcdata.leftright >= 105 && Rcdata.leftright <= 120) // repeated as for forward displacement
{
Backward_run();
}
else if(Rcdata.leftright < 105)
{
LeftDown_run();
}
else if(Rcdata.leftright > 120)
{
RightDown_run();
}
}
}
void Forward_run()
{
motor1.runSpeed(motor1speed,0);
motor2.runSpeed(motor2speed,0);
motor3.runSpeed(motor3speed,0);
motor4.runSpeed(motor4speed,0);
}
void Backward_run()
{
motor1.runSpeed(-motor1speed,0);
motor2.runSpeed(-motor2speed,0);
motor3.runSpeed(-motor3speed,0);
motor4.runSpeed(-motor4speed,0);
}
void Right_run()
{
motor1.runSpeed(-motor1speed,0);
motor2.runSpeed(motor2speed,0);
motor3.runSpeed(motor3speed,0);
motor4.runSpeed(-motor4speed,0);
}
void Left_run()
{
motor1.runSpeed(motor1speed,0);
motor2.runSpeed(-motor2speed,0);
motor3.runSpeed(-motor3speed,0);
motor4.runSpeed(motor4speed,0);
}
void LeftUp_run()
{
motor1.runSpeed(motor1speed,0);
motor2.runSpeed(0,0);
motor3.runSpeed(0,0);
motor4.runSpeed(motor4speed,0);
}
void LeftDown_run()
{
motor1.runSpeed(0,0);
motor2.runSpeed(-motor2speed,0);
motor3.runSpeed(-motor3speed,0);
motor4.runSpeed(0,0);
}
void RightUp_run()
{
motor1.runSpeed(0,0);
motor2.runSpeed(motor2speed,0);
motor3.runSpeed(motor3speed,0);
motor4.runSpeed(0,0);
}
void RightDown_run()
{
motor1.runSpeed(-motor1speed,0);
motor2.runSpeed(0,0);
motor3.runSpeed(0,0);
motor4.runSpeed(-motor4speed,0);
}
void TurnRight_run()
{
motor1.runSpeed(-motor1speed,0);
motor2.runSpeed(motor2speed,0);
motor3.runSpeed(-motor3speed,0);
motor4.runSpeed(motor4speed,0);
}
void TurnLeft_run()
{
motor1.runSpeed(motor1speed,0);
motor2.runSpeed(-motor2speed,0);
motor3.runSpeed(motor3speed,0);
motor4.runSpeed(-motor4speed,0);
}
void Speed_up()
{
moveSpeed +=2;
if(moveSpeed>MAXSPEED) moveSpeed=MAXSPEED;
}
void Speed_down()
{
moveSpeed -= 2;
if(moveSpeed<0) moveSpeed=0;
}
void Stop_run()
{
motor1.runSpeed(0,1);
motor2.runSpeed(0,1);
motor3.runSpeed(0,1);
motor4.runSpeed(0,1);
}
void setup() {
delay(10);
motor1.begin();
motor2.begin();
motor3.begin();
motor4.begin();
delay(10);
motor1.setMode(2); //0:I2C_MODE;1:PWM_MODE;2:PWM_I2C_PWM;
motor2.setMode(2);
motor3.setMode(2); //0:I2C_MODE;1:PWM_MODE;2:PWM_I2C_PWM;
motor4.setMode(2);
motor1.runSpeed(0);
motor2.runSpeed(0);
motor3.runSpeed(0);
motor4.runSpeed(0);
Wire.begin();
Serial.begin(9600); // start serial for output
Serial.println(“Begin Orion receive:”);
}
void loop() {
Wire.requestFrom(4, 1); // request 1 bytes from device 4
while (Wire.available()) {
int x = Wire.read(); // receive byte as an integer
int buf[] = {0,x,0,0};
parseJoystick(buf);
Serial.println(buf[1]); // print the integer
}
delay(500);
motorprocess();
}
Here’s the code from the arduino:
#include <Arduino.h>
#include <SoftwareSerial.h>
#include <Wire.h>
void setup() {
Wire.begin(4);
Serial.begin(9600);
Serial.println(“Begin Mega sender test”);
Wire.onRequest(requestEvent);
}
byte x = 0;
void loop() {
Serial.println(“Begin Loop”);
delay(100);
}
void requestEvent()
{
Serial.println(x);
Wire.write(x);
x++;
}
What do you think the problem could be?