@@ -136,6 +136,11 @@ namespace microcode {
136
136
}
137
137
138
138
private updateSpeed ( ) {
139
+ console . log ( `tmode: ${ this . targetSpeedMode } ` )
140
+ console . log ( `tspeed: ${ this . targetSpeed } ` )
141
+ console . log ( `cmode: ${ this . currentSpeedMode } ` )
142
+ console . log ( `cspeed: ${ this . currentSpeed } ` )
143
+
139
144
// transition from one mode to the other, robot should stop
140
145
if ( this . currentSpeedMode !== this . targetSpeedMode ) {
141
146
const alpha = MODE_TRANSITION_ALPHA
@@ -174,25 +179,32 @@ namespace microcode {
174
179
}
175
180
}
176
181
}
177
- console . log ( `left: ${ left } ` )
178
- console . log ( `right: ${ right } ` )
179
- this . robot . motorRun ( left , right )
180
- this . showMotorState ( left , right )
182
+ this . setMotorState ( left , right )
181
183
} else {
182
184
let s = this . currentSpeed
183
185
if ( lines )
184
- s = s * Math . min ( Math . abs ( s ) , this . robot . maxLineTurnSpeed )
185
- console . log ( `speed: ${ s } ` )
186
- this . robot . motorTurn ( s )
187
- this . showMotorState (
188
- s > 0 ? s : 0 ,
189
- s <= 0 ? 0 : - s
190
- )
186
+ s = Math . sign ( s ) * Math . min ( Math . abs ( s ) , this . robot . maxLineTurnSpeed )
187
+ let left = 0
188
+ let right = 0
189
+ const op = Math . abs ( s ) / 3
190
+ if ( s > 0 ) {
191
+ right = Math . constrain ( this . robot . maxTurnSpeed + s , 0 , op )
192
+ left = s
193
+ } else {
194
+ right = - s
195
+ left = Math . constrain ( this . robot . maxTurnSpeed - s , 0 , op )
196
+ }
197
+ this . setMotorState ( left , right )
191
198
}
192
199
}
193
200
194
- private showMotorState ( left : number , right : number ) {
201
+ private setMotorState ( left : number , right : number ) {
202
+ left = Math . round ( left )
203
+ right = Math . round ( right )
204
+ this . robot . motorRun ( left , right )
195
205
if ( this . showConfiguration ) return
206
+ console . log ( `left: ${ left } ` )
207
+ console . log ( `right: ${ right } ` )
196
208
this . showSingleMotorState ( 3 , left )
197
209
this . showSingleMotorState ( 1 , right )
198
210
}
@@ -269,9 +281,11 @@ namespace microcode {
269
281
speed > 0
270
282
? Math . min ( this . robot . maxRunSpeed , speed )
271
283
: Math . max ( - this . robot . maxBackSpeed , speed )
272
- this . setHeadlingSpeedColor ( speed )
273
- this . targetSpeedMode = RobotSpeedMode . Run
274
- this . targetSpeed = speed
284
+ if ( this . targetSpeedMode !== RobotSpeedMode . Run || this . targetSpeed !== speed ) {
285
+ this . setHeadlingSpeedColor ( speed )
286
+ this . targetSpeedMode = RobotSpeedMode . Run
287
+ this . targetSpeed = speed
288
+ }
275
289
}
276
290
277
291
motorTurn ( speed : number ) {
@@ -281,10 +295,11 @@ namespace microcode {
281
295
speed > 0
282
296
? Math . min ( this . robot . maxTurnSpeed , speed )
283
297
: Math . max ( - this . robot . maxTurnSpeed , speed )
284
- this . setHeadlingSpeedColor ( speed )
285
- this . targetSpeedMode = RobotSpeedMode . Turn
286
- this . targetSpeed = speed
287
- this . currentSpeed = 0
298
+ if ( this . targetSpeedMode !== RobotSpeedMode . Turn || this . targetSpeed !== speed ) {
299
+ this . setHeadlingSpeedColor ( speed )
300
+ this . targetSpeedMode = RobotSpeedMode . Turn
301
+ this . targetSpeed = speed
302
+ }
288
303
}
289
304
290
305
motorStop ( ) {
0 commit comments