Skip to content

Commit d424723

Browse files
committed
Improved frequency selection for triphase configuration
1 parent ebb280b commit d424723

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

automatedDriver.js

+22-9
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,24 @@ class AutomatedDriver {
7272
return selectedWaveform.waveform;
7373
}
7474

75-
varyFrequency(channel) {
76-
// Slightly vary the frequencies
77-
const variation = (Math.random() * 2 - 1) * 50; // Random value between -50 and 50
78-
channel.freq = Math.max(this.minFrequency, Math.min(this.maxFrequency, channel.freq + variation));
79-
channel.freq = Math.round(channel.freq);
75+
varyFrequency(channel, otherChannelFreq) {
76+
// Vary the frequencies using a random walk with reflective boundaries
77+
// Limit the step size to be the lesser of 50 Hz or the min-to-max frequency span
78+
const variation = (Math.random() * 2 - 1) * Math.min(50, this.maxFrequency - this.minFrequency);
79+
let newFreq = channel.freq + variation;
80+
if (newFreq < this.minFrequency) {
81+
newFreq = 2 * this.minFrequency - newFreq;
82+
} else if (newFreq > this.maxFrequency) {
83+
newFreq = 2 * this.maxFrequency - newFreq;
84+
}
85+
// Round frequency to the nearest 0.1 Hz
86+
newFreq = Math.round(newFreq * 10) / 10;
87+
if (newFreq != otherChannelFreq) {
88+
channel.freq = newFreq;
89+
}
90+
// Do not change frequency if it is the same as other channel's frequency,
91+
// because in triphase configuration channels can cancel or add too strongly.
92+
// Instead keep the same frequency and wait for the next update.
8093
}
8194

8295
emitToRiders(channel, channelName, electronState) {
@@ -121,7 +134,7 @@ class AutomatedDriver {
121134
electronState.storeLastMessage(this.sessId, channelName, msg);
122135
}
123136

124-
processChannel(channel, channelName, elapsedMinutes, electronState) {
137+
processChannel(channel, channelName, otherChannel, elapsedMinutes, electronState) {
125138
// first, we consider the possibility of pain!
126139
if (Math.random() < (this.painProbability * 0.01) && elapsedMinutes > 0) {
127140
console.log(`Automated driver ${this.sessId} is sending PAIN signal to the ${channelName.toUpperCase()} channel`);
@@ -135,7 +148,7 @@ class AutomatedDriver {
135148
this.toggleAM(channel, elapsedMinutes);
136149
}
137150

138-
this.varyFrequency(channel);
151+
this.varyFrequency(channel, otherChannel.freq);
139152
this.emitToRiders(channel, channelName, electronState);
140153
console.log(`Automated driver ${this.sessId} made changes to the ${channelName.toUpperCase()} channel. Elapsed minutes: ${elapsedMinutes.toFixed(2)}`);
141154
}
@@ -157,12 +170,12 @@ class AutomatedDriver {
157170
runActionsOnChannels(elapsedMinutes, electronState) {
158171
if (Math.random() < 0.5 || elapsedMinutes === 0) {
159172
// 50% chance of making changes to the left channel
160-
this.processChannel(this.leftChannel, 'left', elapsedMinutes, electronState);
173+
this.processChannel(this.leftChannel, 'left', this.rightChannel, elapsedMinutes, electronState);
161174
}
162175

163176
if (Math.random() < 0.5 || elapsedMinutes === 0) {
164177
// 50% chance of making changes to the right channel
165-
this.processChannel(this.rightChannel, 'right', elapsedMinutes, electronState);
178+
this.processChannel(this.rightChannel, 'right', this.leftChannel, elapsedMinutes, electronState);
166179
}
167180
}
168181

0 commit comments

Comments
 (0)