Gyro Problems

Starting using gyro sensor with students today. Using the sample gyro code (basic). We copied and pasted this code into a function called “turn_left(degree)” so that we can call it whenever we want to turn.

We are using Virtual Worlds to test. Eventually we will try it on the physical Clawbots but I know that introduces sensor drift and such.

The problem…the gyro sensor values do not reset to 0. So the function can turn_left(90) once. If we call turn_left(90) again, the sensor seems to say “you are already at -900, so you don’t want to turn…” or if we make a companion function for turn_right and I send it turn_right(180), it spins it around 270 degrees, so that it is a 180-turn from the original position.

I try to put SensorValue[gyroSensor] = 0 in the code, but it doesn’t work. The value stays at the previous value. I try to do a new initialization (turn sensorType to NoSensor, then back again) and it still picks up the old value.

In other words, we don’t get a “relative” turning value. Instead we get an absolute value from where we started.
Is there a way to reset the value? Or should we build a function that calculates the new turn from the old position?

As you’ve already noticed, the normal way to use a gyro is like how you would use a compass: once initialized (that is, pointing North, for the compass), it always remembers that direction to be used as a reference throughout the match. If you keep “resetting” it, you will have cumulative error that could add up over several resets. Better to work out the necessary code than try to reset the gyro.

Try setting up some code where after it finishes the turning function, the program stores the gyro value in a variable and the next time the function is called, the function will use that value as a reference point for making the turn.

So you should make the function turn 90 from current location. What you currently do is turn TO 90 rather than turn 90.

Goal = sensorValue[gryo] - 900