View unanswered posts | View active topics It is currently Thu Oct 02, 2014 4:15 am






Reply to topic  [ 20 posts ]  Go to page 1, 2  Next
Having some trouble understanding the functions for NXTCam. 
Author Message
Rookie

Joined: Sun Jan 27, 2013 8:01 pm
Posts: 18
Post Having some trouble understanding the functions for NXTCam.
For the last couple days I have been working on a project to get my NXT motors to simulate the movements I do with my head. (For example: If I look up, it moves a motor to point up at the same angle that I do). I know exactly what I am trying to do, but I am unfamiliar with the commands/functions of the NXTCam and I have just been coping code from sample programs, etc to see if it will just magically work and that is the only reason I have this much code in the program. I have commented into the code what I THINK each line means but I really have no clue.
Code:
#pragma config(Sensor, S1,     cam,                 sensorI2CCustomFastSkipStates)//setup sensor
//SETUP
#include "mindsensors-nxtcam.h" //include commands from mindsensors
#include "nxtcamlib.c" //include commands from nxtcamlib

float x; //setup x as a variable
float y; //setup y as a variable
int _blob1_center;
short xmotor = motorA; //let xmotor be used in place of motorA
short ymotor = motorB; //let ymotor be used in place of motorB
const tSensors cam  = (tSensors) S1; //not quite sure
short t = 100; //time alloted for motor to move (in thousandths of a second)
short d = 15; //"color's" distance from middle of head (in cm)
short s = 50; //motor speed (as a % of fastest possible speed)
float xdegrees; //setup xdegrees as a variable
float ydegrees; //setup ydegrees as a variable
void moveymotor()//move ymotor when conditions exist
         {
         if (nMotorEncoder[ymotor] < nMotorEncoderTarget[ymotor])
            {
            ymotor = s;
            wait1Msec(t);
            }
         if (nMotorEncoder[ymotor] > nMotorEncoderTarget[ymotor])
            {
            ymotor = -s;
            wait1Msec(t);
            }
         }
void movexmotor() //move xmotor to designated encoder target when conditions exist
         {
         if (nMotorEncoder[xmotor] < nMotorEncoderTarget[xmotor])
            {
            xmotor = s;
            wait1Msec(t);
            }
         if (nMotorEncoder[xmotor] > nMotorEncoderTarget[xmotor])
            {
            xmotor = -s;
            wait1Msec(t);
            }
         }
void encodertargets()//set the encoder targets of ymotor and xmotor to the same degree as my head is looking
         {
         nMotorEncoderTarget[xmotor] = atan(_blob1_center/d);
         nMotorEncoderTarget[ymotor] = atan(_blob1_center/d);
         }
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////!!!!Actual Code!!!!////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


         
task main()
{

      while (true)
      {
         eraseDisplay(); //clears the display
         blob_array _blobs; //not sure
         bool _condensed = true; //condense all close blobs into one?
         NXTCAMinit(cam); //turns the camera on
         NXTCAMgetBlobs(cam, _blobs, _condensed); //
      
         nMotorEncoder[xmotor] = 0; //sets the current
         nMotorEncoder[ymotor] = 0;
         x = (_blobs[0].x2 + _blobs[0].x1)/2;
         y = (_blobs[0].y2 + _blobs[0].y1)/2;
         xdegrees =   x - 50; //xdegrees = the middle of the block on the x axis - the middle of the camera's vision
         ydegrees = y - 32; //ydegrees = the middle of the block on the y axis = the middle of the camera's vision
         encodertargets(); //set encoder targets
         movexmotor(); //move motors to encoder targets
         //short movexmotor (short nMotorEncoderTarget = xdegrees)
      }
}      


Fri Feb 15, 2013 2:05 am
Profile
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3223
Location: Rotterdam, The Netherlands
Post Re: Having some trouble understanding the functions for NXTC
Don't mix the nxtcamlib and my driver suite one.

= Xander

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]


Fri Feb 15, 2013 5:21 am
Profile WWW
Rookie

