PROS Help: Gyroscope

Good evening ladies and gentlemen, and viewing robots of all ages!

Over the past few days, I have been trying to work on the gyroscope of my robot in PROS. If any of you have experience with PROS code, please continue to read this forum!

The intention of my program it to be able to make the robot turn based on a Gyro, a simple task at heart.

Given that I am a beginner to the PROS workspace and coding environment I tried to take my code from my previous program software and translate it into PROS, most of this worked except for the gyroscope.

Note: It had previously worker on the different software.

I’ll give you the code I used below with a quick description:

The below is code from my var.h file that deals with all global variables such as motor ports, sensor ports, and soon to be much more:

#include <API.h>
Gyro gyro1;

#define FlatbedMotorLeft 10 //Flatbed motor on spot 10
#define ArmMotorRightUp 9 //Arm motor on spot 9
#define ArmMotorRightY 8 //Arm motor on spot 8
#define RFW 6 //Right front wheel motor on spot 7
#define RBW 7 //Right back wheel motor on spot 6
#define LBW 4 //Left back wheel motor on spot 5
#define LFW 5 //Left front wheelmotor on spot 4
#define ArmMotorLeftY 3 //Arm motor on spot 3
#define ArmMotorLeftUp 2//Arm motor on spot 2
#define FlatbedMotorRight 1//Flatbed motor on spot 1

//Sensors (Digital)
#define DownBumpP 12
#define FBBumpP 8
#define ArmStableBump 10
#define ArmVertBump 9
#define BackBump 11

//Sensors (Analog)
#define GYR 2
#define AutoPot 1
#define LineTracker 3

As you can see, at the top of the program gyro1 is declared as type Gyro, and GYR (later used) is defined under the Analog Sensors section of variables.

Below is the code from the init.c file:

void initialize() {
  gyro1 = gyroInit(GYR, 0);

As you can see, gyro1 is defined as the initialization of GYR (port of gyroscope) and the default setting for the gyro, which is 0.

Below is the function used to turn the robot a certain degrees based on my asking:

void FuncRotateGyro(int speed, int GyroRequest) { //based on starting point; not added on
  int Target = GyroRequest;
  int pos = gyroGet(gyro1);
  if(pos > Target) { //required to decrease (turn left)
    while(pos > Target + 150 && pos < Target - 150) {
      DriveRotateLeftSpeed(speed, speed, 0, 0); }
      pos = gyroGet(gyro1);
    while(pos > Target + 10 && pos < Target - 10) {
      DriveRotateLeftSpeed(2*speed/3, 2*speed/3, 0, 0);
      pos = gyroGet(gyro1); }
    driveSet(0, 9);
  if(gyroGet(gyro1) < Target) {
    while(pos > Target + 150 && pos < Target - 150) {
      DriveRotateRightSpeed(speed, speed, 0, 0);
      pos = gyroGet(gyro1); }
    while(pos > Target + 10 && pos > Target - 10) {
      DriveRotateRightSpeed(2*speed/3, 2*speed/3, 0, 0);
      pos = gyroGet(gyro1); }
      driveSet(0, 0);

On top of that, I also made a program in simpler forms that uses a while loop and makes everything much simpler. I had no success.

On top of that, I made a test program with the same definitions of init.c and var.h except one minor tweak:

if(joystickGetDigital(1, 7, 4) == 1) {
while(1 == 1) {
int gyrogyro = gyroGet(gyro1);
printf("%d\n", gyrogyro);

The main issue with all of this is that the gyro returns a value of 0 or it remains to spin for ever and ever. If anyone could help me on this, please do. More information (if needed) will be given later on by request.

Everything in your code looks great, the one thing you will need to add to it in order for it to work would be the intiallization sequence… what you need to do is have it calibrate by simply have the sensor=none then have a wait 500msec, then have the port equal the desired sensor and then have the robot sit two seconds before moving, we encountered a similar problem. Heres some example code…

task main()
//Completely clear out any previous sensor readings by setting the port to “sensorNone”
SensorType[in8] = sensorNone;
//Reconfigure Analog Port 8 as a Gyro sensor and allow time for ROBOTC to calibrate it
SensorType[in8] = sensorGyro;

He’s using PROS not RobotC, calibration is different.

void operatorControl() {
    while (1) {
        if (joystickGetDigital(1, 7, 4) == 1) {
            while (1 == 1) {
                int gyrogyro = gyroGet(gyro1);
                printf("%d\n", gyrogyro);

Once your code reaches the inner


loop, it stays there forever. Because you have no


in this inner loop, the processor cannot allocate enough time to run any other tasks, including the internal gyroscope tasks.

If you put another


into the inner


loop, the internal gyroscope tasks will be able to run and you will get values out.

1 Like

Thank you. I have added the delay and will be testing shortly!

I did exactly what you said and the problem persists. No value other than 0 is given in the PROS terminal.

Hi Aditya,

We tested your code, and from the information you have given us, the code works.

Some things to check:

  • Make sure that when you call

gyroInit(port, multiplier)

, the multiplier should be zero unless you need to scale differently (you don’t)

  • Make sure that nothing else is starving the CPU of resources (if you have any tasks running at the same priority, make sure that they have delays)
  • If your code is stuck printing things in the inner while loop, the flasher may not be able to communicate properly with the Cortex (though you would get an error message while flashing). To fix this, restart the cortex.

When you tested, you were getting values that were not 0?

The init multiplier is 0.

The program I gave you for the print screen is all that is happening; nothing else. Nothing else is starving the CPU.

The brain has no issue with printing.

If you could send an email (email given through DM) with the files that you made on your copy, that would be very helpful!

Yes, we tested the code you sent us with the init multiplier value of 0 and we were getting non-zero values as we moved the gyro. Again, with the details you have provided both here and in our email discussion, we can find nothing else wrong.

For further testing, try plugging a potentiometer into the gyroscope port (2) while still using the same code that thinks a gyroscope is plugged into port 2. Twisting the potentiometer should give you some non-zero values that won’t appear make any sense, but at least you now know that something is up with the gyroscope.

I see. Thanks for the quick replies here and on our email discussion. It appears that the gyro may have an issue; which is an odd because I tested it before with Easy C v4.

Thanks for the help.

Did you plug your wires correctly onto the gyro (black ground wire from Cortex goes next to the silkscreened ‘B’ on the gyro)?