ROBOTC.net forums
http://robotc.net/forums/

Pass a sensor as an argument?
http://robotc.net/forums/viewtopic.php?f=1&t=4844
Page 1 of 1

Author:  fxlj007 [ Mon Sep 17, 2012 1:41 pm ]
Post subject:  Pass a sensor as an argument?

Is is possible to pass a sensor as an argument to a function? If yes how to declare the type of the parameters?

Author:  JohnWatson [ Mon Sep 17, 2012 1:58 pm ]
Post subject:  Re: Pass a sensor as an argument?

You should be able to pass sensor values as parameters to a function. For example:

Code:
#pragma config(Sensor, in1,    touch1,         sensorTouch)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

void test(int number)
{
   
}

task main()
{
test(SensorValue[touch1]);
}


You have to make sure that the data type you are sending up matches what is required for the function (in this case, 'int'; 'bool' would also work). You also have to make sure that you are sending the value of the sensor (by using the SensorValue command) and not the index of the sensor:

Code:
#pragma config(Sensor, in1,    touch1,         sensorTouch)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

void test(int number)
{
   
}

task main()
{
test(touch1);
}


The above code will compile but will pass the index location of the 'touch1' sensor, not the actual value of it.

Author:  MHTS [ Mon Sep 17, 2012 2:03 pm ]
Post subject:  Re: Pass a sensor as an argument?

I am not sure that's what he's asking. I thought he's asking if you can pass the sensor ID to a function. If so, yes, the type for it is tSensors.
Code:
#pragma config(Sensor, in1,    touch1,         sensorTouch)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

void Func1(tSensors sensorID)
{
    nxtDisplayTextLine(0, "value=%d", SensorValue[sensorID]);
}

task main()
{
    Func1(touch1);
}

EDIT: fixed the spelling of tSensors in the code.

Author:  fxlj007 [ Mon Sep 17, 2012 2:51 pm ]
Post subject:  Re: Pass a sensor as an argument?

>>JohnWatson: Sorry that I didn't express my question clearly. MHTS gets my question. Still it's very nice of you :-)

>>MHTS:
I tried to compile your codes but it yields below compilation errors. Is there anything wrong? Or with the software version?

File "tSensorTest.c" compiled on Sep 18 2012 02:48:32
**Error**:Variable 'tSensor' declaration must be qualified with type. Type 'short' used.
**Error**:Ummatched left parenthesis '('
**Error**:Expected->')'. Found 'sensorID'
**Error**:No body declared for procedure
**Error**:Expected->'}'. Found 'sensorID'
**Error**:Undefined variable 'sensorID'. 'short' assumed.
**Error**:Missing ';' before ')'
**Error**:Executable statements not valid in 'main' declaration block
**Error**:Unexpected scanner token-> ')'
*Warning*:Meaningless statement -- no code generated
**Error**:Duplicate definition for task 'main'.

Author:  MHTS [ Mon Sep 17, 2012 3:32 pm ]
Post subject:  Re: Pass a sensor as an argument?

I am sorry, I don't have RobotC in front of me. So I told you the wrong spelling. It should be tSensors not tSensor.
Sorry about that.

Author:  mightor [ Tue Sep 18, 2012 1:25 am ]
Post subject:  Re: Pass a sensor as an argument?

Do you have the NXT set as the platform to compile for?

- Xander

Author:  fxlj007 [ Tue Sep 18, 2012 10:06 am ]
Post subject:  Re: Pass a sensor as an argument?

Dear All,

Case settled. Thanks for your help.

Btw, I'm using RobotC 3.5 and a set of NXT for my undergraduate project. Can you recommend some material that introduces more advanced stuff of RobotC? The curriculum on RobotC's website seems a little too "basic" for me LOL~~

Author:  Spiked3 [ Tue Sep 18, 2012 11:39 am ]
Post subject:  Re: Pass a sensor as an argument?

More advanced stuff? Programming wise or robot wise?

C is (kinda) C. There are lots of books on that subject.

Robot wise; There is not anything I know of, published as a book, it has been discussed.

http://botbench.com/blog/ publishes code, as do I http://www.spiked3.com/

I often find very good stuff from http://nxttime.wordpress.com/

Other than that, google is your friend.

Author:  JohnWatson [ Tue Sep 18, 2012 12:19 pm ]
Post subject:  Re: Pass a sensor as an argument?

fxlj007 wrote:
Dear All,

Case settled. Thanks for your help.

Btw, I'm using RobotC 3.5 and a set of NXT for my undergraduate project. Can you recommend some material that introduces more advanced stuff of RobotC? The curriculum on RobotC's website seems a little too "basic" for me LOL~~


I would also suggest checking out our wiki (https://www.robotc.net/wiki) for advanced programming material. We are continually adding to and updating it (we recently added overviews on pointers and recursion, for example). It also has a 'Programming Tips and Tricks' section that starts to dive into the more advanced features of ROBOTC with topics such as multitaskingand fuzzy logic being covered. Plus, most of the information is provided by the ROBOTC community members.

If anyone has anything they would like to add or like to see added to the wiki, please do not hesitate to let me know!

Author:  roboRed [ Sat Dec 15, 2012 11:34 pm ]
Post subject:  Re: Pass a sensor as an argument?

Interesting, I didn't know that before. But, however, could you guys formulate some examples of ways to use this code?

thanks in advance. :shock: :? :P :oops: :?: :idea:

Author:  roboRed [ Sat Dec 15, 2012 11:35 pm ]
Post subject:  Re: Pass a sensor as an argument?

I mean also real world applications of this structure?

thanks :bigthumb: :eek:

Page 1 of 1 All times are UTC - 5 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/