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






Reply to topic  [ 15 posts ] 
Writing data to a file from an integer 
Author Message
Rookie
User avatar

Joined: Thu Jul 30, 2009 6:37 am
Posts: 13
Location: Brisbane, Australia
Post Writing data to a file from an integer
I am tring to write a program which uses 2 light sensors to distinguish between white, black & green. I am using this program to automatically generate thresholds and save them for reading in the other program
Here is my code:
Code:
#pragma config(Sensor, S1,     sLeft,               sensorLightActive)
#pragma config(Sensor, S2,     sTouch,              sensorTouch)
#pragma config(Sensor, S4,     sRight,              sensorLightActive)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

#include "lightdata.dat"//file to write to

int press = 1;
int wl = 0;
int wr = 0;
int bl = 0;
int br = 0;
int gl = 0;
int gr = 0;
int h = 0;
int l = 0;
int calc = 1;

task main(){
  while(calc == 1){//loop, calc is trigger
     if(sTouch == 1){//touch sensor press
      press = press++;//press count plus 1
     }
    switch(press){
         case 1: wl = SensorValue(sLeft); //white on left
                 wr = SensorValue(sRight);//white on right
                 break;//before press 1
         case 2: bl = SensorValue(sLeft);//black on left
                  br = SensorValue(sRight);//black on right
                  break;//before press 2
        case 3: gl = SensorValue(sLeft);//green on left
                 gr = SensorValue(sRight);//green on right
                 break;//before press 3
        case 4: if(calc == 1){//on press 3
                   h = ((wr+gr)/2)+((wl+gl)/2)/2;//average between white and green on both sides
                  l = ((bl+gl)/2)+((br+gr)/2)/2;//average between black and green on both sides
                  OpenWrite(hFileHandle, nIoResult, "lightdata.dat", 2); //open lightdata.dat for writing, 2 bytes
                  WriteByte(hFileHandle, nIoResult, nParm);//write int h to file
                  WriteByte(hFileHandle, nIoResult, nParm);//write int l to file
                  calc = 2;//end the loop
                }
                break;
    }
  }
}

Details on reading these from another file would be appreciated

_________________
If you want my name, look at my name!
THE CAKE IS A LIE


Thu Aug 13, 2009 6:07 am
Profile WWW
Expert
User avatar

Joined: Tue Oct 14, 2008 7:16 pm
Posts: 171
Location: Investigating an unidentified ship sighted in Sector 31428
Post Re: Writing data to a file from an integer
I believe the functions you are looking for are the following,
  • OpenRead(short hFileHandle, TFileIOResult& nIoResult, const string& sFileName, short FileSize)
  • ReadByte(short hFileHandle, TFileIOResult& nIoResult, const byte nParam)

Looking at your code, however, I'm not sure your usage of the Open/Write functions is correct. You appear to be passing the functions variables which do not exist. I believe you have to create local variables of type short for hFileHandle and type TFileIOResult for nIoResult, and you must pass your variables h and l in place of nParam in the Read/Write functions in order to have them written to the file. This code shouldn't even compile, frankly.

I'm not 100% certain as I've never actually used the File IO functions, but logic seems to dictate that the way you have used them is slightly incorrect.

_________________
Captain, Head programmer, School of the Arts, Silverbots Robtics Team #2890
Code:
using namespace System;
using namespace Genius;
using namespace Personality;
public ref class Nerd : Geek, IAnserable
{
    Geek::Type brainMode = Geek::Type::Programmer;
}


Thu Aug 13, 2009 2:27 pm
Profile
Rookie
User avatar

Joined: Thu Jul 30, 2009 6:37 am
Posts: 13
Location: Brisbane, Australia
Post Re: Writing data to a file from an integer
Thanks.
I still don't get what is supposed to happen with nIoResult

_________________
If you want my name, look at my name!
THE CAKE IS A LIE


Thu Aug 13, 2009 4:49 pm
Profile WWW
Expert
User avatar

