View unanswered posts | View active topics It is currently Wed Sep 03, 2014 3:06 am






Reply to topic  [ 9 posts ] 
Writing motor position to file 
Author Message
Rookie

Joined: Wed Dec 11, 2013 12:31 pm
Posts: 3
Post Writing motor position to file
Hi
I am using RobotC to program NXT 2.0 to cycle a product to test its durability. the program cycle back and fourth 150 degree.
I have managed to write the motor position using one of the ROBOTC example, but that does not give me what I want.
This is what the program write to file

Motor A,Motor BCycle number,3D Act0, 35, 145, -144, -1, 36, 144, -145, -2, 35, 145, -145, -3, 35, 145, -145, -4, 35, 145, -145, -5, 36, 145, -144, -6, 35, 145, -145, -7, 35, 145, -145, -8, 36, 145, -145, -9, 35, 144, -145, -.

Ideally it should be
Motor A Motor B
Cycle number, 3D Actuation, 0D Actuation, 3D Actuation, 0D Actuation
1, 35, 35, 145, 145
2, 35, 35, 145, 145
3, 35, 35, 145, 145
this is what i had in my mind to achieve, so I can upload my file to computer and log the data.

here is code

what am doing wrong?

Thanks for the help in Advance.


task main()
{

// File Handling, logging encoder value to file -----------------------------

TFileHandle hFileHandle;
TFileIOResult nIOResult;
string sFileName = "Encoder.txt";
long nFileSize = 10000;

byte CR = 0x13; // define CR (carriage return)
byte LF = 0x10;

string MotorHeader = "Motor A,Motor B";
string header = "Cycle number,3D Actuation,0D Actuation,3D Actuation,0D Actuation";

Delete("Encoder.txt", nIOResult);
OpenWrite(hFileHandle, nIOResult, sFileName, nFileSize);
WriteText(hFileHandle, nIOResult, MotorHeader);
WriteByte(hFileHandle, nIOResult, CR);
WriteByte(hFileHandle, nIOResult, LF);
WriteText(hFileHandle, nIOResult, header);
WriteByte(hFileHandle, nIOResult, CR);
WriteByte(hFileHandle, nIOResult, LF);
//Close(hFileHandle, nIOResult);


// -------------------------------------------------

string list;


// for tracking cycle number and updating screen.
// ---------------------------------------------------------------------------------------------------------------------------
long initial, max;
initial = 0;
max = 10; // change this number to set the maximum number of cycle
long i = 0;

//------------------------------------------------------------------- these variable is used for updating screen
string s1 = "Set Thumbwheel"; //
string s2 = "Max anti-clockwise"; //
string s3 = "Press Enter"; //
string s4 = "to continue"; //
string s5 = "Cycle #:"; //
string s6 = "/"; //
string s7 = "Cycle completed"; //
string s8 = "Enter to continue"; //
string s9 = "Start:"; //
string s10 = "End :"; //
int h,j,k,l; //

// -------------------------------------------------------------------------------------------------------------------------------


//-------------------------------------------------------------------
eraseDisplay(); // Clear screan
//-----------------------------------------------------------
while (nNxtButtonPressed !=3) // display instruction on screen until the user press the orange button
{
nxtDisplayCenteredTextLine(1, "%s", s1);
nxtDisplayCenteredTextLine(3, "%s", s2);
nxtDisplayCenteredTextLine(5, "%s", s3);
nxtDisplayCenteredTextLine(7, "%s", s4);
}
//----------------------------------------------------------------
if (nNxtButtonPressed == 3) // initialise motor after the user pressed the orage button on nxt
{
eraseDisplay();
motor[motorA] = 0; // set motor to zero power
motor[motorB] = 0; // set power of motor B to zero
nMotorEncoder[motorA] =0; // set encoder motor A to zero
nMotorEncoder[motorB] = 0; // set encode motor B to zero

//---------------------------------------------------------------------
for (i = initial; i<max; i++) // looping for number of cycle
{
while (nMotorEncoder[motorA] <145) //uper limit to 145 degree. the motor over shoot by 20 degree the actual limit is 165
{
motor[motorA] = 40;
j = nMotorEncoder[motorA];
}
motor[motorA] = 0;
wait1Msec(100);
while (nMotorEncoder[motorA] >35) // lower limit is 35 degree, with the overshoot of 20 degree the actual limit is 15 degree
{
if (i == (max-1))
motor[motorA] =-25; // on the final cycle the motor come slower than normal
else
motor[motorA] = -40;
h = nMotorEncoder[motorA];
}
motor[motorA] = 0;

while (nMotorEncoder[motorB] >-145) // uper limit to -145 degree. the motor over shoot by 20 degree the actual limit is -165
{
motor[motorB] = -40;
k = nMotorEncoder[motorB];
}
motor[motorB] = 0;
while (nMotorEncoder[motorB] <-35) // lower limit is -35 degree, with the overshoot of the motor the actual limit is -15 degree
{
if (i == (max-1))
motor[motorB] =25; // on the final cycle the motor come slower than normal
else
motor[motorB] = 40;
l = nMotorEncoder[motorB];
}

motor[motorB] = 0;
// ----------------------------------------------------
StringFormat(list, "%d, %d, %d, %d, %d", i, h, j,k,l);
WriteText(hFileHandle, nIOResult, list);
WriteByte(hFileHandle, nIOResult, CR);
WriteByte(hFileHandle, nIOResult, LF);
//-----------------------------------------------------

nxtDisplayCenteredTextLine(7, "%d%s%d", i, s6,max); //update screen
nxtDisplayCenteredTextLine(5, " %s", s5);
nxtDisplayCenteredTextLine(0, "%s %s", s9, s10);
nxtDisplayCenteredTextLine(2, "A: %d %d", j, h);
nxtDisplayCenteredTextLine(3, "B:%d %d", k, l);}
}

//-------------------------close file
Close(hFileHandle, nIOResult);
//-------------------------------
//------------------------------------------------------------------------------------------------
motor[motorA]=0;
motor[motorB] =0; // once the program cycled to maxinum number of cycle, program turn off motor A and B and clear screen

eraseDisplay();
//------------------------------------------------------------------------------------------------
while (nNxtButtonPressed !=3) //Program complete message will remain on the creen permanently until the user pressed orange button.
{
nxtDisplayCenteredTextLine(3, "%s", s7);
nxtDisplayCenteredTextLine(5, "%s", s8);
}
//-------------------------------------------------------------------------------------------------
}


