View unanswered posts | View active topics It is currently Sun Dec 21, 2014 6:18 pm






Reply to topic  [ 11 posts ] 
Pass a sensor as an argument? 
Author Message
Rookie
User avatar

Joined: Sun Jun 17, 2012 11:18 am
Posts: 26
Post 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?


Mon Sep 17, 2012 1:41 pm
Profile
Site Admin
Site Admin

Joined: Thu May 24, 2012 12:15 pm
Posts: 619
Post 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.

_________________
Check out our Blog! And our Facebook page!
Need help? Take a look at our Wiki and our Forums.

I just met you,
And this is crazy,
But here's my code now,
So fix it, maybe?
~ Carly Rae Jepsen parody


Mon Sep 17, 2012 1:58 pm
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1372
Post 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.


Last edited by MHTS on Mon Sep 17, 2012 3:40 pm, edited 2 times in total.



Mon Sep 17, 2012 2:03 pm
Profile
Rookie
User avatar

Joined: Sun Jun 17, 2012 11:18 am
Posts: 26
Post 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'.


Mon Sep 17, 2012 2:51 pm
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1372
Post 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.


Mon Sep 17, 2012 3:32 pm
Profile
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3297
Location: Rotterdam, The Netherlands
Post Re: Pass a sensor as an argument?
Do you have the NXT set as the platform to compile for?

- 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]


Tue Sep 18, 2012 1:25 am
Profile WWW
Rookie
User avatar

Joined: Sun Jun 17, 2012 11:18 am
Posts: 26
Post 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~~


Tue Sep 18, 2012 10:06 am
Profile
Expert

Joined: Tue Feb 28, 2012 3:10 pm
Posts: 195
Post 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.

_________________
Mike aka Spiked3
http://www.spiked3.com


Tue Sep 18, 2012 11:39 am
Profile
Site Admin
Site Admin

Joined: Thu May 24, 2012 12:15 pm
Posts: 619
Post 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!

_________________
Check out our Blog! And our Facebook page!
Need help? Take a look at our Wiki and our Forums.

I just met you,
And this is crazy,
But here's my code now,
So fix it, maybe?
~ Carly Rae Jepsen parody


Tue Sep 18, 2012 12:19 pm
Profile
Expert
User avatar

Joined: Fri Nov 02, 2012 12:07 am
Posts: 163
Location: California, USA
Post 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:

_________________
string Robored = "Awesome"
~~Neil Balch~~


Sat Dec 15, 2012 11:34 pm
Profile WWW
Expert
User avatar

Joined: Fri Nov 02, 2012 12:07 am
Posts: 163
Location: California, USA
Post Re: Pass a sensor as an argument?
I mean also real world applications of this structure?

thanks :bigthumb: :eek:

_________________
string Robored = "Awesome"
~~Neil Balch~~


Sat Dec 15, 2012 11:35 pm
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 11 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.