View unanswered posts | View active topics It is currently Fri Nov 22, 2019 11:12 am






Reply to topic  [ 36 posts ]  Go to page Previous  1, 2, 3  Next
Gyro PID 
Author Message
Novice

Joined: Tue Dec 16, 2014 10:25 am
Posts: 81
Post Re: Gyro PID
Although won't this have the same effect as my original code that was having the arcing problem? The original code still had no waits or while loops so it still should have been called periodically

_________________
Thanks!


Thu Dec 18, 2014 1:21 am
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1523
Post Re: Gyro PID
I think your previous code works fine. I was just addressing your concern on the accuracy of the gyro that if you integrate it more frequently, the accuracy would increase. Regarding the "arcing problem", I think it was caused by K being too small. Regarding the "gyro reading" problem, since your format string was wrong, I suspect the gyro wasn't stuck or frozen, it is just the displayed info was not correct. Please fix the format string and try again and see if that fixed it.


Thu Dec 18, 2014 1:38 am
Profile
Novice

Joined: Tue Dec 16, 2014 10:25 am
Posts: 81
Post Re: Gyro PID
Ok thanks!

_________________
Thanks!


Thu Dec 18, 2014 8:34 am
Profile
Novice

Joined: Tue Dec 16, 2014 10:25 am
Posts: 81
Post Re: Gyro PID
.

_________________
Thanks!


Last edited by akash329dsouza on Sat Dec 20, 2014 11:45 am, edited 1 time in total.



Sat Dec 20, 2014 10:41 am
Profile
Novice

Joined: Tue Dec 16, 2014 10:25 am
Posts: 81
Post Re: Gyro PID
Ok so with this exact code :
Code:
#pragma config(Hubs,  S1, HTMotor,  HTMotor,  none,     none)
#pragma config(Sensor, S1,     ,               sensorI2CMuxController)
#pragma config(Motor,  mtr_S1_C1_1,     motorD,        tmotorTetrix, PIDControl, encoder)
#pragma config(Motor,  mtr_S1_C1_2,     motorE,        tmotorTetrix, PIDControl, encoder)
#pragma config(Motor,  mtr_S1_C2_1,     motorF,        tmotorTetrix, PIDControl, reversed, encoder)
#pragma config(Motor,  mtr_S1_C2_2,     motorG,        tmotorTetrix, PIDControl, reversed, encoder)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//
#include "gyro.h"
typedef struct
{
int DesiredAngle;
float MotorCorrection;
float MotorSpeed;
float ProportionalGain;
}Math;
Math VLF;
GYRO HDGyro; //Creates Gyro Struct as a global struct

task main()
{
   VLF.DesiredAngle = 0; //Desired Angle of where you want to be (Set to current Gyro Position)
   VLF.MotorSpeed = 30; //Speed motors start at
   VLF.ProportionalGain = 5; //K- value
   unsigned long nextPeriod = nPgmTime;
   while(true){
      GyroTask(HDGyro);
      if (nPgmTime >= nextPeriod) {
         nextPeriod += 150;
         VLF.MotorCorrection = VLF.ProportionalGain*(HDGyro.heading - VLF.DesiredAngle); //Calculates Motor Correction to make
         nxtDisplayCenteredBigTextLine(4, "%5.1f",HDGyro.heading); //Displays the current Gyro Heading
         motor[motorD] = VLF.MotorSpeed + VLF.MotorCorrection; //Moves and applies motor correction (You may need to adjust this depending on if the sign is correct)
         motor[motorE] = VLF.MotorSpeed + VLF.MotorCorrection; //Moves and applies motor correction
         motor[motorF] = VLF.MotorSpeed - VLF.MotorCorrection; //Moves and applies motor correction
         motor[motorG] = VLF.MotorSpeed - VLF.MotorCorrection; //Moves and applies motor correction
      }
      EndTimeSlice();
   }
 }

The robot is still arcing to the left. We don't know why this is happening . Does anyone know why this is happening? We were thinking it could be gyro drift but when we let it sit still it stays at zero for well over 3 minutes. We turned PID of and it still is arcing but one thing we noticed is it is always left so we tried adjusting it so left side went faster but that didn't seem to work so we abandoned the idea , do you know why this is happening?

_________________
Thanks!


Sat Dec 20, 2014 11:44 am
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1523
Post Re: Gyro PID
If that was the exact code, then you have some major bugs in it. First off, your code did not call GyroInit. Without GyroInit, the gyro module wouldn't know which port the gyro sensor is connected to and there is no way for it to integrate. That's probably why it sits there at zero. I am even surprise it ran.
You need to add the GyroInit call and make sure you correctly specify the sensor port. Then the next thing is to make sure the gyro does work. You need to manually turn the robot around and check if the heading is indeed the amount of angle you turn (e.g. If you manually turn the robot a complete round clockwise, you should read 360). Once you have proven the gyro is giving you correct values, then you can debug the code.
Secondly, I noticed all your motors have RobotC built-in PID turned ON, you need to turn them OFF because your code is doing your own PID. This way you want to make sure the RobotC built-in PID does not interfere the value you program to the motors.


