RCU Forums - View Single Post - Open Source Battle System
View Single Post
Old 12-21-2009 | 12:56 AM
  #2  
THE_BREED
Senior Member
 
Joined: Apr 2007
Posts: 152
Likes: 0
Received 0 Likes on 0 Posts
From: , OH
Default RE: Open Source Battle System

Here is the code I have thus far. Anyone super familiar with programing Arduinos.



#include "servo.h"

///////////////////////////////////////////////////////////////////////////////////////
// PIN ASSIGNMENTS //
///////////////////////////////////////////////////////////////////////////////////////

// RC RECEIVER SERVO INPUT PINS
const int RawThrottleIn = ?; // RC receiver Throttle pin
const int RawSteeringIn = ?; // RC receiver Steering pin
const int RawAzimithIn= ?; // RC receiver Azimith pin
const int RawElevationIn = ?; // RC receiver Elevation pin
const int RawStartStopStabIn = ?; // RC receiver Start-Stop/Stabilization pin
const int RawMGMaingunMissleIn = ?; // RC receiver Machine gun-Maingun/Missle pin
const int RawPivotSteerIn = ?; // RC receiver Pivot Steer pin
const int RawCITVIn = ?; // RC receiver CITV pin
const int RawGyroIn = ?; // RC receiver Gyro pin

// SWITCH INPUT PINS
const int FrontIrIn = ?; // Front Infrared Receiver pin
const int SideIrIn = ?; // Side Infrared Receivers pin
const int BackIrIn = ?; // Back Infrared Receiver pin
const int HullIrIn = ?; // Hull Infrared Receiver pin
const int DeckClearanceIn = ?; // Deck Clearance Switch pin

// ANALOG INPUT PINS
const int xIn = ?; // x-axis of the accelerometer pin
const int yIn = ?; // y-axis of the accelerometer pin

// ON BOARD ANALOG CONTROL PINS
const int RecoilPotIn = ?; // Recoil potentiometer pin
const int MaxForwardPotIn = ?; // Forward potentiometer pin
const int MaxReversePotIn = ?; // Reverse potentiometer pin
const int MaxAzimithPotIn = ?; // Azimith potentiometer pin
const int MaxPivotPotIn = ?; // Pivot potentiometer pin
const int CITVRatioPotIn = ?; // CITV potentiometer pin
const int ElevationRatioPotIn = ?; // Elevation potentiometer pin

// ON BOARD DIGITAL CONTROL PINS
const int StabSwitchIn = ?; // Stabilization Switch pin
const int MomentumSwitchIn = ?; // Momentum Switch pin
const int SelectSwitch1In = ?; // Vehicle selection switch 1 pin
const int SelectSwitch2In = ?; // Vehicle selection switch 2 pin
const int SelectSwitch3In = ?; // Vehicle selection switch 3 pin
const int SelectSwitch4In = ?; // Vehicle selection switch 4 pin
const int SelectSwitch5In = ?; // Vehicle selection switch 5 pin
const int SelectSwitch6In = ?; // Vehicle selection switch 6 pin
const int SelectSwitch7In = ?; // Vehicle selection switch 7 pin
const int SelectSwitch8In = ?; // Vehicle selection switch 8 pin

// SERVO OUTPUT PINS
const int LeftTrackOut = ?; // Left Track output pin
const int RightTrackOut = ?; // Right Track output pin
const int AzimithOut = ?; // Azimith output pin
const int ElevationOut = ?; // Elevation output pin
const int RecoilOut = ?; // Recoil output pin
const int CITVOut = ?; // CITV output pin
const int GyroOut = ?; // Gyro Reference output pin
const int SmokeModuleOut = ?; // Smoke Generator output pin

//SETUP SERVOS
servo myservo1; //LeftTrack / Throttle Servo setup
servo myservo2; //RightTrack / Steering Servo setup
servo myservo3; //Azimith Servo setup
servo myservo4; //Elevation Servo setup
servo myservo5; //Recoil Servo setup
servo myservo6; //CITV Servo setup
servo myservo7; //Gyro Reference setup
servo myservo8; //Smoke Module Servo setup

