# [HELP NEEDED] Controlling 3-wheel omniwheel robot

Hi, I am making a 3-wheels Omniwheel robot to move in a certain pattern.

Below is my code for controlling the robot:

``````#define motor1A 3
#define motor1C 5
#define motor2A 6
#define motor2C 9
#define motor3A 10
#define motor3C 11
#define clkwise 0
#define anticlkwise 1
#define M1
#define M2
#define M3

int halt = 0; // how bright the LED is
int incAmount = 5; // how many points to fade the LED by
String inputString = ""; // a string to hold incoming data
boolean stringComplete = false; // whether the string is complete
void setup() {
pinMode(motor1A, OUTPUT);
pinMode(motor1C, OUTPUT);
pinMode(motor2A, OUTPUT);
pinMode(motor2C, OUTPUT);
pinMode(motor3A, OUTPUT);
pinMode(motor3C, OUTPUT);

Serial.begin(9600);
// reserve 200 bytes for the inputString:
inputString.reserve(20);
}

void loop() {
forward(200, 1000);
}
``````

Below is the code for controlling the individual motor speed and direction:

``````#define cos60 0.5
#define sin60 0.866
float a1_sp;
float a2_sp;
float a3_sp;
char a1_speed;
char a2_speed;
char a3_speed;

void control(char robospeed, float roboangle, char roborotate )  {
// assign the correct output pin for motor_num

a1_sp= cos60*robospeed*cos(roboangle)- sin60*robospeed*sin(roboangle);
a2_sp= 1*robospeed*cos(roboangle);
a3_sp= cos60*robospeed*cos(roboangle)+ sin60*robospeed*sin(roboangle);

#ifdef M1
if (a1_sp > 0) {
a1_speed = a1_sp+roborotate;
motor(1, a1_speed, anticlkwise);
}
else {
a1_sp *=-1;
a1_speed = a1_sp+roborotate;
motor(1, a1_speed, clkwise);
}

#endif

#ifdef M2
if (a2_sp > 0) {
a2_speed = a2_sp+roborotate;
motor(2, a2_speed, anticlkwise);
}
else {
a2_sp *=-1;
a2_speed = a2_sp+roborotate;
motor(2, a2_speed, clkwise);
}

#endif

#ifdef M3
if (a3_sp > 0) {
a3_speed = a3_sp+roborotate;
motor(3, a3_speed, anticlkwise);
}
else {
a3_sp *=-1;
a3_speed = a3_sp+roborotate;
motor(3, a3_speed, clkwise);
}
#endif

}
``````

Below is the code for setting the speed and direct control of a specific motor

``````void motor(char motor_num, char speed_val, boolean direct )  {
char motorLA;
char motorLC;
// assign the correct output pin for motor_num

switch(motor_num) {
case 1:
motorLA = motor1A;
motorLC = motor1C;
break;

case 2:
motorLA = motor2A;
motorLC = motor2C;
break;

case 3:
motorLA = motor3A;
motorLC = motor3C;
break;
}

if (direct) { // direct = 0:clockwise  direct = 1:anti-clockwise
analogWrite(motorLA, 0);
analogWrite(motorLC, speed_val);
}
else {
analogWrite(motorLC, 0);
analogWrite(motorLA, speed_val);
}

}
``````

Below is the code for moving the robot forward:

``````void forward(int speedval, int time)
{
control (speedval, 3.141*0.5, 0);
delay(time);
}
``````

So, the above codes will be run together and allows the robot to move forward. But how do I move the robot to move in triangular waveform for 10 seconds?

Thank you.

But how do I move the robot to move in triangular waveform for 10 seconds?

Program it properly.

PaulS:

But how do I move the robot to move in triangular waveform for 10 seconds?

Program it properly.

lol

Do you mean it has to follow a sawtooth-like path, or it has to describe the perimeter of a triangle?

AWOL: Do you mean it has to follow a sawtooth-like path, or it has to describe the perimeter of a triangle?

A sawtooth-like pattern. Example like this: Questions : Have you made the robot move forward for a fixed time at a fixed speed then stop ? Have you made the robot turn 90 degrees right or left then stop ?

If you have then you know how to move it in the pattern that you describe

``````move forward
stop
spin right
stop
move forward
stop
spin left
stop
go back and do it over again
``````

If not, then start by getting it to move forward a fixed amount and stop. Write the code to do it, try it out and if it does not work post it here for help.

HINT - the forward(), control() and motor() functions need to be in your script. They are not there in the script you posted although you did post them separately.

If not, then start by getting it to move forward a fixed amount and stop.

Keep in mind that just running the motor(s) for some amount of time does not equate to moving a fixed distance. If you don't have feedback for the distance moved, you will just be guessing as to the distance.

"Close enough for Jazz" Worth bearing in mind, certainly, but refinements can come later.