ROBOTC.net forums
http://www.robotc.net/forums/

NXTCAMgetAverageCenter
http://www.robotc.net/forums/viewtopic.php?f=41&t=3335
Page 1 of 1

Author:  Thibaud [ Mon Jun 20, 2011 3:49 pm ]
Post subject:  NXTCAMgetAverageCenter

Hi,

What is the max X and Y value for the NXTCAMgetAverageCenter function? i got X = 248 is a normal value?

Thanks

Author:  mightor [ Mon Jun 20, 2011 4:10 pm ]
Post subject:  Re: NXTCAMgetAverageCenter

No, that doesn't seem very normal. Can I see your entire program?

- Xander

Author:  Thibaud [ Mon Jun 20, 2011 4:52 pm ]
Post subject:  Re: NXTCAMgetAverageCenter

yes sure :

Code:

int contactPressed;
int distLeft;
int distRight;
int dirToSeek;
tSensors touch;
tSensors cam;
tSensors usLeft;
tSensors usRight;
blob_array _blobs, _blobs_temp;
int nbBlobs;

void initSensors(tSensors _cam,tSensors _touch,tSensors _left,tSensors _right)
{
 touch = _touch;
 cam = _cam;
 usLeft = _left;
 usRight = _right;
 USreset(usLeft);
 USreset(usRight);
// USsetSingleMode(usLeft);
 //USsetSingleMode(usRight);
 NXTCAMinit(_cam);

}

task readSensors()
{
  USsetOff(usLeft);
  USsetOff(usRight);


  memset(_blobs, 0, sizeof(blob_array));
  memset(_blobs_temp, 0, sizeof(blob_array));

  while(1)
  {
    contactPressed = SensorValue[touch];

    USsetSingleMode(LeftUS);
    distLeft = USreadDist(LeftUS);
    wait1Msec(50);
    USsetOff(LeftUS);

    USsetSingleMode(RightUS);
    distRight = USreadDist(RightUS);
    wait1Msec(50);
    USsetOff(RightUS);

    nbBlobs = NXTCAMgetBlobs(cam, _blobs, true);//retourne le nombre de blob trouve
    _blobs = _blobs_temp;

  }
}

task main()
{
  initSensors(NXTcam, BallContact, LeftUS, RightUS);
  StartTask(readSensors);

  int _x, _y;
  motor[LeftMotor] = 0;
  motor[RightMotor] = 0;
  while(1)
  {
    if(nbBlobs <= 0)
    {
 
      motor[LeftMotor] = 0;
      motor[RightMotor] = 0;
    }
    else
    {
          writeDebugStreamLine("Blob : %d",nbBlobs);
        if(nbBlobs !=0 )
        {
           NXTCAMgetAverageCenter(_blobs, nbBlobs,0, _x, _y);
           writeDebugStreamLine("x:%d; y:%d",_x,_y);
          
           if(_x != 0 && _y!= 0)
           {
              if(_x > ((88) +5))
              {
                motor[LeftMotor] =  - (_x - 88)/5;
                motor[RightMotor] = (_x-88)/5;
                writeDebugStreamLine("On my Right!!!");
              }
              else if(_x <((88) - 5))
              {
                motor[LeftMotor] = (88 - _x)/5;
                motor[RightMotor] = -(88 - _x)/5;
                          writeDebugStreamLine("On my Left!!!");

              }else
              {
                motor[LeftMotor] = 0;
                motor[RightMotor] = 0;
              }
           }
        }

    }
    EndTimeSlice();
    wait1Msec(100);
  }

}


sometimes this kind of value appears.

Author:  mightor [ Mon Jun 20, 2011 5:20 pm ]
Post subject:  Re: NXTCAMgetAverageCenter

I'll take a look in the morning, it's a bit late now :)

- Xander

Author:  mightor [ Tue Jun 21, 2011 3:41 am ]
Post subject:  Re: NXTCAMgetAverageCenter

Your program is not complete. Where are the sensor configuration pragmas?

- Xander

Author:  mightor [ Tue Jun 21, 2011 4:07 am ]
Post subject:  Re: NXTCAMgetAverageCenter

I modified your program to make it more readable. You should use consistent sensor naming. ALWAYS use the Motor and Sensor configuration UI. Consider getting rid of the sensor reading task, it serves no real purpose.