Sat Dec 20, 2014 7:26 pm
Profile
Novice

Joined: Tue Dec 16, 2014 10:25 am
Posts: 81
Post Re: Gyro PID
So we tried all that today and fixed the errors but it still seems to be arcing?

_________________
Thanks!


Sat Dec 20, 2014 10:13 pm
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1523
Post Re: Gyro PID
Would you show me the fixed code? Also, did you test the gyro and made sure it did work?


Sat Dec 20, 2014 11:00 pm
Profile
Novice

Joined: Tue Dec 16, 2014 10:25 am
Posts: 81
Post Re: Gyro PID
Here is the fixed code when it was last updated , it was this without PID enabled.
Code:
#pragma config(Hubs,  S1, HTMotor,  HTMotor,  none,     none)
#pragma config(Sensor, S1,     ,               sensorI2CMuxController)
#pragma config(Motor,  mtr_S1_C1_1,     motorD,        tmotorTetrix, PIDControl, encoder)
#pragma config(Motor,  mtr_S1_C1_2,     motorE,        tmotorTetrix, PIDControl, encoder)
#pragma config(Motor,  mtr_S1_C2_1,     motorF,        tmotorTetrix, PIDControl, reversed, encoder)
#pragma config(Motor,  mtr_S1_C2_2,     motorG,        tmotorTetrix, PIDControl, reversed, encoder)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

#include "gyro.h"
typedef struct
{
   int DesiredAngle;
   float MotorCorrection;
   float MotorSpeed;
   float ProportionalGain;
}Math;
Math VLF;
GYRO HDGyro; //Creates Gyro Struct as a global struct

task main()
{
   GyroInit(HDGyro,S2);
   VLF.DesiredAngle = 0; //Desired Angle of where you want to be (Set to current Gyro Position)
   VLF.MotorSpeed = 30; //Speed motors start at
   VLF.ProportionalGain = 0; //K- value
   unsigned long nextPeriod = nPgmTime;
   while(true){
      GyroTask(HDGyro);
      if (nPgmTime >= nextPeriod) {
         nextPeriod += 150;
         VLF.MotorCorrection = VLF.ProportionalGain*(HDGyro.heading - VLF.DesiredAngle); //Calculates Motor Correction to make
         nxtDisplayCenteredBigTextLine(4, "%5.1f",HDGyro.heading); //Displays the current Gyro Heading
         motor[motorD] = VLF.MotorSpeed + VLF.MotorCorrection; //Moves and applies motor correction (You may need to adjust this depending on if the sign is correct)
         motor[motorE] = VLF.MotorSpeed + VLF.MotorCorrection; //Moves and applies motor correction
         motor[motorF] = (0.1 + VLF.MotorSpeed) - VLF.MotorCorrection; //Moves and applies motor correction
         motor[motorG] = (0.1 + VLF.MotorSpeed) - VLF.MotorCorrection; //Moves and applies motor correction
      }
      EndTimeSlice();
   }
}

_________________
Thanks!


Sun Dec 21, 2014 1:08 am
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1523
Post Re: Gyro PID
You have many issues with this code.
First, the RobotC built-in PID is not disabled. You need to disable it with the "Motors and Sensors setup" dialog on the motors tab where you need to uncheck the "PID control" box for all the drive motors.
Secondly, you did not declare the gyro sensor in the "Motors and Sensors setup" dialog. Although you have specified the gyro is on sensor port 2 when calling GyroInit, it won't work unless you declare the gyro sensor in the sensors tab. It will produce a pragma that tells RobotC there is a Gyro sensor on port 2 so that it will initialize the sensor correctly. Without it, SensorValue[S2] will not return correct values.
Thirdly, your ProportionalGain is ZERO. That means you have disabled your PID control. That means your Correction is always ZERO.
Finally, I have no idea why you added 0.1 to motors F and G. Motor values are integers, so adding 0.1 is practically adding ZERO.
Also, I assume motors D and E are the right wheels and F and G are the left wheels and the gyro is mounted horizontally and right side up. If these are not true, the code will not work correctly.


Sun Dec 21, 2014 5:08 am
Profile
Novice

