summaryrefslogtreecommitdiffstats
path: root/ctrl/flight-control.vala
blob: 00e7b31c17f4d68dbf23ab39defe48262d144652 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/* Copyright 2012, Sebastian Reichel <sre@ring0.de>
 * Copyright 2012, Ted Carancho
 *
 * Ported from AeroQuad
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

public class FlightControl {
	MotorController motor;
	FlightMode mode;
	PID[] pid;
	Kinematics k;

	public FlightControl() {
		motor = new AtmostripeMotorController();
		pid = new PID[PIDEntry.length];
		k = new Kinematics(0, 0); // TODO: read compass instead of using 0,0
	}

	/**
	 * calculateFlightError
	 *
	 * Calculate roll/pitch axis error with gyro/accel data to
	 * compute motor command thrust so used command are executed
	 */
	void calculateFlightError(out double roll, out double pitch) {
		roll = 0; pitch = 0;

		if (mode == FlightMode.ATTITUDE_FLIGHT_MODE) {
		}
#if 0
		if (mode == FlightMode.ATTITUDE_FLIGHT_MODE) {
			// 0.0 -> (receiverCommand[AXIS.X] - receiverZero[AXIS.X]) * ATTITUDE_SCALING
			double rollAttitudeCmd   = pid[PIDEntry.ATTITUDE_XAXIS].update(0.1, k.kinematicsAngle[AXIS.X]);
			// 0.0 -> (receiverCommand[AXIS.Y] - receiverZero[AXIS.Y]) * ATTITUDE_SCALING
			double pitchAttitudeCmd  = pid[PIDEntry.ATTITUDE_YAXIS].update(0.1, k.kinematicsAngle[AXIS.Y]);

			roll   = pid[PIDEntry.ATTITUDE_GYRO_XAXIS].update(rollAttitudeCmd, gyroRate[AXIS.X]*1.2);
			pitch  = pid[PIDEntry.ATTITUDE_GYRO_YAXIS].update(pitchAttitudeCmd, -gyroRate[AXIS.Y]*1.2);
		}
#endif
#if 0
			double pitchAttitudeCmd = updatePID((receiverCommand[AXIS.Y] - receiverZero[AXIS.Y]) * ATTITUDE_SCALING, -kinematicsAngle[AXIS.Y], &PID[ATTITUDE_YAXIS_PID_IDX]);
			roll   = updatePID(rollAttitudeCmd, gyroRate[XAXIS]*1.2, &PID[ATTITUDE_GYRO_XAXIS_PID_IDX]);
			pitch  = updatePID(pitchAttitudeCmd, -gyroRate[YAXIS]*1.2, &PID[ATTITUDE_GYRO_YAXIS_PID_IDX]);
		}
		else {
			roll  = updatePID(getReceiverSIData(XAXIS), gyroRate[XAXIS]*0.8, &PID[RATE_XAXIS_PID_IDX]);
			pitch = updatePID(getReceiverSIData(YAXIS), -gyroRate[YAXIS]*0.8, &PID[RATE_YAXIS_PID_IDX]);
		}
#endif
	}

#if 0
	/**
	 * processThrottleCorrection
	 *
	 * This function will add some throttle imput if the craft is angled
	 * this prevent the craft to loose altitude when angled.
	 * it also add the battery throttle correction in case
	 * of we are in auto-descent.
	 *
	 * Special thank to Ziojo for this.
	 */
	void processThrottleCorrection() {
		int throttleAsjust = throttle / (cos (radians (kinematicsAngle[XAXIS])) * cos (radians (kinematicsAngle[YAXIS])));
		throttleAsjust = constrain ((throttleAsjust - throttle), 0, 160); //compensate max  +/- 25 deg XAXIS or YAXIS or  +/- 18 ( 18(XAXIS) + 18(YAXIS))
		throttle = throttle + throttleAsjust + (int)batteyMonitorThrottleCorrection;

		throttle = constrain(throttle,MINCOMMAND,MAXCOMMAND-150);  // limmit throttle to leave some space for motor correction in max throttle manuever
	}
#endif

	/**
	 * processHeading
	 *
	 * This function will calculate the craft heading correction depending
	 * of the users command. Heading correction is process with the gyro
	 * or a magnetometer
	 */
	private void processHeading() {
		// TODO
	}

	/**
	 * processFlightControl
	 *
	 * Main flight control processos function
	 */
	public void processFlightControl() {
		double roll, pitch;
		calculateFlightError(out roll, out pitch);
		processHeading();

#if 0
		/* 50Hz tasks */
		processAltitudeHold();
		processThrottleCorrection();

		/* if running */
		applyMotorCommand();

		processMinMaxCommand();

		/* Allows quad to do acrobatics by lowering power to opposite motors during hard manuevers */
		//processHardManuevers();

		/* If throttle in minimum position, don't apply yaw */
		if (receiverCommand[THROTTLE] < MINCHECK) {
			for (byte motor = 0; motor < LASTMOTOR; motor++) {
				motorMaxCommand[motor] = minArmedThrottle;
			}
		}

		/* Apply limits to motor commands */
		for (byte motor = 0; motor < LASTMOTOR; motor++) {
			motorCommand[motor] = constrain(motorCommand[motor], motorMinCommand[motor], motorMaxCommand[motor]);
		}

		/* ESC Calibration */
		if (motorArmed == OFF) {
			processCalibrateESC();
		}

		if (motorArmed == ON && safetyCheck == ON) {
			writeMotors();
		}
#endif
	}
}