Joined: Sun Jan 27, 2013 8:01 pm
Posts: 18
Post Re: Having some trouble understanding the functions for NXTC
Ok, I got rid of "nxtcamlib.c". Is there a page or .pdf where I can get information as to what the usable command/functions are for NXTCam and what each one does?


Fri Feb 15, 2013 12:54 pm
Profile
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3223
Location: Rotterdam, The Netherlands
Post Re: Having some trouble understanding the functions for NXTC
Yes, it is shipped with the driver suite in the html directory, but also available online here: http://botbench.com/driversuite/

= Xander

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]


Fri Feb 15, 2013 1:00 pm
Profile WWW
Rookie

Joined: Sun Jan 27, 2013 8:01 pm
Posts: 18
Post Re: Having some trouble understanding the functions for NXTC
Thank you for the very rapid response. I will take a look at that and get back if I have any more questions. Thanks again!


Fri Feb 15, 2013 1:17 pm
Profile
Rookie

Joined: Sun Jan 27, 2013 8:01 pm
Posts: 18
Post Re: Having some trouble understanding the functions for NXTC
I am getting closer to getting this working, but have ran into a problem with this code
Code:
NXTCAMgetCenter(blob_array & blobs, 0, x, y);

It is giving me these errors:
*Warning*:Invalid '&' operation for types 'blob [8]' and 'short'
**Error**:Procedure call Parameters don't match declaration for 'NXTCAMgetCenter(blob_array & blobs, short index, short & x, short & y)'

I am pretty sure my problem is that I don't know the exact "answer" that the parameters want?


Fri Feb 15, 2013 2:36 pm
Profile
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3223
Location: Rotterdam, The Netherlands
Post Re: Having some trouble understanding the functions for NXTC
Which version of ROBOTC are you using? You should use 3.55B2, which you can download here: viewtopic.php?f=52&t=5122

= Xander

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]


Fri Feb 15, 2013 4:56 pm
Profile WWW
Rookie

Joined: Sun Jan 27, 2013 8:01 pm
Posts: 18
Post Re: Having some trouble understanding the functions for NXTC
I downloaded from that link and installed. It did not get rid of any errors?


Fri Feb 15, 2013 7:12 pm
Profile
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3223
Location: Rotterdam, The Netherlands
Post Re: Having some trouble understanding the functions for NXTC
Your programs compiles just fine for me. I have found that sometimes when you uninstall ROBOTC, it leaves behind some things. So I would advise you to completely uninstall ROBOTC. Use your File Explorer to navigate to C:\Program Files(x86)\Robomatter\ and remove the ROBOTC folder after you've uninstalled it. Then proceed to reinstall the 3.55B2 version.

I can assure you that your program compiles just fine :) I get a single warning, but that's because you're not using a specific function:
Code:
*Warning*:Unreferenced function 'moveymotor'

That's not something you need to worry about :)

Btw, int and short are the same. I'd say pick "int" and stick to it. I've added some comments to you program, they're all prepended with "XS:"

= Xander

Code:
#pragma config(Sensor, S1,     cam,                 sensorI2CCustomFastSkipStates)//setup sensor
//SETUP
#include "drivers/mindsensors-nxtcam.h" //include commands from mindsensors


float x; //setup x as a variable
float y; //setup y as a variable
int _blob1_center;
int xmotor = motorA; //let xmotor be used in place of motorA
int ymotor = motorB; //let ymotor be used in place of motorB


// XS: you don't need this line, it's in the pragma already
// const tSensors cam  = (tSensors) S1; //not quite sure

// XS: Either use const int here, or simply define them
// XS: Also, try not to use single letter variables, why not use
// XS: motorMoveTime, motorDistance and motorSpeed?
// XS: this will make it easier to share your core later and
// XS: and have someone else understand your code quickly.
const int t = 100; //time alloted for motor to move (in thousandths of a second)
const int d = 15; //"color's" distance from middle of head (in cm)
const int s = 50; //motor speed (as a % of fastest possible speed)

float xdegrees; //setup xdegrees as a variable
float ydegrees; //setup ydegrees as a variable