// OUTPUT DRIVER PINS
const int BrakeLightsOut = ?; // Brake Lights output pin
const int MuzzleFlashOut = ?; // Muzzle Flash output pin
const int IRTransOut = ?; // Infrared Transmitter output pin
const int HitLEDSOut = ?; // Hit LEDS output pin
const int Aux1Out = ?; // Aux1 output pin
const int Aux2Out = ?; // Aux2 output pin

// COMMUNICATIONS PINS
const int SCK = ?; // SPI Clock pin
const int SDO = ?; // SPI Data Out pin
const int SDI = ?; // SPI Data In pin



///////////////////////////////////////////////////////////////////////////////////////
// TABLES //
///////////////////////////////////////////////////////////////////////////////////////

//byte Acceleration1_Table[] = {1500, 1525, 1575, 1670, 1770, 1870, 1950, 2020, 2075, 2125, 2178, 2205, 2250, 2275, 2300, 2330, 2350, 2365, 2380, 2400, 2410, 2420, 2430, 2440, 2450, 2455, 2460, 2465, 2467, 2469, 2471, 2473, 2476, 2479, 2481, 2483, 2485, 2487, 2488, 2489, 2490, 2491, 2492, 2493, 2494, 2495, 2496, 2497, 2498, 2499, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500};

//byte Acceleration2_Table[] = {1500, 1521, 1561, 1635, 1713, 1791, 1853, 1908, 1952, 1992, 2034, 2057, 2094, 2115, 2136, 2161, 2179, 2193, 2206, 2224, 2234, 2244, 2254, 2264, 2274, 2280, 2286, 2293, 2297, 2301, 2305, 2308, 2313, 2318, 2322, 2326, 2330, 2334, 2337, 2340, 2344, 2347, 2350, 2353, 2357, 2360, 2363, 2366, 2369, 2373, 2376, 2378, 2381, 2383, 2386, 2388, 2391, 2393, 2396, 2398, 2401, 2403, 2406, 2408, 2411, 2413, 2416, 2418, 2421, 2423, 2426, 2428, 2431, 2433, 2435, 2438, 2440, 2443, 2445, 2448, 2450, 2453, 2455, 2458, 2460, 2463, 2465, 2468, 2470, 2473, 2475, 2478, 2480, 2483, 2485, 2488, 2490, 2493, 2495, 2498, 2500};

//byte Acceleration3_Table[] = {1500, 1518, 1548, 1600, 1655, 1710, 1755, 1795, 1828, 1858, 1889, 1908, 1935, 1953, 1970, 1990, 2005, 2018, 2030, 2045, 2055, 2065, 2075, 2085, 2095, 2103, 2110, 2118, 2124, 2130, 2136, 2142, 2148, 2155, 2161, 2167, 2173, 2179, 2184, 2190, 2195, 2201, 2206, 2212, 2217, 2223, 2228, 2234, 2239, 2245, 2250, 2255, 2260, 2265, 2270, 2275, 2280, 2285, 2290, 2295, 2300, 2305, 2310, 2315, 2320, 2325, 2330, 2335, 2340, 2345, 2350, 2355, 2360, 2365, 2370, 2375, 2380, 2385, 2390, 2395, 2400, 2405, 2410, 2415, 2420, 2425, 2430, 2435, 2440, 2445, 2450, 2455, 2460, 2465, 2470, 2475, 2480, 2485, 2490, 2495, 2500};

//byte Acceleration4_Table[] = {1500, 1475, 1425, 1330, 1230, 1130, 1050, 980, 925, 875, 823, 795, 750, 725, 700, 670, 650, 635, 620, 600, 590, 580, 570, 560, 550, 545, 540, 535, 533, 531, 529, 527, 524, 521, 519, 517, 515, 513, 512, 511, 510, 509, 508, 507, 506, 505, 504, 503, 502, 501, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500};

//byte Acceleration5_Table[] = {1500, 1479, 1439, 1365, 1287, 1209, 1147, 1092, 1048, 1008, 966, 943, 906, 885, 864, 839, 821, 807, 794, 776, 766, 756, 746, 736, 726, 720, 714, 707, 703, 699, 695, 692, 687, 682, 678, 674, 670, 666, 663, 660, 656, 653, 650, 647, 643, 640, 637, 634, 631, 627, 624, 622, 619, 617, 614, 612, 609, 607, 604, 602, 599, 597, 594, 592, 589, 587, 584, 582, 579, 577, 574, 572, 569, 567, 565, 562, 560, 557, 555, 552, 550, 547, 545, 542, 540, 537, 535, 532, 530, 527, 525, 522, 520, 517, 515, 512, 510, 507, 505, 502, 500};