Code:
#pragma config(Sensor, S1,     NXTCAM,              sensorI2CCustomFastSkipStates9V)
#pragma config(Sensor, S2,     TOUCH,               sensorTouch)
#pragma config(Sensor, S3,     USRIGHT,             sensorI2CCustom9V)
#pragma config(Sensor, S4,     USLEFT,              sensorI2CCustom9V)
#pragma config(Motor,  motorA,          MRIGHT,        tmotorNormal, PIDControl, encoder)
#pragma config(Motor,  motorB,          MLEFT,         tmotorNormal, PIDControl, encoder)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

#include "drivers/LEGOUS-driver.h"
#include "drivers/NXTCam-driver.h"

int contactPressed;
int distLeft;
int distRight;
int dirToSeek;
blob_array _blobs, _blobs_temp;
int nbBlobs;


task readSensors()
{
  USsetOff(USLEFT);
  USsetOff(USRIGHT);


  memset(_blobs, 0, sizeof(blob_array));
  memset(_blobs_temp, 0, sizeof(blob_array));

  while(1)
  {
/*
    contactPressed = SensorValue[TOUCH];

    USsetSingleMode(USRIGHT);
    distLeft = USreadDist(USLEFT);
    wait1Msec(50);
    USsetOff(USLEFT);

    USsetSingleMode(USRIGHT);
    distRight = USreadDist(USRIGHT);
    wait1Msec(50);
    USsetOff(USRIGHT);
*/
    nbBlobs = NXTCAMgetBlobs(NXTCAM, _blobs, true);   //retourne le nombre de blob trouve

    // This part should not be interrupted.
    hogCPU();
    memcpy(_blobs, _blobs_temp, sizeof(_blobs_temp);
    releaseCPU();
  }
}

task main()
{
  // Initialise the sensors.
  USreset(USLEFT);
  USreset(USRIGHT);
  // USsetSingleMode(USLEFT);
  //USsetSingleMode(USRIGHT);
  NXTCAMinit(NXTCAM);
 
  StartTask(readSensors);
 
  int _x, _y;
  motor[MLEFT] = 0;
  motor[MRIGHT] = 0;
  while(1)
  {
    if(nbBlobs <= 0)
    {
      motor[MLEFT] = 0;
      motor[MRIGHT] = 0;
    }
    else
    {
      writeDebugStreamLine("Blob : %d",nbBlobs);
      if(nbBlobs !=0 )
      {
        NXTCAMgetAverageCenter(_blobs, nbBlobs,0, _x, _y);
        writeDebugStreamLine("x:%d; y:%d",_x,_y);
       
        if(_x != 0 && _y!= 0)
        {
          if(_x > ((88) +5))
          {
            motor[MLEFT] =  - (_x - 88)/5;
            motor[MRIGHT] = (_x-88)/5;
            writeDebugStreamLine("On my MRIGHT!!!");
          }
          else if(_x <((88) - 5))
          {
            motor[MLEFT] = (88 - _x)/5;
            motor[MRIGHT] = -(88 - _x)/5;
            writeDebugStreamLine("On my MLEFT!!!");
          }
          else
          {
            motor[MLEFT] = 0;
            motor[MRIGHT] = 0;
          }
        }
      }
    }
    wait1Msec(100);
  }
}


- Xander

Author:  Thibaud [ Tue Jun 21, 2011 4:08 am ]
Post subject:  Re: NXTCAMgetAverageCenter

sorry the copy paste is not complete :) i will post the complement later

Author:  Thibaud [ Tue Jun 21, 2011 4:14 am ]
Post subject:  Re: NXTCAMgetAverageCenter

thanks for your answer, i'm using non consistent sensor naming because my sensor task reading was in a different file . You think it's because i'm not using consistent naming that i find some errors? or because i don't hog the CPU?

Author:  mightor [ Tue Jun 21, 2011 4:35 am ]
Post subject:  Re: NXTCAMgetAverageCenter

You were not copying the array from one to the other, merely assigning another variable to the same block of memory. That is why I am doing a proper memcpy() call. To make sure that it isn't being read while the copying is happening, I block the CPU.

- Xander

Page 1 of 1 All times are UTC - 5 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/