Joined: Tue Oct 14, 2008 7:16 pm
Posts: 171
Location: Investigating an unidentified ship sighted in Sector 31428
Post Re: Writing data to a file from an integer
I believe the type TFileIOResult is an enum, so the variable which gets passed as the second argument, nFileIoResult, will be set to one of the values in that enum which will indicate whether the Read/Write succeeded, failed, encountered an error, etc. You could then theoretically test for a result by anding (note I mean bitwise and: &, not logical and: &&) it with one of the enum values. As a general rule, you can probably ignore it, but you still have to create a variable of type TFileIOResult and pass it to the function for it to work.

_________________
Captain, Head programmer, School of the Arts, Silverbots Robtics Team #2890
Code:
using namespace System;
using namespace Genius;
using namespace Personality;
public ref class Nerd : Geek, IAnserable
{
    Geek::Type brainMode = Geek::Type::Programmer;
}


Thu Aug 13, 2009 6:00 pm
Profile
Rookie
User avatar

Joined: Thu Jul 30, 2009 6:37 am
Posts: 13
Location: Brisbane, Australia
Post Re: Writing data to a file from an integer
Thanks, any ideas on writing these to a variable in another program. Obviously you need to #include "lightsensor.dat"
possibly:
Code:
#include "lightdata.dat"//file to write to
const string sFileName = "lightdata.dat";
short hFileHandle;
short nIoResult;

OpenRead(hFileHandle, nIoResult, sFileName, 2);//open lightdata.dat
ReadByte(hFileHandle, nIoResult, h);//read h value
ReadByte(hFileHandle, nIoResult, l);//read l value


Would h & l be added as individual variables?

_________________
If you want my name, look at my name!
THE CAKE IS A LIE


Fri Aug 14, 2009 4:33 am
Profile WWW
Site Admin
Site Admin
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3654
Location: Rotterdam, The Netherlands
Post Re: Writing data to a file from an integer
Tom,

In my driver suite I have a driver for the light sensor that allows you to write calibration values to the NXT's file system. You can download it here: http://rdpartyrobotcdr.sourceforge.net/, the driver you're looking for is called LEGOLS-driver.h in the drivers/ sub directory. You can also just view the code here: http://rdpartyrobotcdr.sourceforge.net/ ... ource.html

Regards,
Xander

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]


Fri Aug 14, 2009 4:39 am
Profile WWW
Rookie
User avatar

Joined: Thu Jul 30, 2009 6:37 am
Posts: 13
Location: Brisbane, Australia
Post Re: Writing data to a file from an integer
Xander,
I have had repeated looks at your driver suite, but have never been able to figure out the reading from the driver.
You have done a good job of documentation, I am just not that great at figuring programming out. An explanation would be appreciated. I modified LEGOLS-test2.c for myself, I just cant work out how to implement it in another program, or with 2 light sensors

_________________
If you want my name, look at my name!
THE CAKE IS A LIE


Fri Aug 14, 2009 4:42 am
Profile WWW
Site Admin
Site Admin
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3654
Location: Rotterdam, The Netherlands
Post Re: Writing data to a file from an integer
Tom,

Yeah, I agree, the file system calls are really confusing and took me a long time to the hang of. I mainly just used the tried and tested method of "fiddling with it until it did what I wanted it to".

You need to specify the size of the file you want to write to. I never figured out how to append to a file. I just nuke it each time.

Why do you have an include line for your .dat file?

Regards,
Xander

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]


Fri Aug 14, 2009 4:53 am
Profile WWW
Rookie
User avatar

Joined: Thu Jul 30, 2009 6:37 am
Posts: 13
Location: Brisbane, Australia
Post Re: Writing data to a file from an integer
Xander, I have no idea about the include, it just seem logical to include the file I wanted to write to. I like your idea of fiddling until it works, the problem is, this is for my school(and I am at home) so I cant access my robot at all times. The forums seemed easier at the time.

