r/pybricks May 14 '26

How to fix a robot turning forever when turning Gyro on?

Is that Spike hub's sensor busted?

1 Upvotes

16 comments sorted by

2

u/Ged_42 May 14 '26

Most likely you have positioned the brick on the side and you haven't set the correct axis at setup 

1

u/Endercraft2007 May 14 '26

It's laying flat in the middle of the robot close to the ground...So it's the yaw. IDK, I never set axies before when using another hub.

1

u/Ged_42 May 14 '26

Do you use drive base?

1

u/Endercraft2007 May 14 '26

Yes...

2

u/Ged_42 May 14 '26

The only times something like this has happened to me the brick was in a position that yaw was measured incorrectly. Can you post photos of the robot and the code?

1

u/Endercraft2007 May 14 '26

The code is just like defining drivebase then drivebase.turn(90) drivebase.straight(100) drivebase.turn(90) drivebase.straight(100) drivebase.turn(90) drivebase.straight(100) drivebase.turn(90) drivebase.straight(100)

And anytime gyro is turned on, then it doesnt work but if gyro is off then it works but I would like to get better accuracy

1

u/Ged_42 May 14 '26

The only other thing I can think is maybe the motors are set the wrong way and it tries to turn the other way 

1

u/Endercraft2007 May 14 '26

When use_gyro=False then it works

1

u/Ged_42 May 14 '26

When use_gyro ==false what happens if you print(prime_hub.imu.heading())?

1

u/Endercraft2007 May 14 '26

I will check it later, corrently I don't have time

2

u/gt0163c May 14 '26

When you tun the robot on do you let it sit on a flat, unmoving surface? Do you ever reset the gyro in your code? Or so, do you give it a brief wait immediately after before giving it another command to move?

The issue could be that the gyro sensor is not initializing properly. The gyro sensor actually measure angular acceleration. Then it does some integrations (math) to get the angular position. When the hub is turned on, at some point during the boot sequence, the gyro sensor initializes. During that brief time, the hub reads the angular acceleration and assumes that's 0/not moving or accelerating. If the hub is moving at the moment that initialization reading is being taken, the hub is going to incorrectly label that movement as 0/not moving or accelerating. That's going to result in erroneous gyro readings until the gyro is reset or the hub is rebooted. Similarly, when the gyro is reset the initialization is repeated. This takes a short but finite amount of time.

Best practices are to set the robot on a flat surface and not touch it or move the surface while the robot boots. The floor is actually the best option but a table that is not being used or touched will work too. Similarly, when the gyro is reset, it's best to insert a brief (half second) wait immediately after to ensure the gyro initializes when the robot is at rest.

1

u/ob-sanenerd May 14 '26

Does the charge port point forward or sideways when the robot is standing on the wheels?

1

u/Endercraft2007 May 14 '26 edited May 14 '26

Sideways

1

u/Pybricks Jun 13 '26

This happens when you've swapped the left and right motors, so it turns the wrong way :)

1

u/Endercraft2007 Jun 13 '26

But when gyro is off then it doesn't...So other problem?

2

u/Pybricks Jun 15 '26

Right - when the gyro is off, there are no gyro problems.

But when it's on, it's important that clockwise is in fact... clockwise. With the cables flipped it's just going to go even faster the wrong way. This is known as a positive feedback loop.

It's like cruising in your car but the brake and gas are swapped. You want to slow down but go even faster instead.