Wed Dec 11, 2013 12:47 pm
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1347
Post Re: Writing motor position to file
It looks like your code is based on the sample code in the RobotC documentation. Unfortunately, it looks like the sample code was wrong. The LF character is decimal 10 not 0x10. Also, the CR character is decimal 13 not 0x13. Try replacing these lines and see if that works for you.
Code:
byte CR = 13;   // define CR (carriage return)
byte LF = 10;   // define LF (line feed)


Wed Dec 11, 2013 4:03 pm
Profile
Site Admin
Site Admin
User avatar

Joined: Tue Oct 09, 2012 10:34 am
Posts: 192
Post Re: Writing motor position to file
Hi hanman,

hanman wrote:
Code:
byte CR = 0x13;   // define CR (carriage return)
byte LF = 0x10;

The ASCII code for CR (carriage return) is either decimal 13 or hexadecimal 0xD. Similarly, the ASCII code for LF (line feed) is either decimal 10 or hexadecimal 0xA.

Also, keep in mind that you can use character literals for these expressions as well:

Code:
byte CR = '\r';   // define CR (carriage return)
byte LF = '\n';

You can also use string literals to simplify your code. Instead of having to write out the CR and LF bytes individually, you can just do:

Code:
WriteText(hFileHandle, nIOResult, "\r\n");

Cheers,
--Ryan

_________________
Ryan Cahoon
CMU Robotics Academy
RVW Software Developer

Robot Potato Head; Virtual NXT


Wed Dec 11, 2013 4:09 pm
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1347
Post Re: Writing motor position to file
rcahoon wrote:
Hi hanman,

hanman wrote:
Code:
byte CR = 0x13;   // define CR (carriage return)
byte LF = 0x10;

The ASCII code for CR (carriage return) is either decimal 13 or hexadecimal 0xD. Similarly, the ASCII code for LF (line feed) is either decimal 10 or hexadecimal 0xA.

Ryan,
Hanman's code seems to be straight out of the sample code in the RobotC documentation. You may want to fix that documentation.
Thanks.
Code:
/*****************************\
|* Example File Input/Output *|
|* Bence Feher 22/Dec/2011   *|
\*****************************/
 