//byte Acceleration6_Table[] = {1500, 1483, 1453, 1400, 1345, 1290, 1245, 1205, 1173, 1143, 1111, 1093, 1065, 1048, 1030, 1010, 995, 983, 970, 955, 945, 935, 925, 915, 905, 898, 890, 883, 877, 871, 865, 859, 852, 846, 840, 834, 828, 822, 816, 811, 805, 800, 794, 789, 783, 778, 772, 767, 761, 756, 750, 745, 740, 735, 730, 725, 720, 715, 710, 705, 700, 695, 690, 685, 680, 675, 670, 665, 660, 655, 650, 645, 640, 635, 630, 625, 620, 615, 610, 605, 600, 595, 590, 585, 580, 575, 570, 565, 560, 555, 550, 545, 540, 535, 530, 525, 520, 515, 510, 505, 500};


///////////////////////////////////////////////////////////////////////////////////////
// VARIABLES //
///////////////////////////////////////////////////////////////////////////////////////

// RC RECEIVER SERVO INPUT VARIABLES
int ThrottleVal; // Throttle input value in microseconds
int SteeringVal; // Steering input value in microseconds
int AzimithVal; // Azimith input value in microseconds
int ElevationVal; // Elevation input value in microseconds
int StartStopStabVal; // Start-Stop/Stab value in microseconds
int MGMaingunMissleVal; // MG-Maingun/Missle value in microseconds
int PivotSteerVal; // Pivot steer value in microseconds
int CITVVal; // CITV value in microseconds
int GyroVal; // Gyro value in microseconds

// SWITCH INPUT VARIABLES
int FrontIRState; // Front infrared sensor state (High-Low)
int SideIRState; // Side infrared sensor state (High-Low)
int BackIRState; // Back infrared sensor state (High-Low)
int HullIRState; // Hull infrared sensor state (High-Low)
int DeckClearananceState; // Deck Clearance switch state (High-Low)

// ACCELEROMETER INPUT VARIABLES
int X_AxisVal; // Accelerometer X axis value
int Y_AxisVal; // Accelerometer Y axis value
int pulseX;
int pulseY;
int AccelerationX;
int AccelerationY;

// ON BOARD ANALOG CONTROL VARIABLES
int RecoilPotVal; // Recoil potentiometer value (0-1023)
int MaxForwardPotVal; // Forward potentiometer value (0-1023)
int MaxReversePotVal; // Reverse potentiometer value (0-1023)
int MaxAzimithPotVal; // Azimith potentiometer value (0-1023)
int MaxPivotPotVal; // Pivot potentiometer value (0-1023)
int CITVRatioPotVal; // CITV potentiometer value (0-1023)
int ElevationRatioPotVal; // Elevation potentiometer value (0-1023)

// ON BOARD DIGITAL CONTROL VARIABLES
int StabSwitchVal; // Stabilization Switch state (High-Low)
int MomentumSwitchVal; // Momentum switch state (High-Low)
int SelectSwitch1Val; // Select Switch 1 state (High-Low)
int SelectSwitch2Val; // Select Switch 2 state (High-Low)
int SelectSwitch3Val; // Select Switch 3 state (High-Low)
int SelectSwitch4Val; // Select Switch 4 state (High-Low)
int SelectSwitch5Val; // Select Switch 5 state (High-Low)
int SelectSwitch6Val; // Select Switch 6 state (High-Low)
int SelectSwitch7Val; // Select Switch 7 state (High-Low)
int SelectSwitch8Val; // Select Switch 8 state (High-Low)

// SERVO OUTPUT VARIABLES
int LeftTrackVal; // Left Track Speed output value in microseconds
int RightTrackVal; // Right Track Speed output value in microseconds
int AzimithVal; // Azimith output value in microseconds
int ElevationVal; // Elevation output value in microseconds
int RecoilVal; // Recoil output value in microseconds
int CITVVal; // CITV output value in microseconds
int SmokeModuleVal; // Smoke Module output value in microseconds

