Hi everyone! I'm pretty new to Arduino and this is my first project.
I am working on a school project to control the positioning of a swashplate in an RC helicopter.
The 'Cyclic' or 3D dimension tilt of the plate, and the 'Collective' or vertical position are controlled by the positions of two separate joysticks.
the joysticks are controlling 4 servos arranged in a cross configuration; the joysticks are mapped so that their position relates to a min and max of a servo.
e.g.
When the Cyclic joystick is tilted forward, back, left, or right the Swashplate tilts accordingly.
(Cyclic modifies the positions of two opposite servos per axis)
When the Collective joystick is tilted forward or back the Swashplate moves up or down.
(Collective modifies the positions of all 4 servos at a time)
The Collective and Cyclic angles are calculated and then combined into the output variable that is fed into the servo.
frontGroup.frontOutput = frontCyclic.frontServo + colValue; // output angle for front servo
e.g
(output = 65+10)
I choose this approach because making the movements independent is beyond me. Additionally, I think this would allow for more consistent and smooth control.
e.g
The Cyclic could tilt all the way forward, then whilst retaining the tilt move up and down.
In the same vein as the Cyclic or Collective change, this method should yield smooth motion.
It is worth noting that I haven't tested this and this remains theoretical for now.
Currently, I am managing these variables with struct
's and int
's but I was wondering if it would be better to use an array
or different data type to manage them that would contribute to better readability and further expansion of servo groups? (Admittedly the struct
s do a pretty good job as it is)
I am planning on eventually setting up some sort of wireless control and I would imagine that an array would lend itself well to communication, and it might be best to leave the changing of data structures until that point.
here's the code:
#include<Servo.h>
Servo frontServo1; //declare servo objects
Servo backServo1;
Servo leftServo1;
Servo rightServo1;
void setup() {
Serial.begin(9600);
//attach servos to pwm pin(x)
frontServo1.attach(3);
backServo1.attach(5);
leftServo1.attach(6);
rightServo1.attach(9);
}
void loop() {
//collective value
int colValue = map(analogRead(A3), 0, 1023, -15, 15); // '0' from joystick should turn servo all the way down
//struct for Cyclic
struct Cyclic {
int frontServo = map(analogRead(A0), 0, 1023, 75, -75); // '0' from joystick should turn servo all the way up
int backServo = map(analogRead(A0), 0, 1023, -75, 75); // '0' from joystick should turn servo all the way down
int leftServo = map(analogRead(A1), 0, 1023, -75, 75); // '0' from joystick should turn servo all the way down
int rightServo = map(analogRead(A1), 0, 1023, 75, -75); // '0' from joystick should turn servo all the way up
};
Cyclic frontCyclic; // create a struct variable for front group
//Cyclic backCyclic; // create a struct varaible for back group
// struct for output angles
struct outputAngle {
int frontOutput;
int backOutput;
int leftOutput;
int rightOutput;
};
outputAngle frontGroup; // create a struct outputAngle variable for the 4 front servos
//outputAngle backGroup; // create a struct outputAngle variable for the 4 back servos
//define values for the front group
frontGroup.frontOutput = frontCyclic.frontServo + colValue; // output angle for front servo
frontGroup.backOutput = frontCyclic.backServo + colValue; // output angle for the back servo
frontGroup.leftOutput = frontCyclic.leftServo + colValue; // output angle for the left servo
frontGroup.rightOutput = frontCyclic.rightServo + colValue; // output angle for the right servo
// forward the output values into the front servos;
frontServo1.write(frontGroup.frontOutput);
backServo1.write(frontGroup.backOutput);
leftServo1.write(frontGroup.leftOutput);
rightServo1.write(frontGroup.rightOutput);
/*
Relationships
Cylic angle + collective angle = output control angle (per servo)
frontServoCyc + colValue = frontServoOutput (could use struct!)
X-Axis
------------------------------------------------------
Xvalue map 0-1023
frontServoCyc linear-negative, so that 1023 is min angle
backServoCyc linear-positive, so that 1023 is max angle
Y-Axis
------------------------------------------------------
Yvalue map 0-1023
leftServoCyc linear-positive, so that 1023 is max angle
backSeroCyc linear-negative, so that 1023 is min angle
Collective
------------------------------------------------------
(Joystick 2's x-axis)
Xvalue2 map 0-1023
*collective modifies all of the output variables
colValue linear-positive, so that 0 is max and 1023 is min
*/
}
I appreciate any advice or criticism!