Joined: Tue Dec 16, 2014 10:25 am
Posts: 81
Post Re: Gyro PID
Oh, sorry I don't know what I was doing. I gave you the code for our testing instead of the original code. Although we didn't setup the Gyro in the setup it was still working perfectly on the screen though.:
Code:
#pragma config(Hubs,  S1, HTMotor,  HTMotor,  none,     none)
#pragma config(Sensor, S1,     ,               sensorI2CMuxController)
#pragma config(Motor,  mtr_S1_C1_1,     motorD,        tmotorTetrix, openLoop, encoder)
#pragma config(Motor,  mtr_S1_C1_2,     motorE,        tmotorTetrix, openLoop, encoder)
#pragma config(Motor,  mtr_S1_C2_1,     motorF,        tmotorTetrix, openLoop, reversed, encoder)
#pragma config(Motor,  mtr_S1_C2_2,     motorG,        tmotorTetrix, openLoop, reversed, encoder)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

#include "gyro.h"
typedef struct
{
   int DesiredAngle;
   float MotorCorrection;
   float MotorSpeed;
   float ProportionalGain;
}Math;
Math VLF;
GYRO HDGyro; //Creates Gyro Struct as a global struct

task main()
{
   GyroInit(HDGyro,S2);
   VLF.DesiredAngle = 0; //Desired Angle of where you want to be (Set to current Gyro Position)
   VLF.MotorSpeed = 30; //Speed motors start at
   VLF.ProportionalGain = 5; //K- value
   unsigned long nextPeriod = nPgmTime;
   while(true){
      GyroTask(HDGyro);
      if (nPgmTime >= nextPeriod) {
         nextPeriod += 150;
         VLF.MotorCorrection = VLF.ProportionalGain*(HDGyro.heading - VLF.DesiredAngle); //Calculates Motor Correction to make
         nxtDisplayCenteredBigTextLine(4, "%5.1f",HDGyro.heading); //Displays the current Gyro Heading
         motor[motorD] = VLF.MotorSpeed + VLF.MotorCorrection; //Moves and applies motor correction (You may need to adjust this depending on if the sign is correct)
         motor[motorE] = VLF.MotorSpeed + VLF.MotorCorrection; //Moves and applies motor correction
         motor[motorF] = VLF.MotorSpeed - VLF.MotorCorrection; //Moves and applies motor correction
         motor[motorG] = VLF.MotorSpeed - VLF.MotorCorrection; //Moves and applies motor correction
      }
      EndTimeSlice();
   }
}

_________________
Thanks!


Sun Dec 21, 2014 11:37 am
Profile
Novice

Joined: Tue Dec 16, 2014 10:25 am
Posts: 81
Post Re: Gyro PID
Also just a quick question.
Code:
#if 0
///
///
/// <module name="gyro.h" />
///
/// <summary>
///     This module contains the library functions for the gyro sensor for no deadband + noise.
/// </summary>
///
/// <remarks>
///     Environment: RobotC for Lego Mindstorms NXT.
/// </remarks>
#endif

in your Gyro.h what does the #if 0 and #endif do? It wouldn't execute any code would it. I'm just wondering because for some reason I'm getting this error for it:*Warning*:Missing ';' has been automatically inserted by compiler. It makes no sense because even if I add a semicolen the error stays there.

_________________
Thanks!


Sun Dec 21, 2014 1:46 pm
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1523
Post Re: Gyro PID
Regarding your code, it now looks fine except you do need to declare the gyro in the "Motors and Sensors setup".
Also, did you test the gyro if it returns 360 when you turn the robot a complete round clockwise? It should give you -360 if you turn a complete round anti-clockwise. This will tell you definitively that the gyro is working correctly. You also need to double check if motor D and E are the right wheels and F and G are the left wheels. If they are reversed, you robot will curve to one side more and more. Finally, if after everything checked out and the robot is still curving, you need to turn off the power and turn the wheels by hand to see if any particular wheel has more friction than the others. If a particular wheel has more friction than the others, you need to fix whatever mechanical problems that caused it.
Regarding the "#if 0". Don't worry about that. It's is equivalent to commenting out a section of code (same as /* and */). It was in there to make a document tool happy (a tool that auto generate API document).
Where in the code did you get the ";" warning? Sometimes, you need to look at the code a line above or below the line it's complaining about. The compiler may mean the previous line is missing a semi-colon.


Sun Dec 21, 2014 4:14 pm
Profile
Novice

Joined: Tue Dec 16, 2014 10:25 am
Posts: 81
Post Re: Gyro PID
Thanks for all your help! We applied all your suggestions and balanced the weight out and its working perfectly now!

_________________
Thanks!


Mon Dec 22, 2014 11:34 pm
Profile
Novice

Joined: Tue Dec 16, 2014 10:25 am
Posts: 81
Post Re: Gyro PID
I was just wondering could you explain to me how your gyro driver works and how it's different than Xanders? Because for some reason yours was able to calculate the drift out almost entirely for us and with xanders it was going up nearly one per second. So I wanted to figure out why. Thanks!

_________________
Thanks!


Thu Dec 25, 2014 12:19 am
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 36 posts ]  Go to page Previous  1, 2, 3  Next

Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.