Skip to content

Commit 6d92789

Browse files
authored
Merge pull request #266 from mikeller/add_deadband_to_curves_and_model
Added deadband to curves and model calculations. Also fixed rates curve bug in pre 2.8.
2 parents f9ea2c8 + edad524 commit 6d92789

File tree

4 files changed

+58
-34
lines changed

4 files changed

+58
-34
lines changed

js/RateCurve.js

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ var RateCurve = function (useLegacyCurve) {
1111
return Math.max(min, Math.min(value, max));
1212
};
1313

14-
this.rcCommand = function (rcData, rcRate) {
15-
var tmp = Math.min(Math.abs(rcData - midRc), 500);
14+
this.rcCommand = function (rcData, rcRate, deadband) {
15+
var tmp = Math.min(Math.max(Math.abs(rcData - midRc) - deadband, 0), 500);
1616

1717
var result = tmp * rcRate;
1818

@@ -23,7 +23,7 @@ var RateCurve = function (useLegacyCurve) {
2323
return result;
2424
};
2525

26-
this.drawRateCurve = function (rate, rcRate, rcExpo, superExpoActive, maxAngularVel, context, width, height) {
26+
this.drawRateCurve = function (rate, rcRate, rcExpo, superExpoActive, deadband, maxAngularVel, context, width, height) {
2727
var canvasHeightScale = height / (2 * maxAngularVel);
2828

2929
var stepWidth = context.lineWidth;
@@ -33,10 +33,10 @@ var RateCurve = function (useLegacyCurve) {
3333

3434
context.beginPath();
3535
var rcData = minRc;
36-
context.moveTo(-500, -canvasHeightScale * this.rcCommandRawToDegreesPerSecond(rcData, rate, rcRate, rcExpo, superExpoActive));
36+
context.moveTo(-500, -canvasHeightScale * this.rcCommandRawToDegreesPerSecond(rcData, rate, rcRate, rcExpo, superExpoActive, deadband));
3737
rcData = rcData + stepWidth;
3838
while (rcData <= maxRc) {
39-
context.lineTo(rcData - midRc, -canvasHeightScale * this.rcCommandRawToDegreesPerSecond(rcData, rate, rcRate, rcExpo, superExpoActive));
39+
context.lineTo(rcData - midRc, -canvasHeightScale * this.rcCommandRawToDegreesPerSecond(rcData, rate, rcRate, rcExpo, superExpoActive, deadband));
4040

4141
rcData = rcData + stepWidth;
4242
}
@@ -48,7 +48,7 @@ var RateCurve = function (useLegacyCurve) {
4848
this.drawLegacyRateCurve = function (rate, rcRate, rcExpo, context, width, height) {
4949
// math magic by englishman
5050
var rateY = height * rcRate;
51-
rateY = rateY + (1 / (1 - ((rateY / height) * rate)))
51+
rateY = rateY + (1 / (1 - ((rateY / height) * rate)));
5252

5353
// draw
5454
context.beginPath();
@@ -58,13 +58,14 @@ var RateCurve = function (useLegacyCurve) {
5858
}
5959
};
6060

61-
RateCurve.prototype.rcCommandRawToDegreesPerSecond = function (rcData, rate, rcRate, rcExpo, superExpoActive) {
61+
RateCurve.prototype.rcCommandRawToDegreesPerSecond = function (rcData, rate, rcRate, rcExpo, superExpoActive, deadband) {
6262
var angleRate;
6363
if (rate !== undefined && rcRate !== undefined && rcExpo !== undefined) {
6464
if (rcRate > 2) {
6565
rcRate = rcRate + (rcRate - 2) * 14.54;
6666
}
67-
var inputValue = this.rcCommand(rcData, rcRate);
67+
68+
var inputValue = this.rcCommand(rcData, rcRate, deadband);
6869
var maxRc = 500 * rcRate;
6970

7071
var expoPower;
@@ -98,24 +99,24 @@ RateCurve.prototype.rcCommandRawToDegreesPerSecond = function (rcData, rate, rcR
9899
return angleRate;
99100
};
100101

101-
RateCurve.prototype.getMaxAngularVel = function (rate, rcRate, rcExpo, superExpoActive) {
102+
RateCurve.prototype.getMaxAngularVel = function (rate, rcRate, rcExpo, superExpoActive, deadband) {
102103
var maxAngularVel;
103104
if (!this.useLegacyCurve) {
104-
maxAngularVel = this.rcCommandRawToDegreesPerSecond(maxRc, rate, rcRate, rcExpo, superExpoActive);
105+
maxAngularVel = this.rcCommandRawToDegreesPerSecond(maxRc, rate, rcRate, rcExpo, superExpoActive, deadband);
105106
}
106107

107108
return maxAngularVel;
108109
};
109110

110-
RateCurve.prototype.draw = function (rate, rcRate, rcExpo, superExpoActive, maxAngularVel, context) {
111+
RateCurve.prototype.draw = function (rate, rcRate, rcExpo, superExpoActive, deadband, maxAngularVel, context) {
111112
if (rate !== undefined && rcRate !== undefined && rcExpo !== undefined) {
112113
var height = context.canvas.height;
113114
var width = context.canvas.width;
114115

115116
if (this.useLegacyCurve) {
116117
this.drawLegacyRateCurve(rate, rcRate, rcExpo, context, width, height);
117118
} else {
118-
this.drawRateCurve(rate, rcRate, rcExpo, superExpoActive, maxAngularVel, context, width, height);
119+
this.drawRateCurve(rate, rcRate, rcExpo, superExpoActive, deadband, maxAngularVel, context, width, height);
119120
}
120121
}
121122
};

tabs/pid_tuning.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
<th class="derivative" i18n="pidTuningDerivative"></th>
7373
<th class="rc_rate" i18n="pidTuningRcRate"></th>
7474
<th class="rate" i18n="pidTuningRate"></th>
75-
<th class="maxVel" i18n="pidTuningMaxVel"></th>
75+
<th class="new_rates maxVel" i18n="pidTuningMaxVel"></th>
7676
<th class="rc_expo" i18n="pidTuningRcExpo"></th>
7777
</tr>
7878
</table>

tabs/pid_tuning.js

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@ TABS.pid_tuning.initialize = function (callback) {
4747
promise = MSP.promise(MSPCodes.MSP_BF_CONFIG);
4848
}
4949

50+
return promise;
51+
}).then(function() {
52+
var promise = true;
53+
if (semver.gte(CONFIG.apiVersion, "1.15.0")) {
54+
promise = MSP.promise(MSPCodes.MSP_RC_DEADBAND);
55+
}
56+
5057
return promise;
5158
}).then(function() {
5259
$('#content').load("./tabs/pid_tuning.html", process_html);
@@ -453,18 +460,18 @@ TABS.pid_tuning.initialize = function (callback) {
453460

454461
self.rateCurve = new RateCurve(useLegacyCurve);
455462

456-
function printMaxAngularVel(rate, rcRate, rcExpo, useSuperExpo, maxAngularVelElement) {
457-
var maxAngularVel = self.rateCurve.getMaxAngularVel(rate, rcRate, rcExpo, useSuperExpo).toFixed(0);
463+
function printMaxAngularVel(rate, rcRate, rcExpo, useSuperExpo, deadband, maxAngularVelElement) {
464+
var maxAngularVel = self.rateCurve.getMaxAngularVel(rate, rcRate, rcExpo, useSuperExpo, deadband).toFixed(0);
458465
maxAngularVelElement.text(maxAngularVel);
459466

460467
return maxAngularVel;
461468
}
462469

463-
function drawCurve(rate, rcRate, rcExpo, useSuperExpo, maxAngularVel, colour, yOffset, context) {
470+
function drawCurve(rate, rcRate, rcExpo, useSuperExpo, maxAngularVel, deadband, colour, yOffset, context) {
464471
context.save();
465472
context.strokeStyle = colour;
466473
context.translate(0, yOffset);
467-
self.rateCurve.draw(rate, rcRate, rcExpo, useSuperExpo, maxAngularVel, context);
474+
self.rateCurve.draw(rate, rcRate, rcExpo, useSuperExpo, maxAngularVel, deadband, context);
468475
context.restore();
469476
}
470477

@@ -485,7 +492,9 @@ TABS.pid_tuning.initialize = function (callback) {
485492
rc_rate_yaw: RC_tuning.rcYawRate,
486493
rc_expo: RC_tuning.RC_EXPO,
487494
rc_yaw_expo: RC_tuning.RC_YAW_EXPO,
488-
superexpo: BF_CONFIG.features.isEnabled('SUPEREXPO_RATES')
495+
superexpo: BF_CONFIG.features.isEnabled('SUPEREXPO_RATES'),
496+
deadband: RC_deadband.deadband,
497+
yawDeadband: RC_deadband.yaw_deadband
489498
};
490499

491500
if (semver.lt(CONFIG.apiVersion, "1.7.0")) {
@@ -717,22 +726,25 @@ TABS.pid_tuning.initialize = function (callback) {
717726
var curveHeight = rcCurveElement.height;
718727
var curveWidth = rcCurveElement.width;
719728

720-
var maxAngularVel = Math.max(
721-
printMaxAngularVel(self.currentRates.roll_rate, self.currentRates.rc_rate, self.currentRates.rc_expo, self.currentRates.superexpo, maxAngularVelRollElement),
722-
printMaxAngularVel(self.currentRates.pitch_rate, self.currentRates.rc_rate, self.currentRates.rc_expo, self.currentRates.superexpo, maxAngularVelPitchElement),
723-
printMaxAngularVel(self.currentRates.yaw_rate, self.currentRates.rc_rate_yaw, self.currentRates.rc_yaw_expo, self.currentRates.superexpo, maxAngularVelYawElement));
724-
725729
curveContext.clearRect(0, 0, curveWidth, curveHeight);
726730

731+
var maxAngularVel;
727732
if (!useLegacyCurve) {
733+
maxAngularVel = Math.max(
734+
printMaxAngularVel(self.currentRates.roll_rate, self.currentRates.rc_rate, self.currentRates.rc_expo, self.currentRates.superexpo, self.currentRates.deadband, maxAngularVelRollElement),
735+
printMaxAngularVel(self.currentRates.pitch_rate, self.currentRates.rc_rate, self.currentRates.rc_expo, self.currentRates.superexpo, self.currentRates.deadband, maxAngularVelPitchElement),
736+
printMaxAngularVel(self.currentRates.yaw_rate, self.currentRates.rc_rate_yaw, self.currentRates.rc_yaw_expo, self.currentRates.superexpo, self.currentRates.yawDeadband, maxAngularVelYawElement));
737+
728738
drawAxes(curveContext, curveWidth, curveHeight, (curveHeight / 2) / maxAngularVel * 360);
739+
} else {
740+
maxAngularVel = 0;
729741
}
730742

731743
curveContext.lineWidth = 4;
732744

733-
drawCurve(self.currentRates.roll_rate, self.currentRates.rc_rate, self.currentRates.rc_expo, self.currentRates.superexpo, maxAngularVel, '#ff0000', 0, curveContext);
734-
drawCurve(self.currentRates.pitch_rate, self.currentRates.rc_rate, self.currentRates.rc_expo, self.currentRates.superexpo, maxAngularVel, '#00ff00', -4, curveContext);
735-
drawCurve(self.currentRates.yaw_rate, self.currentRates.rc_rate_yaw, self.currentRates.rc_yaw_expo, self.currentRates.superexpo, maxAngularVel, '#0000ff', 4, curveContext);
745+
drawCurve(self.currentRates.roll_rate, self.currentRates.rc_rate, self.currentRates.rc_expo, self.currentRates.superexpo, self.currentRates.deadband, maxAngularVel, '#ff0000', 0, curveContext);
746+
drawCurve(self.currentRates.pitch_rate, self.currentRates.rc_rate, self.currentRates.rc_expo, self.currentRates.superexpo, self.currentRates.deadband, maxAngularVel, '#00ff00', -4, curveContext);
747+
drawCurve(self.currentRates.yaw_rate, self.currentRates.rc_rate_yaw, self.currentRates.rc_yaw_expo, self.currentRates.superexpo, self.currentRates.yawDeadband, maxAngularVel, '#0000ff', 4, curveContext);
736748

737749
updateNeeded = false;
738750
}
@@ -904,9 +916,9 @@ TABS.pid_tuning.renderModel = function () {
904916
if (RC.channels[0] && RC.channels[1] && RC.channels[2]) {
905917
var delta = this.clock.getDelta();
906918

907-
var roll = delta * this.rateCurve.rcCommandRawToDegreesPerSecond(RC.channels[0], this.currentRates.roll_rate, this.currentRates.rc_rate, this.currentRates.rc_expo, this.currentRates.superexpo),
908-
pitch = delta * this.rateCurve.rcCommandRawToDegreesPerSecond(RC.channels[1], this.currentRates.pitch_rate, this.currentRates.rc_rate, this.currentRates.rc_expo, this.currentRates.superexpo),
909-
yaw = delta * this.rateCurve.rcCommandRawToDegreesPerSecond(RC.channels[2], this.currentRates.yaw_rate, this.currentRates.rc_rate_yaw, this.currentRates.rc_yaw_expo, this.currentRates.superexpo);
919+
var roll = delta * this.rateCurve.rcCommandRawToDegreesPerSecond(RC.channels[0], this.currentRates.roll_rate, this.currentRates.rc_rate, this.currentRates.rc_expo, this.currentRates.superexpo, this.currentRates.deadband),
920+
pitch = delta * this.rateCurve.rcCommandRawToDegreesPerSecond(RC.channels[1], this.currentRates.pitch_rate, this.currentRates.rc_rate, this.currentRates.rc_expo, this.currentRates.superexpo, this.currentRates.deadband),
921+
yaw = delta * this.rateCurve.rcCommandRawToDegreesPerSecond(RC.channels[2], this.currentRates.yaw_rate, this.currentRates.rc_rate_yaw, this.currentRates.rc_yaw_expo, this.currentRates.superexpo, this.currentRates.yawDeadband);
910922

911923
this.model.rotateBy(-degToRad(pitch), -degToRad(yaw), -degToRad(roll));
912924
}

tabs/receiver.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
'use strict';
22

33
TABS.receiver = {
4-
rateChartHeight: 117
4+
rateChartHeight: 117,
5+
useSuperExpo: false,
6+
deadband: 0,
7+
yawDeadband: 0
58
};
69

710
TABS.receiver.initialize = function (callback) {
@@ -64,6 +67,14 @@ TABS.receiver.initialize = function (callback) {
6467
} else {
6568
$('.deadband input[name="yaw_deadband"]').val(RC_deadband.yaw_deadband);
6669
$('.deadband input[name="deadband"]').val(RC_deadband.deadband);
70+
71+
$('.deadband input[name="deadband"]').change(function () {
72+
this.deadband = parseInt($(this).val());
73+
}).change();
74+
$('.deadband input[name="yaw_deadband"]').change(function () {
75+
this.yawDeadband = parseInt($(this).val());
76+
}).change();
77+
6778
}
6879

6980
// generate bars
@@ -400,7 +411,7 @@ TABS.receiver.initModelPreview = function () {
400411
this.model = new Model($('.model_preview'), $('.model_preview canvas'));
401412

402413
this.useSuperExpo = false;
403-
if (CONFIG.flightControllerIdentifier === 'BTFL' && semver.gte(CONFIG.flightControllerVersion, '2.8.0')) {
414+
if (semver.gte(CONFIG.flightControllerVersion, '2.8.0')) {
404415
this.useSuperExpo = BF_CONFIG.features.isEnabled('SUPEREXPO_RATES');
405416
}
406417

@@ -417,9 +428,9 @@ TABS.receiver.renderModel = function () {
417428
if (RC.channels[0] && RC.channels[1] && RC.channels[2]) {
418429
var delta = this.clock.getDelta();
419430

420-
var roll = delta * this.rateCurve.rcCommandRawToDegreesPerSecond(RC.channels[0], RC_tuning.roll_rate, RC_tuning.RC_RATE, RC_tuning.RC_EXPO, this.useSuperExpo),
421-
pitch = delta * this.rateCurve.rcCommandRawToDegreesPerSecond(RC.channels[1], RC_tuning.pitch_rate, RC_tuning.RC_RATE, RC_tuning.RC_EXPO, this.useSuperExpo),
422-
yaw = delta * this.rateCurve.rcCommandRawToDegreesPerSecond(RC.channels[2], RC_tuning.yaw_rate, RC_tuning.rcYawRate, RC_tuning.RC_YAW_EXPO, this.useSuperExpo);
431+
var roll = delta * this.rateCurve.rcCommandRawToDegreesPerSecond(RC.channels[0], RC_tuning.roll_rate, RC_tuning.RC_RATE, RC_tuning.RC_EXPO, this.useSuperExpo, this.deadband),
432+
pitch = delta * this.rateCurve.rcCommandRawToDegreesPerSecond(RC.channels[1], RC_tuning.pitch_rate, RC_tuning.RC_RATE, RC_tuning.RC_EXPO, this.useSuperExpo, this.deadband),
433+
yaw = delta * this.rateCurve.rcCommandRawToDegreesPerSecond(RC.channels[2], RC_tuning.yaw_rate, RC_tuning.rcYawRate, RC_tuning.RC_YAW_EXPO, this.useSuperExpo, this.yawDeadband);
423434

424435
this.model.rotateBy(-degToRad(pitch), -degToRad(yaw), -degToRad(roll));
425436
}

0 commit comments

Comments
 (0)