_________________
If you want my name, look at my name!
THE CAKE IS A LIE


Fri Aug 14, 2009 4:55 am
Profile WWW
Site Admin
Site Admin
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3654
Location: Rotterdam, The Netherlands
Post Re: Writing data to a file from an integer
Tom,

Well, check out how I did it in the LEGOLS driver. I use a #define for the filename, not an #include :)

Not having access to your robot sucks. I would really recommend getting your own set, it'll make your life a lot easier.

Regards,
Xander

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]


Fri Aug 14, 2009 5:02 am
Profile WWW
Rookie
User avatar

Joined: Thu Jul 30, 2009 6:37 am
Posts: 13
Location: Brisbane, Australia
Post Re: Writing data to a file from an integer
I have my own set, but retail is nowhere near as comprehensive as education, and I couldnt be bothered to go scouting around the neighbourhood to finnd an educatonal version ofr sale

_________________
If you want my name, look at my name!
THE CAKE IS A LIE


Fri Aug 14, 2009 5:42 am
Profile WWW
Site Admin
Site Admin
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3654
Location: Rotterdam, The Netherlands
Post Re: Writing data to a file from an integer
Ah but the retail version will work just fine with your program, no? You could easily get another light sensor from the Lego shop. To get more pieces you could get some Technic Lego boxes instead. That is what I did.

Regards,
Xander

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]


Fri Aug 14, 2009 5:47 am
Profile WWW
Rookie
User avatar

Joined: Thu Jul 30, 2009 6:37 am
Posts: 13
Location: Brisbane, Australia
Post Re: Writing data to a file from an integer
Thanks for the idea Xander, I'll get started right away!
EDIT: 1 last question. With your LS driver, if I calibrate in 1 program, will the calibration work for all others?

_________________
If you want my name, look at my name!
THE CAKE IS A LIE


Fri Aug 14, 2009 5:53 am
Profile WWW
Site Admin
Site Admin
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3654
Location: Rotterdam, The Netherlands
Post Re: Writing data to a file from an integer
Yes, that is correct. The data will be read the first time you query the normalised sensor value.

Regards,
Xander

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]


Fri Aug 14, 2009 8:01 am
Profile WWW
Expert
User avatar

Joined: Tue Oct 14, 2008 7:16 pm
Posts: 171
Location: Investigating an unidentified ship sighted in Sector 31428
Post Re: Writing data to a file from an integer
TomManderson wrote:
Thanks, any ideas on writing these to a variable in another program. Obviously you need to #include "lightsensor.dat"
possibly:
Code:
#include "lightdata.dat"//file to write to
const string sFileName = "lightdata.dat";
short hFileHandle;
short nIoResult;

OpenRead(hFileHandle, nIoResult, sFileName, 2);//open lightdata.dat
ReadByte(hFileHandle, nIoResult, h);//read h value
ReadByte(hFileHandle, nIoResult, l);//read l value


Would h & l be added as individual variables?


This is exactly how you would retrieve the values; you would have to declare h and l in the program reading the file as well. Additionally as Xander said, you don't have to #include files that you read or write from in your program, in fact if a file used in a #include directive is not found, the program won't compile; #includes are only for other code files that your program uses like .h or .c files. Like Xander said you could use #define to define a symbol which could be used in place of the file name. Like so,
Code:
#define filename "lightdata.dat"

You will also have to declare a variable of type short to hold the file size; if you just pass a number it assumes the value is of type int, since it appears there is no type casting (if you don't know what that means, don't worry about it), the program won't compile; the arguments are "wrong".

_________________
Captain, Head programmer, School of the Arts, Silverbots Robtics Team #2890
Code:
using namespace System;
using namespace Genius;
using namespace Personality;
public ref class Nerd : Geek, IAnserable
{
    Geek::Type brainMode = Geek::Type::Programmer;
}


Fri Aug 14, 2009 1:31 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 15 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.