void moveymotor()//move ymotor when conditions exist
{
  if (nMotorEncoder[ymotor] < nMotorEncoderTarget[ymotor])
  {
    ymotor = s;
    wait1Msec(t);
  }
  if (nMotorEncoder[ymotor] > nMotorEncoderTarget[ymotor])
  {
    ymotor = -s;
    wait1Msec(t);
  }
}
void movexmotor() //move xmotor to designated encoder target when conditions exist
{
  if (nMotorEncoder[xmotor] < nMotorEncoderTarget[xmotor])
  {
    xmotor = s;
    wait1Msec(t);
  }
  if (nMotorEncoder[xmotor] > nMotorEncoderTarget[xmotor])
  {
    xmotor = -s;
    wait1Msec(t);
  }
}
void encodertargets()//set the encoder targets of ymotor and xmotor to the same degree as my head is looking
{
  nMotorEncoderTarget[xmotor] = atan(_blob1_center/d);
  nMotorEncoderTarget[ymotor] = atan(_blob1_center/d);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////!!!!Actual Code!!!!////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////



task main()
{

  while (true)
  {
    eraseDisplay(); //clears the display
    blob_array _blobs; //not sure
    bool _condensed = true; //condense all close blobs into one?
    NXTCAMinit(cam); //turns the camera on
    NXTCAMgetBlobs(cam, _blobs, _condensed); //

    nMotorEncoder[xmotor] = 0; //sets the current
    nMotorEncoder[ymotor] = 0;
    x = (_blobs[0].x2 + _blobs[0].x1)/2;
    y = (_blobs[0].y2 + _blobs[0].y1)/2;
    xdegrees =   x - 50; //xdegrees = the middle of the block on the x axis - the middle of the camera's vision
    ydegrees = y - 32; //ydegrees = the middle of the block on the y axis = the middle of the camera's vision
    encodertargets(); //set encoder targets
    movexmotor(); //move motors to encoder targets
    //int movexmotor (int nMotorEncoderTarget = xdegrees)
  }
}

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]


Sat Feb 16, 2013 3:05 am
Profile WWW
Rookie

Joined: Sun Jan 27, 2013 8:01 pm
Posts: 18
Post Re: Having some trouble understanding the functions for NXTC
Xander, you are the man! It finally compiles, which is awesome!!
But now I think I have a slight problem with the actual NXTCam. I have tried the 3 MindsensorsNXTCam sample programs and I cannot get anything to be displayed on the NXT's screen. I have used NXTCamview to adjust which exact color the camera tracks, and on the NXTCamview program under the Tracking tab it will track my "blob" perfectly, but when I try to use the actual NXT and its programs it does not work. Would you happen to know what is wrong?


Sat Feb 16, 2013 12:17 pm
Profile
Rookie

Joined: Sun Jan 27, 2013 8:01 pm
Posts: 18
Post Re: Having some trouble understanding the functions for NXTC
Actually I got it working switched my connect cord from NXT to NXTCam and its working, I think. Must be a bad cord :(

Thanks for all the help Xander!!!! I appreciate it.


Sat Feb 16, 2013 12:20 pm
Profile
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3223
Location: Rotterdam, The Netherlands
Post Re: Having some trouble understanding the functions for NXTC
You're very welcome and I am glad you got it working! Make sure you throw the bad cable away so you can't get boggled by it again :)

= Xander

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]


Sat Feb 16, 2013 1:13 pm
Profile WWW
Rookie

Joined: Sun Jan 27, 2013 8:01 pm
Posts: 18
Post Re: Having some trouble understanding the functions for NXTC
The sample programs are working great! :lol:
But not my own code...
Code:
#pragma config(Sensor, S1,     cam,                 sensorI2CCustomFastSkipStates)         //setup sensor

#include "mindsensors-nxtcam.h"         //include commands from mindsensors