task main()
{
  TFileHandle   hFileHandle;              // will keep track of our file
  TFileIOResult nIOResult;                // will store our IO results
  string        sFileName = "test.txt";   // the name of our file
  int           nFileSize = 100;          // will store our file size
 
  byte CR = 0x13;   // define CR (carriage return)
  byte LF = 0x10;   // define LF (line feed)
 
  string        sMessageToWrite = "ROBOTC IO test!";    // we will write this to the file
  string        sMesageToWrite_2 = "A new line!";       // we will also write this to the file on the next line
  char          incommingChar;                          // this will store each char as we read back in from the file
  string        incommingString[5];                     // this will store the final, fully-read strings (with new strings getting put into new indexes
  int           nLineCounter = 0;                       // this will let us know which line we are on when reading and writing (used as the index to 'incommingString[]')
 
  Delete("test.txt",nIOResult);
  OpenWrite(hFileHandle, nIOResult, sFileName, nFileSize);    // open the file for writing (creates the file if it does not exist)
  WriteText(hFileHandle, nIOResult, sMessageToWrite);         // write 'sMessageToWrite' to the file
  WriteByte(hFileHandle, nIOResult, CR);                      // write 'CR' to the file (carriage return)
  WriteByte(hFileHandle, nIOResult, LF);                      // write 'LF' to the file (line feed)
  WriteText(hFileHandle, nIOResult, sMesageToWrite_2);        // write 'sMesageToWrite_2' to the file
  Close(hFileHandle, nIOResult);                              // close the file (DON'T FORGET THIS STEP!)
 
  wait1Msec(1000);  // just a wait so we can see the variables updating in the ROBOTC debugger in order (this is not necessary)
 
  OpenRead(hFileHandle, nIOResult, sFileName, nFileSize);     // open our file 'sFileName' for reading
 
  for(int i = 0; i < nFileSize; i++)                          // iterate through the file until we've hit the end:
  {
    ReadByte(hFileHandle, nIOResult, incommingChar);            // read in a single byte
 
    if(incommingChar == CR || incommingChar == LF)              // if the incomming byte is a carriage return or a line feed:
    {
      if(incommingChar == LF)                                     // if it's a line feed:
        nLineCounter++;                                             // increment our index (will now store next char on a 'new line' (a new index into our 'incommingString[]')
    }
    else
    {
      incommingString[nLineCounter] += incommingChar;             // append that byte (char) to the end of our final string, at the right index
    }
  }
  Close(hFileHandle, nIOResult);                              // close our file (DON'T FORGET THIS STEP!)
 
  for(int i = 1; i <= 5; i++)
  {
    nxtDisplayCenteredTextLine(i, incommingString[i-1]);
  }
 
 
  while(true);  // infinite loop to keep our program running so that we can view the ROBOTC debugger variables
}


Wed Dec 11, 2013 5:14 pm
Profile
Site Admin
Site Admin
User avatar

Joined: Tue Oct 09, 2012 10:34 am
Posts: 192
Post Re: Writing motor position to file
MHTS wrote:
Hanman's code seems to be straight out of the sample code in the RobotC documentation. You may want to fix that documentation.


I've submitted a bug report about it. Thanks for pointing it out

_________________
Ryan Cahoon
CMU Robotics Academy
RVW Software Developer

Robot Potato Head; Virtual NXT


Wed Dec 11, 2013 5:24 pm
Profile
Site Admin
Site Admin

Joined: Wed Jan 24, 2007 10:42 am
Posts: 602
Post Re: Writing motor position to file
Tis merely a typo. We'll update it on the Wiki now and in the help docs on the next release.

Thanks.

_________________
Timothy Friez
ROBOTC Developer - SW Engineer
tfriez@robotc.net


Wed Dec 11, 2013 5:30 pm
Profile
Rookie

Joined: Wed Dec 11, 2013 12:31 pm
Posts: 3
Post Re: Writing motor position to file
Thanks Ryan for your explanation.
The code is doing what i expected.
Correcting the documentation would helps.

thanks
hanman


Thu Dec 12, 2013 5:41 am
Profile
Rookie

Joined: Wed Dec 11, 2013 12:31 pm
Posts: 3
Post Re: Writing motor position to file
Sorry to bother again. Is there a limitation on string size(lenght)??

The code below output the result below to file.

string MotorHeader = " Motor A, Motor B";
string header = "Cycle#,3D Actuation,0D Actuation,3D Actuation,0D Actuation";

WriteText(hFileHandle, nIOResult, MotorHeader);
WriteText(hFileHandle, nIOResult, "\r\n");
WriteText(hFileHandle, nIOResult, header);
WriteText(hFileHandle, nIOResult, "\r\n");


StringFormat(list, "%d, %d, %d, %d, %d", i, h, j,k,l);
WriteText(hFileHandle, nIOResult, list);
WriteText(hFileHandle, nIOResult, "\r\n");


i is long,
h,j k, l is integer number.

Motor A,
Cycle#,3D Actuation
0, 144, 35, -144, -
1, 144, 35, -145, -
2, 145, 35, -145, -
3, 145, 35, -145, -
4, 144, 35, -144, -
5, 145, 35, -145, -
6, 144, 36, -145, -
7, 145, 35, -145, -
8, 145, 36, -145, -
9, 145, 35, -145, -


Thu Dec 12, 2013 6:05 am
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1347
Post Re: Writing motor position to file
hanman wrote:
Sorry to bother again. Is there a limitation on string size(lenght)??

Yes, the string type has a limit of about 19 characters. Now that RobotC supports pointers, you should use char [] array (and char *) instead of string. RobotC folks should really obsolete the string type. It is really confusing a lot of users and I can't think of any reason keeping it except for compatibility. But I would think very few RobotC users would care about this minor compatibility as long as the compiler clearly warns the user about the use of the string type and may even map that to char[20] for compatibility.


Mon Dec 23, 2013 5:28 am
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 9 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.