// OUTPUT DRIVER VARIABLES
int BrakeLightsVal = 0; // Brake Lights output State
int MuzzleFlashVal; // Muzzle Flash output State
int IRTransVal; // Infrared Transmitter output State
int HitLEDSVal; // Hit LEDS output State
int Aux1Val; // Aux1 output State
int Aux2Val; // Aux2 output State

// COMMUNICATIONS VARIABLES




//OTHER VARIABLES

int PivotPotBottomLimit; //Pivot Speed Limit Left
int PivotPotTopLimit; //Pivot Speed Limit Right

int AziPotBottomLimit; //Azimith Speed Limit Left
int AziPotTopLimit; //Azimith Speed Limit Right

int ElevationSound;
int AzimithSound;
int PivotSound;

int StabStatus = 0; //Stabilization Status initialized to off
int Start_StopVal = 0; //Startup-Shutdown value initialized to off

int Recoil_Count_Delay = 0; //Delay counter for Recoil sequence initialized to off
int Fire = 0; //Fire main gun signal from battle system initialized to off
int Firing = 0; //Main gun firing sequence initialized to off

int ForwardTractionRamp = 1; //Acceleration ramp multiplier
int ReverseTractionRamp = 1; //Acceleration ramp multiplier
int TractionRamp = 1; //Acceleration ramp multiplier
int LeftTrackMulti = 1; //Multiplier for Steering/Throttle mixing
int RightTrackMulti = 1; //Multiplier for Steering/Throttle mixing
int SmokeVal = 0; //Smoke generator system value

Int ServoCenter = 1500; //Servo calibration input signal center point in uS
Int ServoMax = 2000; //Servo calibration input signal Maximum point in uS
Int ServoMin = 1000; //Servo calibration input signal Minimum point in uS

Int ServoSwitchDeadband; //Servo signal switch calibration dead band in uS
Int ServoDeadBand; //Servo signal calibration dead band in uS
Int VehicleSelection; //Vehicle selection value to Acceleration Ramps and Battle System


///////////////////////////////////////////////////////////////////////////////////////
// SETUP //
///////////////////////////////////////////////////////////////////////////////////////