float x;         //setup x as a variable
float y;         //setup y as a variable
int xmotor = motorB;         //let xmotor be used in place of motorA
int ymotor = motorA;         //let ymotor be used in place of motorB
int xtarget;         //define xtarget as variable
int ytarget;         //define ytarget as variable
int xdegrees;         //define xdegrees as variable
int ydegrees;         //define ydegrees as variable
const int motortime = 100;         //time alloted for motor to move (in thousandths of a second)
const int blobdistance = 15;      //"blob's" distance from middle of head (in cm)
const int motorspeed = 10;         //motor speed (as a % of fastest possible speed)
float xradians;         //setup xradians as a variable
float yradians;         //setup yradians as a variable

void moveymotor()         //move ymotor when conditions exist
      {
      if (ytarget > 0)
           {
           nMotorEncoderTarget[ymotor] = ytarget;
          motor[ymotor] = motorspeed;
          wait1Msec(motortime);
          //while (nMotorEncoder[ymotor] < ytarget) {}
           }
     if (ytarget < 0)
           {
           nMotorEncoderTarget[ymotor] = ytarget;
          motor[ymotor] = -motorspeed;
          wait1Msec(motortime);
          //while (nMotorEncoder[ymotor] > ytarget) {}
           }
      }
void movexmotor()         //move xmotor to designated encoder target when conditions exist
      {
     if (xtarget > 0)
           {
           nMotorEncoderTarget[xmotor] = xtarget;
          motor[xmotor] = -motorspeed;
          wait1Msec(motortime);
          //while (nMotorEncoder[xmotor] < xtarget) {}
           }
     if (xtarget < 0)
           {
           nMotorEncoderTarget[xmotor] = xtarget;
          xmotor = motorspeed;
          wait1Msec(motortime);
          //while (nMotorEncoder[xmotor] > xtarget) {}
           }
      }
int encodertargetx()         //set the encoder targets of ymotor and xmotor to the same degree as my head is looking
      {
      xradians = atan(xdegrees/blobdistance);
      xtarget = radiansToDegrees(xradians);
      return (xtarget);
      }
   
int encodertargety()         //set the encoder targets of ymotor and xmotor to the same degree as my head is looking
      {
      yradians = atan(xdegrees/blobdistance);
      ytarget = radiansToDegrees(yradians);
      return (ytarget);
      }
      
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////!!!!Actual Code!!!!////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

task main()
{

  while (true)
  {
    blob_array _blobs;            //sets up an array?
    bool _condensed = true;      //condense all close blobs into one
    NXTCAMinit(cam);               //turns the camera on
    NXTCAMgetBlobs(cam, _blobs, _condensed);         //gets the blobs

    nMotorEncoder[xmotor] = 0;         //sets the current value of xmotor to 0 and sets that value =x
    nMotorEncoder[ymotor] = 0;         //sets the current value of ymotor to 0 and sets that value =y
   
    x = (_blobs[0].x2 + _blobs[0].x1)/2;         //finds the middle point of the blob on the x axis
    y = (_blobs[0].y2 + _blobs[0].y1)/2;         //finds the middle point of the blob on the y axis
   
    xdegrees =   x - 77;         //xdegrees = the middle of the block on the x axis - the middle of the camera's vision
    ydegrees = y - 44;         //ydegrees = the middle of the block on the y axis - the middle of the camera's vision
   
    encodertargetx();         //set x encoder target
    encodertargety();         //set y encoder target
    movexmotor();            //move xmotor to the encoder target
    moveymotor();                 //move ymotor to the encoder target
  }
}

The logic in my head says that it should do what I want, but its not quite working.
When it gets to the actual code to move the motors - "movexmotor" and "moveymotor" - the motors just keep spinning in the same direction and do not stop. If I am reading my code right, the motors should turn to the "nMotorEncodertarget" that I set with my "encodertarget" int functions, correct? I do not understand why they continue rotating.
Any help is appreciated, of course.


Sat Feb 16, 2013 9:43 pm
Profile
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3223
Location: Rotterdam, The Netherlands
Post Re: Having some trouble understanding the functions for NXTC
Ok, explain to me, in as much detail as possible, without the use of code, what you intend your robot to do, based on what kind of input.

Sometimes it's good to stop and really think about what you want to achieve before you start writing code :)

Also, before pasting your code into the forums, run it through the code formatter, it will make it a lot more readable!

