The previous post has a small code snippet that shows how they're summing up the gyro inputs to make the 90 degree turn:
Code:
drive_L = 254 ' start right turn
drive_R = 1
select sensor1 'select yaw rate sensor
case 0 to 127 ' turning left
new_ct = 127-sensor1
yaw_ct = yaw_ct - new_ct
case 128 to 255 ' turning right
new_ct = sensor1 -127
yaw_ct = yaw_ct + new_ct
endselect
if yaw_ct > 5262 then ‘count has reached 5262 and passed 90
drive_L = 127 ‘stop turn
drive_R = 127
endif
debug ?yaw_ct 'display count value in debug screen
The code in the select statement can be simplified and the case statement eliminated. When the gyro is less than 127 the value of yaw_ct = yaw_ct - new_ct. Since new_ct = 127 - sensor1, the equivalent expression is yaw_ct = yaw_ct - (127 - sensor1), reduced to yaw_ct = yaw_ct + sensor1 - 127.
When the gyro is greater than 127 the expression is yaw_ct = yaw_ct + new_ct, where new_ct = sensor1 - 127. This also simplifies to yaw_ct = yaw_ct + sensor1 - 127.
So the following code will work the same way. I added a deadband around 127 for the gyro so that it doesn't start adding up noise if the robot is sitting still. If yaw_ct was initialized to zero and some noise creeps in then yaw_ct may underflow and trigger the IF statement. If you initialize yaw_ct to something like 10000 then you can use it for both clockwise and anti-clockwise turns. 15,262 would be to the cutoff turning left and 4738 would be the cutoff to the right.
Code:
'constants
gyro_deadband CON 4 'gyro deadband
yaw_ct_base CON 10000 'base value for yaw_ct
yaw_ct_stop CON 5262 'yaw counts for 90 degrees
'initialize
yaw_ct = yaw_ct_base
'start main loop
DO
SERIN...
drive_L = 254 'start right turn
drive_R = 1
if abs(sensor1-127) > gyro_deadband then
yaw_ct = yaw_ct + sensor1 - 127
endif
'stop after turning 90 degrees
if abs(yaw_ct - yaw_ct_base) > yaw_ct_stop then
drive_L = 127 'stop turn
drive_R = 127
endif
debug ?yaw_ct 'display count value in debug screen
SEROUT..
LOOP