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.