Microsoft MakeCode

Motors reset() not consistently clearingCounts

We were having issues calling some custom gyro code in a series of calls where it seemed like the code was just being skipped some times. This was very frustrating to the students. I reviewed the code and then did some testing

example (simplified down this is normally like a gyroFollow type code pattern):
function Steer(heading: number, speed: number, maxAngle: number) {
motors.largeBC.reset()
while(Math.abs(motors.largeB.angle()) < Math.abs(maxAngle) && Math.abs(motors.largeC.angle()) <
Math.abs(maxAngle)){
motors.largeBC.steer(heading, speed);
}
}

Steer(10, 10, 500)
Steer(20, -20, 500)
Steer(20, 40, 300)
Steer(20, 40, 300)

The problem is that reset() some times clears the counts and sometimes it doesn’t. There seems to be an odd timing issue the above code might only work for a couple of the calls to Steer. Now if you
do this:
Steer(10, 10, 500)
pause(50)
Steer(20, -20, 500)
pause(50)
Steer(20, 40, 300)
pause(50)
Steer(20, 40, 300)
All of the calls work as expected. I took a quick look at output.ts and I do see it issuing motorBC.reset() does do a writePWM(mkCmd(out, DAL.opOutputClearCount, 0)) however if you look at the code for clearCounts() there is additional code.

What I found is that it is better to call
motors.LargeB.clearCounts()
motors.LargeC.clearCounts()

calling clearCounts() seems to be very consistent in clearing the counts where reset() is not.