#include <PPM.h>
#include <Servo.h>
#define CHANNELS 8 // max ppm channels
#define PPM_PIN 2 // receiver ppm pin
#define Thr1 3 //Thruster 1 pin
#define Thr2 4 //Thruster 2 pin
#define Thr3 5 //Thruster 3 pin
#define Thr4 6 //Thruster 4 pin
#define UDThr 7 //UD Thruster pin
//xxxxPWMCtrl: Digital Out to relay: Low to energize relay to Normally Open contact = Motor Reverse Direction...//
//xxxxPWMCtrl: Digital Out to relay: High-- relay is "off"-- Normally Closed contact = Motor Forward Direction...//
#define Thr1PWMCtrl 50 //Allow 1 PWM out to control direction via relay switching
#define Thr2PWMCtrl 51 //Allow 1 PWM out to control direction via relay switching
#define Thr3PWMCtrl 52 //Allow 1 PWM out to control direction via relay switching
#define Thr4PWMCtrl 53 //Allow 1 PWM out to control direction via relay switching
#define UDThrPWMCtrl 49 //Allow 1 PWM out to control direction via relay switching
Servo tilt; // create servo object to control a servo
int ch1;
int ch2;
int ch3;
int ch4;
int ch5;
int ch6;
int ch7;
int ch8;
int tiltval; // variable to read the value from the analog pin
void setup() {
ppm.begin(PPM_PIN, CHANNELS);
tilt.attach(11); // attaches the servo on pin 11 to the servo object
pinMode(Thr1, OUTPUT);
pinMode(Thr2, OUTPUT);
pinMode(Thr3, OUTPUT);
pinMode(Thr4, OUTPUT);
pinMode(UDThr, OUTPUT);
pinMode(Thr1PWMCtrl, OUTPUT);
pinMode(Thr2PWMCtrl, OUTPUT);
pinMode(Thr3PWMCtrl, OUTPUT);
pinMode(Thr4PWMCtrl, OUTPUT);
pinMode(UDThrPWMCtrl, OUTPUT);
Serial.begin(9600);
}
void LeftThrusters() {
int motorSpeed;
if (ch3 < 120) { //set motor dir for Down
// speed proportional to stick dist from neutral
// map(channel, neut, low or high, 0, 255)
motorSpeed = map(ch3, 127, 0, 0, 255);
if (motorSpeed>255) motorSpeed=255; //over is bad
analogWrite(Thr1, motorSpeed);
analogWrite(Thr4, motorSpeed);
digitalWrite(Thr1PWMCtrl, LOW); //Setting LOW turns relay "on" for Reverse Direction of thruster. Allows use of ONE PWM signal to IBT_2 motor controller.
digitalWrite(Thr4PWMCtrl, LOW); //Each of the 4 horizontal thruster motors, has it's own IBT_2 motor controller.
} else if (ch3 > 134) { //Each IBT_2 requires a signal for both right and left (forward and reverse in my case) The relays allow me to use 1 signal wire to each motor controller. "My attempt to save PWM pins for future expansion"
motorSpeed = map(ch3, 127, 254, 0, 255);
if (motorSpeed>255) motorSpeed=255; //over is bad
analogWrite(Thr1, motorSpeed);
analogWrite(Thr4, motorSpeed);
digitalWrite(Thr1PWMCtrl, HIGH); //Setting HIGH leaves relay "off" for Forward Direction of thruster. Allows use of ONE PWM signal to IBT_2 motor controller.
digitalWrite(Thr4PWMCtrl, HIGH);
}
else {
analogWrite(Thr1, 0);
analogWrite(Thr4, 0);
digitalWrite(Thr1PWMCtrl, HIGH);
digitalWrite(Thr4PWMCtrl, HIGH);
}
}
void RightThrusters() {
int motorSpeed;
if (ch2 < 120) {
// speed proportional to stick dist from neutral
motorSpeed = map(ch2, 127, 0, 0, 255);
if (motorSpeed>255) motorSpeed=255; //over is bad
analogWrite(Thr2, motorSpeed);
analogWrite(Thr3, motorSpeed);
digitalWrite(Thr2PWMCtrl, LOW);
digitalWrite(Thr3PWMCtrl, LOW);
} else if (ch2 > 134) {
motorSpeed = map(ch2, 127, 254, 0, 255);
if (motorSpeed>255) motorSpeed=255; //over is bad
analogWrite(Thr2, motorSpeed);
analogWrite(Thr3, motorSpeed);
digitalWrite(Thr2PWMCtrl, HIGH);
digitalWrite(Thr3PWMCtrl, HIGH);
} else {
analogWrite(Thr2, 0);
analogWrite(Thr3, 0);
digitalWrite(Thr2PWMCtrl, HIGH);
digitalWrite(Thr3PWMCtrl, HIGH);
}
}
void Strafe() {
int motorSpeed;
if (ch4 < 120) { //set motor dir for Strafe Left
// speed proportional to stick dist from neutral
motorSpeed = map(ch4, 127, 0, 0, 255);
if (motorSpeed>255) motorSpeed=255; //over is bad
analogWrite(Thr1, motorSpeed);
analogWrite(Thr2, motorSpeed);
analogWrite(Thr3, motorSpeed);
analogWrite(Thr4, motorSpeed);
digitalWrite(Thr1PWMCtrl, LOW);
digitalWrite(Thr2PWMCtrl, HIGH);
digitalWrite(Thr3PWMCtrl, LOW);
digitalWrite(Thr4PWMCtrl, HIGH);
} else if (ch4 > 134) { //set motor dir for Strafe Right
motorSpeed = map(ch4, 127, 254, 0, 255);
if (motorSpeed>255) motorSpeed=255; //over is bad
analogWrite(Thr1, motorSpeed);
analogWrite(Thr2, motorSpeed);
analogWrite(Thr3, motorSpeed);
analogWrite(Thr4, motorSpeed);
digitalWrite(Thr1PWMCtrl, HIGH);
digitalWrite(Thr2PWMCtrl, LOW);
digitalWrite(Thr3PWMCtrl, HIGH);
digitalWrite(Thr4PWMCtrl, LOW);
} else {
analogWrite(Thr1, 0);
analogWrite(Thr2, 0);
analogWrite(Thr3, 0);
analogWrite(Thr4, 0);
digitalWrite(Thr1PWMCtrl, HIGH);
digitalWrite(Thr2PWMCtrl, HIGH);
digitalWrite(Thr3PWMCtrl, HIGH);
digitalWrite(Thr4PWMCtrl, HIGH);
}
}
// void UDThruster() {
// int motorSpeed;
// if (ch1 < 120) { //set motor dir for Down
// // speed proportional to stick dist from neutral
// motorSpeed = map(ch1, 128, 0, 0, 255);
// if (motorSpeed>255) motorSpeed=255; //over is bad
// analogWrite(UDThr, motorSpeed);
// digitalWrite(UDThrPWMCtrl, LOW);
// } else if (ch1 > 134) { //set motor dir for Up
// motorSpeed = map(ch1, 128, 254, 0, 255);
// if (motorSpeed>255) motorSpeed=255; //over is bad
// analogWrite(UDThr, motorSpeed);
// digitalWrite(UDThrPWMCtrl, HIGH);
// } else {
// analogWrite(UDThr, 0);
// digitalWrite(UDThrPWMCtrl, HIGH);
// }
//}
void tiltCam() {
tiltval = ch5;
tiltval = map(tiltval, 0, 254, 0, 75);
tilt.write(tiltval);
Serial.println(ch5);
delay(15);
}
void loop() { // THr1 Thr2
ch1 = ppm.getPWM(1); //PWM value for Up/Down Thrusters
ch2 = ppm.getPWM(2); //PWM value for Right Side Horizontal Thrusters (2) Thruster Layout
ch3 = ppm.getPWM(3); //PWM value for Left Side Horizontal Thrusters (2)
ch4 = ppm.getPWM(4); //PWM value for Strafe control of all Horizontal Thrusters (4) Thr4 Thr3
ch5 = ppm.getPWM(5); //SparePWM value
ch6 = ppm.getPWM(6); //SparePWM value
ch7 = ppm.getPWM(7); //SparePWM value
ch8 = ppm.getPWM(8); //SparePWM value
LeftThrusters();
RightThrusters();
Strafe();
//UDThruster();
//tiltCam();
}