NXC Questions
Re: NXC Questions
Commit to LEGO Mindstorms Robotics Stack Exchange:
bit.ly/MindstormsSE
Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
Re: NXC Questions
Thanks again.
One more Question:
Look at this code:
It is successfully downloaded to the brick but i didn't get what i want from the program, and checks for the file (As i wanted).
What i want is to increase the value of "VisitsNO.txt" by one each time the user starts the program, but what i get is the same number (one) each time I start the program.
What to do?
One more Question:
Look at this code:
Code: Select all
byte handle;
int fsize;
task main () {
if(OpenFileRead("VisitsNO.txt", fsize, handle) == LDR_SUCCESS)
{
OpenFileAppend("VisitsNO.txt", fsize, handle);
}
else if(OpenFileRead("VisitsNO.txt", fsize, handle) == LDR_FILENOTFOUND)
{
CreateFile("VisitsNO.txt", 1024, handle);
}
OpenFileRead("VisitsNO.txt", fsize, handle);
NumOut(8,32,"VisitsNO.txt");
Wait(5000);
CloseFile(handle);
}
What i want is to increase the value of "VisitsNO.txt" by one each time the user starts the program, but what i get is the same number (one) each time I start the program.
What to do?
B2O productions are the best for the best.
Re: NXC Questions
I think the file size variable has to be unsigned.
A sophistical rhetorician, inebriated with the exuberance of his own verbosity, and gifted with an egotistical imagination that can at all times command an interminable and inconsistent series of arguments to malign an opponent and to glorify himself.
-
- Posts: 1818
- Joined: 02 Oct 2010, 02:19
- Location: Michigan USA
- Contact:
Re: NXC Questions
Well even if so, that is not the only issue.
You can't use NumOut to display text. Also, you need to read the file in order to get data from it. Attempting to display "VisitsNO.txt" will actually display that, not part of the file. IIRC, any time you pass a string into NumOut, it displays "1" (it is not meant for that data type).
I tried you code a little, and tried several things to get it to even just properly display the contents of a file, but had no success.
You can't use NumOut to display text. Also, you need to read the file in order to get data from it. Attempting to display "VisitsNO.txt" will actually display that, not part of the file. IIRC, any time you pass a string into NumOut, it displays "1" (it is not meant for that data type).
I tried you code a little, and tried several things to get it to even just properly display the contents of a file, but had no success.
Matt
http://mattallen37.wordpress.com/
I'm all for gun control... that's why I use both hands when shooting
http://mattallen37.wordpress.com/
I'm all for gun control... that's why I use both hands when shooting
Re: NXC Questions
The part which shows the visitsNO.txt is an additional part which i wont use in my program, i just made it to see if it works (adds one point) or not.
B2O productions are the best for the best.
Re: NXC Questions
I couldn't find out a way to solve the problem:
"To check for a file existence (Done), if the file exists, add "1" to it's value".
Anthor question is: How does "OpenFileaAppend"? I know its OpenFileAppend(fname, fsize, handle); but how to write a value in it!?
Please, if u read this, think about it. Thanks.
"To check for a file existence (Done), if the file exists, add "1" to it's value".
Anthor question is: How does "OpenFileaAppend"? I know its OpenFileAppend(fname, fsize, handle); but how to write a value in it!?
Please, if u read this, think about it. Thanks.
B2O productions are the best for the best.
Re: NXC Questions
I use this code, which I got from an example on the web (techbricks.nl (write_to_csv_file)) :-
file_size = 1024; // Set file size
file_number = 1; // Number files
sfile_number = NumToStr(file_number); // Convert number to string
file_part = 1; // Set up number of part of file
sfile_part = NumToStr(file_part); // Convert part number to string
file_name = StrCat(base_file_name,"-",sfile_number,"-",sfile_part,".csv");
file_result = CreateFile(file_name, file_size, file_handle); // File exists?
while (file_result== LDR_FILEEXISTS) // If the file exists rename
{
CloseFile(file_handle);
file_number = file_number + 1; // Increment file number
sfile_number = NumToStr(file_number); // Convert new number to string
file_name = StrCat(base_file_name,"-",sfile_number,"-",sfile_part,".csv");
file_result=CreateFile(file_name, file_size, file_handle);
}
file_size = 1024; // Set file size
file_number = 1; // Number files
sfile_number = NumToStr(file_number); // Convert number to string
file_part = 1; // Set up number of part of file
sfile_part = NumToStr(file_part); // Convert part number to string
file_name = StrCat(base_file_name,"-",sfile_number,"-",sfile_part,".csv");
file_result = CreateFile(file_name, file_size, file_handle); // File exists?
while (file_result== LDR_FILEEXISTS) // If the file exists rename
{
CloseFile(file_handle);
file_number = file_number + 1; // Increment file number
sfile_number = NumToStr(file_number); // Convert new number to string
file_name = StrCat(base_file_name,"-",sfile_number,"-",sfile_part,".csv");
file_result=CreateFile(file_name, file_size, file_handle);
}
A sophistical rhetorician, inebriated with the exuberance of his own verbosity, and gifted with an egotistical imagination that can at all times command an interminable and inconsistent series of arguments to malign an opponent and to glorify himself.
Re: NXC Questions
(This post is probably not entirely unlike a useful one.)
Haha! You wanna write to CSV files? Learn, grasshopper.
This is the code from my Power Vs Rotation experiment:
saveResults() is what you want.
Haha! You wanna write to CSV files? Learn, grasshopper.
This is the code from my Power Vs Rotation experiment:
Code: Select all
/* Program Name: "PowerVsRotation.nxc"
** This program was created by "muntoo" on December 12, 2010
** Created using BricxCC (by John Hansen) and written in NXC (by John Hansen)
** ( http://bricxcc.sourceforge.net/ )
Description:
Experiment to see what the correlation is between power and speed.
Results:
<Power Range>: <Equation>
5: Speed = Power*6
10-15: Speed = Power*7
20-70: Speed = Power*(7.5-(Power-20)/100)
75-100: Speed = Power*6.5
Basically, Speed=7*Power
*/
#include "GetButtonDirection.nxc"
struct ROTATEMOTOR
{
byte outputs;
char pwr;
long angle;
};
struct RESULTS
{
byte outputs;
char pwr;
long angle;
long actual_angle_B;
long actual_angle_C;
unsigned long time;
};
#define SIZEOF_RESULTS 18
string OutputToStr(byte outputs)
{
switch(outputs)
{
case OUT_A:
return("OUT_A");
break;
case OUT_B:
return("OUT_B");
break;
case OUT_C:
return("OUT_C");
break;
case OUT_AB:
return("OUT_AB");
break;
case OUT_AC:
return("OUT_AC");
break;
case OUT_BC:
return("OUT_BC");
break;
case OUT_ABC:
return("OUT_ABC");
break;
default:
return("");
break;
}
return("");
}
void ResultsOut(RESULTS result, unsigned long DrawOptions = DRAW_OPT_NORMAL)
{
if(DrawOptions & DRAW_OPT_CLEAR_WHOLE_SCREEN)
{
ClearScreen();
DrawOptions = DrawOptions & (~DRAW_OPT_CLEAR_WHOLE_SCREEN);
}
TextOut(0, LCD_LINE1, OutputToStr(result.outputs), 0);
NumOut(0, LCD_LINE2, result.pwr, 0);
NumOut(0, LCD_LINE3, result.angle, 0);
NumOut(0, LCD_LINE4, result.actual_angle_B, 0);
NumOut(0, LCD_LINE5, result.actual_angle_C, 0);
NumOut(0, LCD_LINE6, result.time, 0);
}
string GetExtension(string filename)
{
unsigned int strlen_filename = strlen(filename);
for(unsigned int stridx = strlen_filename - 1; stridx > 0; stridx--)
{
if(filename[stridx] == '.')
return(SubStr(filename, stridx, strlen_filename - stridx));
}
return("");
}
void SaveResults(string filename, RESULTS results[])
{
RESULTS rTemp;
unsigned int arrlen_results = ArrayLen(results);
unsigned int fsize = arrlen_results * SIZEOF_RESULTS;
// unsigned int fsize = ITERATIONS * SIZEOF_RESULTS;
byte handle;
// NumOut(0, 0, fsize, 0);
if(strcmp(GetExtension(filename), ".csv") == 0)
{
string szBuf = "Iteration,Outputs,Power,Angle,\"Actual Angle B\",\"Actual Angle C\",\"Time (ms)\"\r\n";
for(unsigned int arridx = 0; arridx < arrlen_results; arridx++)
{
rTemp = results[arridx];
szBuf += NumToStr(arridx);
szBuf += ",";
szBuf += OutputToStr(rTemp.outputs);
szBuf += ",";
szBuf += NumToStr(rTemp.pwr);
szBuf += ",";
szBuf += NumToStr(rTemp.angle);
szBuf += ",";
szBuf += NumToStr(rTemp.actual_angle_B);
szBuf += ",";
szBuf += NumToStr(rTemp.actual_angle_C);
szBuf += ",";
szBuf += NumToStr(rTemp.time);
szBuf += "\r\n";
}
fsize = strlen(szBuf) + 1;
DeleteFile(filename);
CreateFile(filename, fsize, handle);
WriteString(handle, szBuf, fsize);
CloseFile(handle);
}
else
{
DeleteFile(filename);
CreateFile(filename, fsize, handle);
for(unsigned int arridx = 0; arridx < arrlen_results; arridx++)
{
rTemp = results[arridx];
Write(handle, rTemp);
}
CloseFile(handle);
}
}
void ReadResults(string filename, RESULTS &results[])
{
RESULTS rTemp;
unsigned int arrlen_results = ArrayLen(results);
unsigned int fsize = arrlen_results * SIZEOF_RESULTS;
// unsigned int fsize = ITERATIONS * SIZEOF_RESULTS;
byte handle;
// NumOut(0, 0, fsize, 0);
OpenFileRead(filename, fsize, handle);
// arrlen_results = fsize / SIZEOF_RESULTS;
// ArrayInit(results, rTemp, arrlen_results);
for(unsigned int arridx = 0; arridx < arrlen_results; arridx++)
{
Read(handle, rTemp);
results[arridx] = rTemp;
}
CloseFile(handle);
}
#define ITERATIONS 20
#define POWER_START 5
#define POWER_INCREMENT 5
#define ANGLE 720
task main()
{
// Stores parameters for RotateMotor()
ROTATEMOTOR rmTemp;
rmTemp.outputs = OUT_BC;
rmTemp.pwr = POWER_START;
rmTemp.angle = ANGLE;
ROTATEMOTOR rmOut[];
ArrayInit(rmOut, rmTemp, ITERATIONS);
for(unsigned int arridx = 0; arridx < ITERATIONS; arridx++)
{
rmOut[arridx].pwr = POWER_INCREMENT * arridx + POWER_START;
}
// Initialize struct to store results
RESULTS rTemp;
rTemp.outputs = OUT_BC;
rTemp.pwr = POWER_START;
rTemp.angle = ANGLE;
rTemp.actual_angle_B = ANGLE;
rTemp.actual_angle_C = ANGLE;
rTemp.time = 0;
RESULTS results[];
ArrayInit(results, rTemp, ITERATIONS);
unsigned long timer;
unsigned long prev_count_B;
unsigned long prev_count_C;
unsigned long delta_count_B;
unsigned long delta_count_C;
byte ButtonDirection = 0;
ClearScreen();
TextOut(0, LCD_LINE1, "Left=Convert2csv", 0);
TextOut(0, LCD_LINE2, "Enter = Save", 0);
TextOut(0, LCD_LINE3, "Right = Read", 0);
// Wait until button is pressed
do
{
ButtonDirection = GetButtonDirection();
} while(!(ButtonDirection));
for(byte bdTemp = ButtonDirection; bdTemp; bdTemp = GetButtonDirection());
// Log data and save to .dat file
if(ButtonDirectionPressed(ButtonDirection, BTNCENTER))
{
for(unsigned int arridx = 0; arridx < ITERATIONS; arridx++)
{
// Store original rotation count
prev_count_B = MotorRotationCount(OUT_B);
prev_count_C = MotorRotationCount(OUT_C);
// Move rmOut[arridx] to rmTemp for easier access
rmTemp = rmOut[arridx];
// Rotate motor and measure time
timer = CurrentTick();
RotateMotor(rmTemp.outputs, rmTemp.pwr, rmTemp.angle);
timer = CurrentTick() - timer;
// Calculate delta between new and old rotation counts
delta_count_B = MotorRotationCount(OUT_B) - prev_count_B;
delta_count_C = MotorRotationCount(OUT_C) - prev_count_C;
// Store results
rTemp.outputs = rmTemp.outputs;
rTemp.pwr = rmTemp.pwr;
rTemp.angle = rmTemp.angle;
rTemp.actual_angle_B = delta_count_B;
rTemp.actual_angle_C = delta_count_C;
rTemp.time = timer;
results[arridx] = rTemp;
// Display results
ClearScreen();
ResultsOut(rTemp, 0);
// Save data to file
SaveResults("results.dat", results);
// Let user see results
while(!(ButtonPressed(BTNCENTER, 0)));
while(ButtonPressed(BTNCENTER, 0));
}
}
// Convert .dat file to .csv
else if(ButtonDirectionPressed(ButtonDirection, BTNLEFT))
{
ReadResults("results.dat", results);
SaveResults("results.csv", results);
}
// Read .dat file results
else if(ButtonDirectionPressed(ButtonDirection, BTNRIGHT))
{
ReadResults("results.dat", results);
for(unsigned int arridx = 0; arridx < ITERATIONS; )
{
// Display results
ClearScreen();
ResultsOut(results[arridx], 0);
// Let user see results
while(1)
{
// Stop reading
if(ButtonPressed(BTNCENTER, 0))
{
arridx = ITERATIONS;
while(ButtonPressed(BTNCENTER, 0));
break;
}
// Go to previous result
if(ButtonPressed(BTNLEFT, 0))
{
if(arridx > 0)
arridx--;
else
arridx = ITERATIONS - 1;
while(ButtonPressed(BTNLEFT, 0));
break;
}
// Go to next result
if(ButtonPressed(BTNRIGHT, 0))
{
arridx++;
if(arridx >= ITERATIONS)
arridx = 0;
while(ButtonPressed(BTNRIGHT, 0));
break;
}
}
}
}
}
Commit to LEGO Mindstorms Robotics Stack Exchange:
bit.ly/MindstormsSE
Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
Re: NXC Questions
Why don't i use the function Write(); instead? If i can use it, can i write Write(Fname, +1);?
B2O productions are the best for the best.
Re: NXC Questions
I reached what i need by doing the following:
It will give me what I need (To add one point each time it starts) but it will create an empty file, thats why i continued the program by:
Code: Select all
if(OpenFileRead("Validation.txt", fsize, handle) == LDR_SUCCESS)
{
handle += 1;
}
Code: Select all
else if(OpenFileRead("Validation.txt", fsize, handle) == LDR_FILENOTFOUND)
{
CreateFile("Validation.txt", 0, handle); // look here, file size is 0 because it will stay empty.
}
B2O productions are the best for the best.
Who is online
Users browsing this forum: No registered users and 4 guests