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

Reply to topic  [ 2 posts ] 
some file I/O messed up stuff 
Author Message

Joined: Thu Sep 24, 2009 4:24 pm
Posts: 18
Post some file I/O messed up stuff
Okay, here I go:

I've written a Proportional Controller for my robot, it works dandy. I do however have to write a couple of files. I want to write my error value to one file, and my actual sensor reading to another (I've capped my error value to not let it screw my bot up when my sensor gets saturated). Here's my issue:

I can get sensorFile uploaded just fine, but errorFile doesn't upload. Says something about how it didn't upload all the data correctly. Gives me no file. But the biggest weird part is that my sensor data and my error data wind up in the SAME file called sensorFile.txt!! I don't know what's going on. I don't know much about the handles or any file I/O stuff for that matter, I just kinda' took a crash course last week to get my assignment turned in properly.

Anyway, I'll leave my code in the next post. Hopefully someone can help me. Thanks.


PS- I bolded the file I/O stuff in the code so it's easier to read

Last edited by scooter_x on Tue Feb 16, 2010 9:09 am, edited 1 time in total.

Mon Feb 15, 2010 11:50 pm

Joined: Thu Sep 24, 2009 4:24 pm
Posts: 18
Post Re: some file I/O messed up stuff
#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.75; //gain motor A
float gainB = 0.8; //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 errorFile = "errorFile.txt";
const string sensorFile = "sensorFile.txt";
int nFileSize = 100000;
TFileIOResult nIoResult;
TFileHandle handleError = 84;
TFileHandle handleSensor = 24;
string errorFileData;
string sensorFileData;
string newLineChar = "\n";

task main()
ClearTimer(T1); //sets run timer to 0 so I can calculate my runtime

//prep my files
errorFileData = 0;
Delete(errorFile, nIoResult);
Delete(sensorFile, nIoResult);
OpenWrite(handleError, nIoResult, errorFile, nFileSize);
OpenWrite(handleSensor, nIoResult, sensorFile, nFileSize);

while (loopControl < 10) //main loop

iteration = 0;
sensorMath = 0;
while (iteration < 50) //loop, averages sensor readings to avoid anomalous data

sensorMath = sensorMath + SensorValue[SONAR];


sensor = sensorMath / 50;

sensorFileData = sensor;
WriteString(handleSensor, nIoResult, sensorFileData); //write SensorValue[SONAR]'s averaged value to file
WriteString(handleSensor, nIoResult, newLineChar); //add a newline char

//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 PID was shooting 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 (integers are the only thing the screen likes)
int powerAdisp = powerA;
int powerBdisp = powerB;
int errorDisp = (errorA+errorB)/2;
errorFileData = errorDisp;

//out to screen stuff
nxtDisplayTextLine(1,"sensor: %d", SensorValue[SONAR]);
nxtDisplayTextLine(2,"error: %d", errorDisp);
nxtDisplayTextLine(3,"powerA: %d", powerAdisp);
nxtDisplayTextLine(4,"powerB: %d", powerBdisp);
nxtDisplayTextLine(5,"Time: %d", time1[T1]/1000);

WriteString(handleError, nIoResult, errorFileData); //writes 'errorDisp' to the file
WriteString(handleError, nIoResult, newLineChar); //newLine char

if (SensorValue[END_BUTTON]) //loop, ends program, outputs time1
nxtDisplayTextLine(5,"Time: %d", time1[T1]);
motor[motorA] = 0;
motor[motorB] = 0;
loopControl = 10;


Mon Feb 15, 2010 11:51 pm
Display posts from previous:  Sort by  
Reply to topic   [ 2 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.