Attachment:
2013-02-17_06-14-26.png
2013-02-17_06-14-26.png [ 30.35 KiB | Viewed 3702 times ]


= Xander

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]


Sun Feb 17, 2013 1:12 am
Profile WWW
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3223
Location: Rotterdam, The Netherlands
Post Re: Having some trouble understanding the functions for NXTC
I cleaned up your code a bit, it may not do everything you want yet, especially the motors. You need to really think about that :)

Code:
#pragma config(Sensor, S1,     NXTCAM,         sensorI2CCustomFastSkipStates)
#pragma config(Motor,  motorA,          YMOTOR,        tmotorNXT, PIDControl, encoder)
#pragma config(Motor,  motorB,          XMOTOR,        tmotorNXT, PIDControl, encoder)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

// XS: this automagically opens the debugStream window.
#pragma debuggerWindows(debugStream)

#include "drivers/mindsensors-nxtcam.h"         //include commands from mindsensors

const float blobdistance = 15.0;   //"blob's" distance from middle of head (in cm)
const int motorspeed = 10;         //motor speed (as a % of fastest possible speed)

//move the motor when conditions exist
// XS: if you find the motor moves in the wrong direction, use the motor setup
// XS: to configure it as "reversed"
void moveMotor(tMotor mot, int target)
{
  writeDebugStreamLine("moveMotor called: motor: %d, target: %d", mot, target);
  if (target > 0)
  {
    nMotorEncoderTarget[mot] = target;
    motor[mot] = motorspeed;
  }
  else if (target < 0)
  {
    nMotorEncoderTarget[mot] = target;
    motor[mot] = -motorspeed;
  }
  else
  {
    motor[mot] = 0;
  }

  // Wait for the motor to be done running
  while (nMotorRunState[mot] != runStateIdle || nMotorRunState[mot] != runStateHoldPosition)
  {
    wait1Msec(5);
  }
}

//set the encoder targets of YMOTOR and XMOTOR to the same degree as my head is looking
int encodertarget(int degrees)
{
  int retval = 0;
  writeDebugStreamLine("encodertarget called: %d", degrees);
  float radians = atan((float)degrees / blobdistance);
  retval = radiansToDegrees(radians);
  writeDebugStreamLine("encodertarget returns: %d", retval);
  return retval;
}

/////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////!!!!Actual Code!!!!///////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////

task main()
{
  int xtarget;
  int ytarget;
  int xdegrees;
  int ydegrees;
  int x;
  int y;

  // XS: You only need to do this once.
  // XS: encoder targets are always relative to whatever they were before.
  nMotorEncoder[XMOTOR] = 0;         //sets the current value of XMOTOR to 0 and sets that value =x
  nMotorEncoder[YMOTOR] = 0;         //sets the current value of YMOTOR to 0 and sets that value =y

  NXTCAMinit(NXTCAM);            //turns the camera on
  blob_array _blobs;             //sets up an array?
  bool _condensed = true;        //condense all close blobs into one

  while (true)
  {
    NXTCAMgetBlobs(NXTCAM, _blobs, _condensed);         //gets the blobs

    x = (_blobs[0].x2 + _blobs[0].x1)/2;         //finds the middle point of the blob on the x axis
    y = (_blobs[0].y2 + _blobs[0].y1)/2;         //finds the middle point of the blob on the y axis

    writeDebugStreamLine("NXTCAM returned: x: %d, y: %d", x, y);

    xdegrees =   x - 77;         //xdegrees = the middle of the block on the x axis - the middle of the camera's vision
    ydegrees = y - 44;         //ydegrees = the middle of the block on the y axis - the middle of the camera's vision

    xtarget = encodertarget(xdegrees);         //set x encoder target
    ytarget = encodertarget(ydegrees);         //set y encoder target
    moveMotor(XMOTOR, xtarget);                //move XMOTOR to the encoder target
    moveMotor(YMOTOR, ytarget);                //move YMOTOR to the encoder target
  }
}

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]


Sun Feb 17, 2013 1:47 am
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 20 posts ]  Go to page 1, 2  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.