View unanswered posts | View active topics It is currently Fri Apr 18, 2014 5:02 pm






Reply to topic  [ 7 posts ] 
Fastest Code Execution 
Author Message
Rookie

Joined: Wed Feb 09, 2011 7:28 pm
Posts: 14
Post Fastest Code Execution
Hello,
I am using the Hitechnic NXT gyro sensor to control our robot's turn degrees. Right now I am just testing the code on a Lego robot. I have found that the ability of the nxt to run through the code quickly seems to be inhibiting me. I have one task calculating the gyro average while having a function calculate the degrees turned. Basically, my problem is this, when the robot turns at a very slow speed, the robot stops precisely enough, but when it spins at a faster speed, it will overshoot the number of degrees I want it to spin. What is the quickest way to make the program execute?
Thanks for any help.

Here's the code: (Some things are in comments which I used to be using but am not using at the moment)

Code:
#pragma config(Sensor, S1,     gyro,                sensorRawValue)
#pragma config(Motor,  motorA,          left,          tmotorNormal, PIDControl, reversed, encoder)
#pragma config(Motor,  motorC,          right,         tmotorNormal, PIDControl, reversed, encoder)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

#include "drivers/HTGYRO-driver.h"

float average = 1;
int readings[1000]; //from gyroUpdate

/* //right now the program is nut using this task
task gyroUpdate()
{
  int readings[1000];
  int up = 0;
  int up2;
  int sum;
  do
  {
    readings[up] = HTGYROreadRot(gyro);
    up++;
    up2 = 0;
    sum = 0;
    do
    {
      sum += readings[up2];
      up2++;
    }
    while(up2 < up);
    average = sum / up;
  }
  while(up < 1000);
}
*/

void untilAngleR(float angleOffset, int speed) //a right turn degree monitor
{

  int up = 0;
  int up2;
  int sum; //from gyroUpdate

  float subtract = nPgmTime / 1000;

  //StartTask(gyroUpdate);
  motor[left] = speed;
  motor[right] = speed * -1;
  while((((nPgmTime / 1000) - subtract) * average) < angleOffset)
  {
    readings[up] = HTGYROreadRot(gyro); //from gyroUpdate
    up++;
    up2 = 0;
    sum = 0;
    do
    {
      sum += readings[up2];
      up2++;
    }
    while(up2 < up);      //                       ^
    average = sum / up; //from gyroUpdate |
  }
  motor[left] = 0;
  motor[right] = 0;
  //StopTask(gyroUpdate);
  nxtDisplayString(3,"%f",(nPgmTime / 1000) - subtract);
  nxtDisplayString(4,"%f",((nPgmTime / 1000) - subtract) * average);

  return;
}

task main()
{
  HTGYROstartCal(gyro);
  wait10Msec(300);

  untilAngleR(90,5); //right now 5% speed is good, but 10% is too much

  wait10Msec(500);
}


Wed Feb 09, 2011 7:58 pm
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1343
Post Re: Fastest Code Execution
I don't understand why you need to do all this complex averaging on a separate task. If you want to turn a given angle as fast as you can without overshooting, why can't you do PID controlled turn? i.e. You turn hard initially but when you are close to the target angle, the turn will slow down.


Wed Feb 09, 2011 10:03 pm
Profile
Rookie

Joined: Wed Feb 09, 2011 7:28 pm
Posts: 14
Post Re: Fastest Code Execution
Well, I am not all that familiar with PID, but I'm pretty that there is no way to read the angle directly off the gyro, so somehow I must use the speed and time to calculate the number of degrees I have turned. (unless there's something I don't know about) I do think that the idea of turning quickly and then slowing down sounds like a good one. Is that all you meant by PID control?


Thu Feb 10, 2011 1:15 pm
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1343
Post Re: Fastest Code Execution
Yes, that's integration. Integrating angular velocity against time gives you angle. Yes, PID control is about approaching your set target as quickly as possible but slowing down when you are almost there. But these are two separate problems. We have a gyro library module that does the integration and gives you the heading. If you want to take a look at how it is implemented, it is here:
http://proj.titanrobotics.net/hg/Frc/20 ... nxt/gyro.h
There are many articles on the Internet about PID control theory if you are interested in reading more about it:
http://en.wikipedia.org/wiki/PID_control


Thu Feb 10, 2011 1:26 pm
Profile
Rookie

Joined: Wed Feb 09, 2011 7:28 pm
Posts: 14
Post Re: Fastest Code Execution
MHTS wrote:
Yes, that's integration. Integrating angular velocity against time gives you angle. Yes, PID control is about approaching your set target as quickly as possible but slowing down when you are almost there. But these are two separate problems. We have a gyro library module that does the integration and gives you the heading. If you want to take a look at how it is implemented, it is here:
http://proj.titanrobotics.net/hg/Frc/20 ... nxt/gyro.h
There are many articles on the Internet about PID control theory if you are interested in reading more about it:
http://en.wikipedia.org/wiki/PID_control


Thanks :)


Thu Feb 10, 2011 1:40 pm
Profile
Rookie

Joined: Sat Jan 14, 2012 12:13 pm
Posts: 3
Post Re: Fastest Code Execution
MHTS wrote:
Yes, that's integration. Integrating angular velocity against time gives you angle. Yes, PID control is about approaching your set target as quickly as possible but slowing down when you are almost there. But these are two separate problems. We have a gyro library module that does the integration and gives you the heading. If you want to take a look at how it is implemented, it is here:
http://proj.titanrobotics.net/hg/Frc/20 ... nxt/gyro.h
There are many articles on the Internet about PID control theory if you are interested in reading more about it:
http://en.wikipedia.org/wiki/PID_control



The header file won't load in my robotc, I'm trying to understand the gyro better, but I can't seem to figure out how to time the calculations accurately. I get many errors when including the header file, gyro.h, example:

The small bit of code it starts out the errors with:

Code:
void
GyroCal(
    __out GYRO &gyro,
    __in  int numSamples,
    __in  int calInterval
    )
{


    File "demo.c" compiled on Mar 28 2012 20:04:55
    *Warning*:Variable '__out' declaration must be qualified with type. Type 'short' used.
    **Error**:Ummatched left parenthesis '('
    **Error**:Expected->')'. Found 'GYRO'
    **Error**:No body declared for procedure
    **Error**:Expected->'}'. Found 'GYRO'
    **Error**:Type mismatch with previous 'const' declaration for variable with same name
    **Error**:Expected->';'. Found 'int'
    **Error**:Duplicate variable declaration of '__in'
    **Error**:Expected->';'. Found 'int'
    **Error**:Missing ';' before ')'
    **Error**:Unexpected scanner token-> ')'

Code:
#pragma config(Sensor, S1,     Touch,          sensorTouch)
#pragma config(Sensor, S2,     gyro,           sensorI2CHiTechnicGyro)
#pragma config(Sensor, S3,     Color,          sensorCOLORFULL)
#pragma config(Motor,  motorB,          Right,         tmotorNormal, PIDControl, encoder)
#pragma config(Motor,  motorC,          Left,          tmotorNormal, PIDControl, encoder)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

# "gyro.h"


It even makes my 'task main' "**Error**:Duplicate definition for task 'main'."


Wed Mar 28, 2012 9:19 pm
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1343
Post Re: Fastest Code Execution
Add the following lines before you include gyro.h, like this:
Code:
#define __in
#define __out
#define __inout
#define TFuncName(s)
#define TLevel(n)
#define TEnter()
#define TExit()

#include "gyro.h"

That should take care of all the related errors.


Wed Mar 28, 2012 10:07 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 7 posts ] 

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.