void setup()
{
Serial.begin(9600); //serial library start

pinMode(RawThrottleIn, INPUT);
pinMode(RawSteeringIn, INPUT);
pinMode(RawAzimithIn, INPUT);
pinMode(RawElevationIn, INPUT);
pinMode(RawStartStopStabIn, INPUT);
pinMode(RawMGMaingunMissleIn, INPUT);
pinMode(RawLRFPivotSteerIn, INPUT);
pinMode(RawCITVIn, INPUT);
pinMode(RawGyroIn, INPUT);

pinMode(FrontIrIn, INPUT);
pinMode(SideIrIn, INPUT);
pinMode(BackIrIn, INPUT);
pinMode(HullIrIn, INPUT);
pinMode(DeckClearanceIn, INPUT);

pinMode(xIn, INPUT);
pinMode(yIn, INPUT);

pinMode(RecoilPotIn, INPUT);
pinMode(MaxForwardPotIn, INPUT);
pinMode(MaxReversePotIn, INPUT);
pinMode(MaxAzimithPotIn, INPUT);
pinMode(MaxPivotPotIn, INPUT);
pinMode(CITVRatioPotIn, INPUT);
pinMode(ElevationRatioPotIn, INPUT);

pinMode(StabSwitchIn, INPUT);
pinMode(MomentumSwitchIn, INPUT);
pinMode(SelectSwitch1In, INPUT);
pinMode(SelectSwitch2In, INPUT);
pinMode(SelectSwitch3In, INPUT);
pinMode(SelectSwitch4In, INPUT);
pinMode(SelectSwitch5In, INPUT);
pinMode(SelectSwitch6In, INPUT);
pinMode(SelectSwitch7In, INPUT);
pinMode(SelectSwitch8In, INPUT);

myservo1.attach(LeftTrackOut);
myservo2.attach(RightTrackOut);
myservo3.attach(AzimithOut);
myservo4.attach(ElevationOut);
myservo5.attach(RecoilOut);
myservo6.attach(CITVOut);
myservo7.attach(GyroOut);
myservo8.attach(SmokeModuleOut);

pinMode(BrakeLightsOut, OUTPUT);
pinMode(MuzzleFlashOut, OUTPUT);
pinMode(IRTransOut, OUTPUT);
pinMode(HitLEDSOut, OUTPUT);
pinMode(Aux1Out, OUTPUT);
pinMode(Aux2Out, OUTPUT);

pinMode(SCK, OUTPUT);
pinMode(SDO, OUTPUT);
pinMode(SDI, INPUT);

StabSwitchVal = digitalRead(StabSwitchIn);
MomentumSwitchVal = digitalRead(MomentumSwitchIn);
SelectSwitch1Val = digitalRead(SelectSwitch1In);
SelectSwitch2Val = digitalRead(SelectSwitch2In);
SelectSwitch3Val = digitalRead(SelectSwitch3In);
SelectSwitch4Val = digitalRead(SelectSwitch4In);
SelectSwitch5Val = digitalRead(SelectSwitch5In);
SelectSwitch6Val = digitalRead(SelectSwitch6In);
SelectSwitch7Val = digitalRead(SelectSwitch7In);
SelectSwitch8Val = digitalRead(SelectSwitch8In);


//VEHICLE SELECTION CODE

If(SelectSwitch1Val = LOW && SelectSwitch2Val = LOW && SelectSwitch3Val = LOW){
VehicleSelection = 0; //WWII Ultra Light Tank
}

If(SelectSwitch1Val = HIGH && SelectSwitch2Val = LOW && SelectSwitch3Val = LOW){
VehicleSelection = 1; //WWII Light Tank
}

If(SelectSwitch1Val = LOW && SelectSwitch2Val = HIGH && SelectSwitch3Val = LOW){
VehicleSelection = 2; //WWII Medium Tank
}

If(SelectSwitch1Val = HIGH && SelectSwitch2Val = HIGH && SelectSwitch3Val = LOW){
VehicleSelection = 3; //WWII Heavy Tank
}

If(SelectSwitch1Val = LOW && SelectSwitch2Val = LOW && SelectSwitch3Val = HIGH){
VehicleSelection = 4; //Eastern MBT
}

If(SelectSwitch1Val = HIGH && SelectSwitch2Val = LOW && SelectSwitch3Val = HIGH){
VehicleSelection = 5; //Western MBT
}

If(SelectSwitch1Val = LOW && SelectSwitch2Val = HIGH && SelectSwitch3Val = HIGH){
VehicleSelection = 6; //IFV / APC / Light MBT
}

If(SelectSwitch1Val = HIGH && SelectSwitch2Val = HIGH && SelectSwitch3Val = HIGH){
VehicleSelection = 7; //Wheeled Vehicle
}

}



