View unanswered posts | View active topics It is currently Mon Sep 21, 2020 4:18 pm

Reply to topic  [ 6 posts ] 
data to file. 
Author Message

Joined: Thu Sep 24, 2009 4:24 pm
Posts: 18
Post data to file.
Alright, I'm stumped. I've made a proportional controller for my robot. I have an ultrasonic sensor calculating how far away I am from the wall while my robot drives down a hallway. I want to output the information to a spreadsheet to do some calculations with, but as far as i've been able to get is having all my data points separated by commas and they only insert into my spreadsheet ( calc) horizontally, and there are too many data points for it to handle horizontally. I could likely go forever vertically though. The thing I really want is to know how to input a newline character instead of a comma so that I can have it go vertically. I type in:

WriteString(hFileHandle, nIoResult, fileDataTwo);

and my variable is stated:

string fileDataTwo = ",";

I tried saying: string fileDataTwo = "/n"; and I can't remember how to declare that it's a special character signifying a new line to save my life!

Any help HUGELY appreciated. I know it's probably really simple, but my brain hurts and I need help. Thanks.

Thu Feb 11, 2010 7:51 pm
User avatar

Joined: Wed Jan 31, 2007 3:39 am
Posts: 299
Location: San Diego, California. USA
Post Re: data to file.
Hi Scooter_x

I was wondering if you figured out your problem, and if not are you willing to post your code so we can take a look at it?

Scott B-)

Mmmm Legos B-)

My Robot Projects:

Sat Feb 13, 2010 3:57 am
Profile WWW

Joined: Sun Dec 27, 2009 2:58 pm
Posts: 6
Post Re: data to file.
I've written a very simple program to do exactly what you're talking about. I too analyze my data in a spread sheet and without the help of a macro or some other modifications to the file, you can't graph it. So I've written a .NET 3.5 console application that will look for any CSV files in the same directory (that don't end in "Transposed") and ask you how many columns you'd like the data to be in. So if I collect 3 data points each loop and want them to be 3 columns, I would type 3 at the prompt. It will modify the file and append the word "Transposed" to the end of the filename so it won't get processed again.

Please let me know if it solves your issue.


P.S. You can run a virus scan on it, but I would never give anyone a virus.

File comment: Transposer [3.27 KiB]
Downloaded 385 times
Sat Feb 13, 2010 3:39 pm

Joined: Thu Sep 24, 2009 4:24 pm
Posts: 18
Post Re: data to file.
As of right now, I figured how to get the data out the way I want, ('/n' is different from '\n') but now I'm running into the problem that after I get through outputting my data to 'myFile.txt' after several regular numbers I run into random chinese characters and stuff from languages that probably nobody has ever even heard of. The odd thing is that I was about to find out after which row it started doing that to see if it may have been a factor with the spreadsheet software (, I opened it just now after not having even looked at my computer since yesterday, and it opened just fine. 7000 some-odd values. Wack. The differences being previously I was using windows7 and right now I'm in Linux, I still have to find out if I'm using the same versions of, but yea. I'm gonna try the program, wbmstr2good, and see what happens there. I'll let y'all know when I find out. Thanks.


Sat Feb 13, 2010 5:17 pm

Joined: Thu Sep 24, 2009 4:24 pm
Posts: 18
Post Re: data to file.
Here's my code too if anyone wanted to take a peek. Maybe something in here isn't right. I do have some stuff commented out because I was probably getting better results that way.
(sorry it's long)

#pragma config(Sensor, S1, SONAR, sensorSONAR)

#pragma config(Sensor, S4, END_BUTTON, sensorTouch)

#pragma config(Motor, motorA, motorA, tmotorNormal, openLoop, encoder)

#pragma config(Motor, motorB, motorB, tmotorNormal, openLoop, encoder)

#pragma config(Motor, motorC, motorC, tmotorNormal, PIDControl, encoder)

//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//

float gainA = 0.85; //gain motor A

float gainB = 0.85; //gain motor B

int gainCap = 30; //prevents from reading huge values

float offset = 30; //how far from the wall I wanna' be

float TargetPower = -80; //where I want my motors set

float powerA; //variable for motorA's power

float powerB; //variable for motorB's power

float errorA;

float errorB;

float turnA;

float turnB;

float sensor; //variable for sensor1 value, to do math

float sensorMath;

int iteration = 0;

int loopControl = 0;

//file IO stuff

const string sFileName = "myFile.txt";

int nFileSize = 100000;

TFileIOResult nIoResult;

TFileHandle hFileHandle;

string fileData;

task main()





fileData = 0;

Delete(sFileName, nIoResult);

hFileHandle = 22;

OpenWrite(hFileHandle, nIoResult, sFileName, nFileSize);

while (loopControl < 10)


iteration = 0;

sensorMath = 0;

while (iteration < 20) //loop, averages 20 sensor readings to avoid anomalous data


sensorMath = sensorMath + SensorValue[SONAR];



sensor = sensorMath / 20;

//if (SensorValue[SONAR] > 50) {sensor = 50;}

//if (SensorValue[SONAR] > 140) {sensor = 140;}

//if (SensorValue[SONAR] < 10) {sensor = 10;}

//motorA turn math

errorA = sensor - offset;

if (errorA > gainCap) {errorA = gainCap;}

turnA = gainA*errorA;

//motorB turn math

errorB = sensor - offset;

if (errorB > gainCap) {errorB = gainCap;}

turnB = gainB*errorB;

//calculate how far to turn

powerA = TargetPower - turnA;

powerB = TargetPower + turnB;

//caps power, my controller was trying to shoot tons of juice into the motors

if (powerA < -80){powerA = -80;}

if (powerA > 0){powerA = 0;}

if (powerB < -80){powerB = -80;}

if (powerB > 0){powerB = 0;}

//juice them motors up!

motor[motorA] = powerA;

motor[motorB] = powerB;

//convert to integers to print to screen, don't remember why I did this.
int powerAdisp = powerA;

int powerBdisp = powerB;

int errorDisp = (errorA+errorB)/2;

fileData = errorDisp;
//var for writing to file.
string fileDataTwo = "\n";

//out to screen stuff

nxtDisplayTextLine(1,"error: %d", errorDisp);

nxtDisplayTextLine(2,"powerA: %d", powerAdisp);

nxtDisplayTextLine(3,"powerB: %d", powerBdisp);

nxtDisplayTextLine(4,"Time: %d", time1[T1]/1000);

WriteString(hFileHandle, nIoResult, fileData); //writes 'errorDisp' (a float turned into a string) to the file

WriteString(hFileHandle, nIoResult, fileDataTwo);

//I couldn't see a way to write a float out to the file, I think kept giving me garbled stuff.

if (SensorValue[END_BUTTON]) //loop, ends program, outputs time1 to a text file


nxtDisplayTextLine(5,"Time: %d", time1[T1]);

motor[motorA] = 0;

motor[motorB] = 0;

loopControl = 10;

//WriteFloat(hFileHandle, nIoResult, errorDisp);

// Close(hFileHandle, nIoResult);





Sat Feb 13, 2010 5:38 pm

Joined: Thu Sep 24, 2009 4:24 pm
Posts: 18
Post Re: data to file.
Well, interesting. I haven't yet tried the .NET application, and yet I'm not getting those wack chinese characters. Odd. But I've run into another problem where I've made 2 files, each analyzing something similar, yet I only can upload 1 of the 2 files, and both sets of information are in the file that I am able to upload. But that's a topic for another thread that I started: some file I/O messed up stuff

Tue Feb 16, 2010 9:04 am
Display posts from previous:  Sort by  
Reply to topic   [ 6 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.