Skip to content

Commit b90d0d6

Browse files
author
Gary Keeble
committed
Add PID Error calculations
Add new graph fields representing the PID Error; This requires the calculation of rcCommand[] to actual deg/s for comparison with gyro values; Only works for ACRO mode at the moment and the calc is hardcoded for ReWrite PID controller; with fixed rates; Need to make the values dynamic….
1 parent d804bf9 commit b90d0d6

File tree

3 files changed

+80
-2
lines changed

3 files changed

+80
-2
lines changed

js/flightlog.js

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ function FlightLog(logData) {
199199
// Add names for our ADDITIONAL_COMPUTED_FIELDS
200200
fieldNames.push("heading[0]", "heading[1]", "heading[2]");
201201
fieldNames.push("axisSum[0]", "axisSum[1]", "axisSum[2]");
202+
fieldNames.push("axisError[0]", "axisError[1]", "axisError[2]"); // Custom calculated error field
202203

203204
fieldNameToIndex = {};
204205
for (i = 0; i < fieldNames.length; i++) {
@@ -469,7 +470,8 @@ function FlightLog(logData) {
469470
gyroADC = [fieldNameToIndex["gyroADC[0]"], fieldNameToIndex["gyroADC[1]"], fieldNameToIndex["gyroADC[2]"]],
470471
accSmooth = [fieldNameToIndex["accSmooth[0]"], fieldNameToIndex["accSmooth[1]"], fieldNameToIndex["accSmooth[2]"]],
471472
magADC = [fieldNameToIndex["magADC[0]"], fieldNameToIndex["magADC[1]"], fieldNameToIndex["magADC[2]"]],
472-
473+
rcCommand = [fieldNameToIndex["rcCommand[0]"], fieldNameToIndex["rcCommand[1]"], fieldNameToIndex["rcCommand[2]"]],
474+
473475
sourceChunkIndex, destChunkIndex,
474476

475477
sysConfig,
@@ -534,6 +536,15 @@ function FlightLog(logData) {
534536
(axisPID[axis][1] !== undefined ? srcFrame[axisPID[axis][1]] : 0) +
535537
(axisPID[axis][2] !== undefined ? srcFrame[axisPID[axis][2]] : 0);
536538
}
539+
540+
// Calculate the PID Error
541+
for (var axis = 0; axis < 3; axis++) {
542+
destFrame[fieldIndex++] =
543+
(gyroADC[axis] !== undefined ? that.gyroRawToDegreesPerSecond(srcFrame[gyroADC[axis]]) : 0) -
544+
(rcCommand[axis] !== undefined ? that.rcCommandRawToDegreesPerSecond(srcFrame[rcCommand[axis]], axis) : 0);
545+
console.log()
546+
}
547+
537548
}
538549
}
539550
}
@@ -881,6 +892,43 @@ FlightLog.prototype.gyroRawToDegreesPerSecond = function(value) {
881892
return this.getSysConfig().gyroScale * 1000000 / (Math.PI / 180.0) * value;
882893
};
883894

895+
// Convert rcCommand to degrees per second
896+
FlightLog.prototype.rcCommandRawToDegreesPerSecond = function(value, axis) {
897+
898+
// Axis 0,1 refers to Roll and Pitch
899+
// Axis 2 refers to Yaw.
900+
901+
var rRate = 65; // Roll Rate from configuration
902+
var pRate = 65; // Pitch Rate from configuration
903+
var yRate = 65; // Yaw Rate from configuration
904+
905+
var REWRITE = true; // Define that this is for the Rewrite Data - need to make dynamic!
906+
907+
if(REWRITE) // PID is ReWrite
908+
{
909+
if(axis==0 /*ROLL*/) {
910+
return ((rRate + 27) * value ) >> 6;
911+
}
912+
if(axis==1 /*PITCH*/) {
913+
return ((pRate + 27) * value ) >> 6;
914+
}
915+
if(axis==2 /*YAW*/) {
916+
return ((yRate + 27) * value ) >> 7;
917+
}
918+
} else { // PID is Luxfloat
919+
if(axis==0 /*ROLL*/) {
920+
return ((rRate+20)*value) / 50;
921+
}
922+
if(axis==1 /*PITCH*/) {
923+
return ((pRate+20)*value) / 50;
924+
}
925+
if(axis==2 /*YAW*/) {
926+
return ((yRate+10)*value) / 50;
927+
}
928+
}
929+
};
930+
931+
884932
FlightLog.prototype.getReferenceVoltageMillivolts = function() {
885933
return this.vbatADCToMillivolts(this.getSysConfig().vbatref);
886934
};

js/flightlog_fields_presenter.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,14 @@ function FlightLogFieldPresenter() {
5959
'axisSum[all]': 'PID_sum',
6060
'axisSum[0]' : 'PID_sum[roll]',
6161
'axisSum[1]' : 'PID_sum[pitch]',
62-
'axisSum[2]' : 'PID_sum[yaw]'
62+
'axisSum[2]' : 'PID_sum[yaw]',
63+
64+
//Virtual fields - Add the Error fields
65+
'axisError[all]': 'PID_Error',
66+
'axisError[0]' : 'PID_Error[roll]',
67+
'axisError[1]' : 'PID_Error[pitch]',
68+
'axisError[2]' : 'PID_Error[yaw]'
69+
6370
};
6471

6572
function presentFlags(flags, flagNames) {
@@ -119,6 +126,18 @@ function FlightLogFieldPresenter() {
119126
case 'gyroADC[2]':
120127
return Math.round(flightLog.gyroRawToDegreesPerSecond(value)) + " deg/s";
121128

129+
case 'axisError[0]':
130+
case 'axisError[1]':
131+
case 'axisError[2]':
132+
return Math.round(value) + " deg/s";
133+
134+
case 'rcCommand[0]':
135+
return Math.round(flightLog.rcCommandRawToDegreesPerSecond(value,0)) + " deg/s";
136+
case 'rcCommand[1]':
137+
return Math.round(flightLog.rcCommandRawToDegreesPerSecond(value,1)) + " deg/s";
138+
case 'rcCommand[2]':
139+
return Math.round(flightLog.rcCommandRawToDegreesPerSecond(value,2)) + " deg/s";
140+
122141
case 'accSmooth[0]':
123142
case 'accSmooth[1]':
124143
case 'accSmooth[2]':

js/graph_config.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,10 @@ GraphConfig.load = function(config) {
173173
label: "PIDs",
174174
fields: ["axisSum[all]"]
175175
},
176+
{
177+
label: "Gyro Error",
178+
fields: ["axisError[all]"]
179+
},
176180
{
177181
label: "Gyro + PID roll",
178182
fields: ["axisP[0]", "axisI[0]", "axisD[0]", "gyroADC[0]"]
@@ -239,6 +243,13 @@ GraphConfig.load = function(config) {
239243
inputRange: sysConfig.acc_1G * 3.0, /* Reasonable typical maximum for acc */
240244
outputRange: 1.0
241245
};
246+
} else if (fieldName.match(/^axisError\[/)) { // new PID error field
247+
return {
248+
offset: 0,
249+
power: 0.8, /* Make this 1.0 to scale linearly */
250+
inputRange: 1000, // Maximum error is hard coded to 1000 deg/s
251+
outputRange: 1.0
252+
};
242253
} else if (fieldName.match(/^axis.+\[/)) {
243254
return {
244255
offset: 0,

0 commit comments

Comments
 (0)