///////////////////////////////////////////////////////////////////////////////////////
// EXECUTEABLE CODE //
///////////////////////////////////////////////////////////////////////////////////////
void loop()
{
ThrottleVal = pulseIn(RawThrottleIn, HIGH, 20000);
SteeringVal = pulseIn(RawSteeringIn, HIGH, 20000);
AzimithVal = pulseIn(RawAzimithIn, HIGH, 20000);
ElevationVal = pulseIn(RawElevationIn, HIGH, 20000);
StartStopStabVal = pulseIn(RawStartStopStabIn, HIGH, 20000);
MGMaingunMissleVal = pulseIn(RawMGMaingunMissleIn, HIGH, 20000);
PivotSteerVal = pulseIn(RawPivotSteerIn, HIGH, 20000);
CITVVal = pulseIn(RawCITVIn, HIGH, 20000);
GyroVal = pulseIn(RawGyroIn, HIGH, 20000);

FrontIRState = digitalRead(FrontIrIn);
SideIRState = digitalRead(SideIrIn);
BackIRState = digitalRead(BackIrIn);
HullIRState = digitalRead(HullIrIn);
DeckClearanceState = digitalRead(DeckClearanceIn);

pulseX = pulseIn(xIn,HIGH);
pulseY = pulseIn(yIn,HIGH);

RecoilPotVal = analogRead(RecoilPotIn);
MaxForwardPotVal = analogRead(MaxForwardPotIn);
MaxReversePotVal = analogRead(MaxReversePotIn);
MaxAzimithPotVal = analogRead(MaxAzimithPotIn);
MaxPivotPotVal = analogRead(MaxPivotPotIn);
CITVRatioPotVal = analogRead(CITVRatioPotIn);
ElevationRatioPotVal = analogRead(ElevationRatioPotIn);



//START STOP SEQUENCE CODE

If(Start_StopVal = 1){ //Vehicle allready running
If(StartStopStabVal < 1400){ //Shutdown command
StartStopVal = 0; //Shutdown vehicle


//Shutdown Sounds //Shutdown sound sequence


Delay(10000); //Delay 10 seconds for sounds
}}

If(Start_StopVal = 0){ //Vehicle not running
If(1600 > SteerVal > 1400){ //Startup Calibration check
If(1600 > ThrottleVal > 1400){ //Startup Calibration check
If(ElevationVal < 1400){ //Startup Calibration check
If(AzimithVal >1600){ //Startup Calibration check
If(StartStopStabVal < 1400){ //Start command
StartStopVal = 1; //Start vehicle
ServoCenter = AzimithVal; //Calibration of servo input signals
ServoMax = ThrottleVal; //Calibration of servo input signals
ServoMin = SteerVal; //Calibration of servo input signals


//Startup Sounds //Startup sound sequence


Delay(10000); //Delay 18 seconds for sounds
}}}}}}

If(Start_StopVal = 1){ //Vehicle allready running, Start of Running Vehicle code


//SERVO INPUT MAPPING TO ServoCenter +/- 1000 counts

SteerVal = map(SteerVal, ServoMin, ServoMax, (ServoCenter - 1000), (ServoCenter + 1000));
ThrottleVal = map(ThrottleVal, ServoMin, ServoMax, (ServoCenter - 1000), (ServoCenter + 1000));
AzimithVal = map(AzimithVal, ServoMin, ServoMax, (ServoCenter - 1000), (ServoCenter + 1000));
ElevationVal = map(ElevationVal, ServoMin, ServoMax, (ServoCenter - 1000), (ServoCenter + 1000));



//POT INPUT MAPPING

MaxPivotPotVal = map(MaxPivotPotVal, 0, 1023, 0, 1000); //Map Pivot pot value to 0-1000 counts
MaxForwardPotVal = map(MaxForwardPotVal, 0, 1023, 0, 1000); //Map Forward pot value to 0-1000 counts
MaxReversePotVal = map(MaxReversePotVal, 0, 1023, 0, 1000); //Map Reverse pot value to 0-1000 counts
MaxAzimithPotVal = map(MaxAzimithPotVal, 0, 1023, 0, 1000); //Map Azimith pot value to 0-1000 counts
ElevationRatioPotVal= map(ElevationRatioPotVal, 0, 1023, 1, 7); //Map Elevation Ratio pot value to 1-7 counts



//BATTLE SYSTEM CODE

digitalwrite(MuzzleFlashOut, MuzzleFlashVal);
digitalwrite(IRTransOut, IRTransVal);
digitalwrite(HitLEDSOut, HitLEDSVal);



//ACCELERATION RAMPS CODE

ThrottleVal = map(ThrottleVal, ServoCenter, (ServoCenter + 1000), ServoCenter, (MaxForwardPotVal + ServoCenter)); //Forward

ThrottleVal = map(ThrottleVal, (ServoCenter - 1000), ServoCenter, (ServoCenter - MaxReversePotVal), ServoCenter); //Reverse

ThrottleVal = map(ThrottleVal, (ServoCenter - 1000), (ServoCenter + 1000), MaxReversePotVal, MaxForwardPotVal); //Wheeled

digitalwrite(BrakeLightsOut, BrakeLightsVal);



//PIVOT CODE

If(VehicleSelection != 7){ //No pivot with Wheeled vehicle
If(PivotSteerVal < (ServoCenter + ServoSwitchDeadBand)){ //Is pivot switch pressed
PivotSound = 0; //No pivot sound
}

If(PivotSteerVal > (ServoCenter + ServoSwitchDeadBand)){ //Is pivot switch pressed

PivotPotBottomLimit = ServoCenter - MaxPivotPotVal; //Set bottom limit to center - pivot pot
PivotPotTopLimit = ServoCenter + MaxPivotPotVal; //Set top limit to center + pivot pot
SteeringVal = map(SteeringVal, (ServoCenter - 1000), (ServoCenter + 1000), PivotPotBottomLimit, PivotPotTopLimit); //Map Steering value to top and bottom limits

If(SteeringVal > (ServoCenter + ServoDeadBand)){ //Pivot Steer Right
LeftTrackVal = SteeringVal; //Run Left track forward
RightTrackVal = ServoCenter - (SteeringVal - ServoCenter); //Run Right track reverse
PivotSound = 1; //Pivot sound
}

If(SteeringVal < (ServoCenter - ServoDeadBand)){ //Pivot Steer Left
LeftTrackVal = SteeringVal; //Run Left track reverse
RightTrackVal = ServoCenter + (ServoCenter - SteeringVal); //Run Right track forward
PivotSound = 1; //Pivot sound
}}}



//MIXING CODE

If(VehicleSelection != 7){ //No Mixing with Wheeled vehicle
If(ThrottleVal > ServoCenter){ //Forward Throttle

If(SteeringVal < (ServoCenter - ServoDeadBand)){ //Steer Left
LeftTrackMulti = 1 - ((SteeringVal - ServoCenter) / 1000); //Steer Multiplier for Left track % of Steerval 0-1
RightTrackVal = ForwardTractionRamp; //Right Track Value
LeftTrackVal = (ServoCenter + ((ForwardTractionRamp - ServoCenter) * LeftTrackMulti)); //Left Track Value
}

If(SteeringVal > (ServoCenter + ServoDeadBand)){ //Steer Right
RightTrackMulti = 1 - ((ServoCenter - SteeringVal) / 1000); //Steer Multiplier for Right track % of Steerval 0-1
LeftTrackVal = ForwardTractionRamp; //Left Track Value
RightTrackVal = (ServoCenter + ((ForwardTractionRamp - ServoCenter) * RightTrackMulti)); //Right Track Value
}

If((ServoCenter - ServoDeadBand) < SteeringVal < (ServoCenter + ServoDeadBand)){ //No Steer
LeftTrackVal = ForwardTractionRamp; //Left Track Value
RightTrackVal = ForwardTractionRamp; //Right Track Value
}}

Else{

If(SteeringVal < (ServoCenter - ServoDeadBand)){ //Steer Left
LeftTrackMulti = 1 - ((SteeringVal - ServoCenter) / 1000); //Steer Multiplier for Left track % of Steerval 0-1
RightTrackVal = ReverseTractionRamp; //Right Track Value
LeftTrackVal = (ServoCenter - ((ServoCenter - ReverseTractionRamp) * LeftTrackMulti)); //Left Track Value
}

If(SteeringVal > (ServoCenter + ServoDeadBand)){ //Steer Right
RightTrackMulti = 1 - ((ServoCenter - SteeringVal) / 1000); //Steer Multiplier for Right track % of Steerval 0-1
LeftTrackVal = ReverseTractionRamp; //Left Track Value
RightTrackVal = (ServoCenter - ((ServoCenter - ReverseTractionRamp) * RightTrackMulti)); //Right Track Value
}

If((ServoCenter - ServoDeadBand) < SteeringVal < (ServoCenter + ServoDeadBand)){ //No Steer
LeftTrackVal = ReverseTractionRamp; //Left Track Value
RightTrackVal = ReverseTractionRamp; //Right Track Value
}}}

Else{

RightTrackVal = SteerVal; //Wheeled Vehicle Steer Value
LeftTrackVal = TractionRamp; //Wheeled Vehicle Throttle Value
}


myservo1.writeMicroseconds(LeftTrackVal);
myservo2.writeMicroseconds(RightTrackVal);



//STABILIZATION CODE

If(StabStatus = 0){ //Stabilization Status = E-Mode
If(StabSwitchVal = HIGH){ //Stabilization Switch ON
If(StartStopStabVal > (ServoCenter + ServoSwitchDeadBand)){ //Stabilization Software Switch pressed
StabStatus = 1; //Stabilization Normal Mode
}}}

If(StabStatus = 1){ //Stabilization Status = Normal Mode
If(StabSwitchVal = HIGH){ //Stabilization Switch ON
If(StartStopStabVal > (ServoCenter + ServoSwitchDeadBand)){ //Stabilization Software Switch pressed
StabStatus = 0; //Stabilization E-Mode
}}}

If(StabSwitchVal = LOW) || VehicleSelection < 4{ //Stabilization Switch OFF or Vehicle selection = WWII era vehicle
StabStatus = 0; //Stabilization E-Mode
}



//AZIMITH CODE

AziPotBottomLimit = ServoCenter - MaxAzimithPotVal; //Set bottom limit to center - Azimith pot

AziPotTopLimit = ServoCenter + MaxAzimithPotVal; //Set top limit to center + Azimith pot

AzimithVal = map(AzimithVal, (ServoCenter - 1000), (ServoCenter + 1000), AziPotBottomLimit, AziPotTopLimit);

If(StabStatus = 1){ //Stabilization Normal Mode
AzimithVal = AzimithVal + (ServoCenter - GryoVal); //Azimith Value with Stabilization
}

If((ServoCenter + ServoDeadBand) > AzimithVal > (ServoCenter - ServoDeadBand)){ //No Azimith Command
AzimithSound = 0; //No Azimith Sound
}
Else{
AzimithSound = 1; //Azimith Sound
}

myservo3.writeMicroseconds(AzimithVal);



//ELEVATION CODE

AccelerationX = ((pulseX / 10) - 500) * 8; // convert the pulse width into Acceleration in milli G's
AccelerationY = ((pulseY / 10) - 500) * 8; // convert the pulse width into Acceleration in milli G's

Y_AxisVal = (AccelerationY * ElevationRatioPotVal); //Multiply Elevation ratio by Acceleration counts
Y_AxisVal = constrain(Y_AxisVal, -1000, 1000); //Constrain Acceleration to +/- 1000 counts

If(StabStatus = 1){
ElevationVal = ElevationVal + (Y_AxisVal + (ElevationVal - ServoCenter));
}

If(DeckClearanceState = HIGH){
ElevationVal = 1500;
}

If((ServoCenter + ServoDeadBand) > ElevationVal > (ServoCenter - ServoDeadBand)){
ElevationSound = 0;
}
Else{
ElevationSound = 1;
}

myservo4.writeMicroseconds(ElevationVal);



//RECOIL CODE

If(Fire = 1){
Firing = 1;
RecoilVal = 2500;
myservo5.writeMicroseconds(RecoilVal);
Fire = 0;
}

If(Firing = 1){
If(RecoilPotVal < 100){
Recoil_Count_Delay++
If(Recoil_Count_Delay > 10){
Recoil_Count_Delay = 0;
RecoilVal = RecoilVal - 22;
myservo5.writeMicroseconds(RecoilVal);
If(RecoilVal < RecoilNeutral){
Fireing = 0;
}}}}

If(Firing = 1){
If(RecoilPotVal >= 100){
RecoilVal = 2500;
Recoil_Count_Delay++;
myservo5.writeMicroseconds(RecoilVal);
If(Recoil_Count_Delay > RecoilPotVal * 2){
Recoil_Count_Delay = 0;
Firing = 0;
}}}

If(Firing = 0){
RecoilVal = RecoilNeutral;
}



//CITV CODE

CITVRatioPotVal = Map(CITVRatioPotVal, 0, 1023, 0, 2);
CITVVal = CITVVal - (AzimithVal * CITVRatioPotVal);
myservo6.writeMicroseconds(CITVVal);



//GYRO REFERENCE CODE

myservo7.writeMicroseconds(1500);



//SMOKE MODULE CODE

If(PivotSteerVal > (ServoCenter + ServoSwitchDeadBand)){
If((ServoCenter + 1000) - SteerVal > (ServoCenter - 1000) + SteerVal){
SmokeVal = SteerVal;
SmokeModuleVal = SteerVal - SmokeVal;
}
Else{
SmokeVal = (ServoCenter - SteerVal) + ServoCenter;
SmokeModuleVal = SteerVal - SmokeVal;
}}
Else{
If(LeftTrackVal > RightTrackVal){
SmokeVal = LeftTrackVal;
SmokeModuleVal = ThrottleVal - SmokeVal;
}
Else{
SmokeVal = RightTrackVal;
SmokeModuleVal = ThrottleVal - SmokeVal;
}}

myservo8.writeMicroseconds(SmokeModuleVal);