Re: NXC Questions
Posted: 26 Feb 2011, 05:30
Give a child a robot, he'll play for an hour. Teach him to build, and he'll play forever.
https://mindboards.org:443/
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);
}
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;
}
}
}
}
}
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.
}