@@ -72,11 +72,24 @@ class AutomatedDriver {
72
72
return selectedWaveform . waveform ;
73
73
}
74
74
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.
80
93
}
81
94
82
95
emitToRiders ( channel , channelName , electronState ) {
@@ -121,7 +134,7 @@ class AutomatedDriver {
121
134
electronState . storeLastMessage ( this . sessId , channelName , msg ) ;
122
135
}
123
136
124
- processChannel ( channel , channelName , elapsedMinutes , electronState ) {
137
+ processChannel ( channel , channelName , otherChannel , elapsedMinutes , electronState ) {
125
138
// first, we consider the possibility of pain!
126
139
if ( Math . random ( ) < ( this . painProbability * 0.01 ) && elapsedMinutes > 0 ) {
127
140
console . log ( `Automated driver ${ this . sessId } is sending PAIN signal to the ${ channelName . toUpperCase ( ) } channel` ) ;
@@ -135,7 +148,7 @@ class AutomatedDriver {
135
148
this . toggleAM ( channel , elapsedMinutes ) ;
136
149
}
137
150
138
- this . varyFrequency ( channel ) ;
151
+ this . varyFrequency ( channel , otherChannel . freq ) ;
139
152
this . emitToRiders ( channel , channelName , electronState ) ;
140
153
console . log ( `Automated driver ${ this . sessId } made changes to the ${ channelName . toUpperCase ( ) } channel. Elapsed minutes: ${ elapsedMinutes . toFixed ( 2 ) } ` ) ;
141
154
}
@@ -157,12 +170,12 @@ class AutomatedDriver {
157
170
runActionsOnChannels ( elapsedMinutes , electronState ) {
158
171
if ( Math . random ( ) < 0.5 || elapsedMinutes === 0 ) {
159
172
// 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 ) ;
161
174
}
162
175
163
176
if ( Math . random ( ) < 0.5 || elapsedMinutes === 0 ) {
164
177
// 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 ) ;
166
179
}
167
180
}
168
181
0 commit comments