View unanswered posts | View active topics It is currently Mon Oct 21, 2019 7:13 pm






Reply to topic  [ 7 posts ] 
Getting same response to push of touch sensor repeatedly 
Author Message
Rookie

Joined: Sat Nov 07, 2009 9:30 pm
Posts: 6
Post Getting same response to push of touch sensor repeatedly
Hi all,
I'm really new to any sort of robotics, so sorry if this question isn't very clear. I'm not sure I even know how to word my questions yet. I've been through the 10 lesson "course" (webinar recordings and PDFs) from the Carnegie Mellon Robotics Academy, but that's about it.

I'm now trying to write a program that includes my robot offering a behavior every time one of the touch sensors is bumped (pushed and released).

The whole program is an infinite loop - while(true).

I know how to get the robot to do different things depending on whether a program is just starting, or the touch sensor is pushed and held, or the touch sensor was pushed and is now released; but my goal is to get it to offer a certain behavior each time a sensor is bumped, repeatedly within an infinite loop. Help please.

TIA,
Meeeee


Sat Nov 07, 2009 10:08 pm
Profile
Expert
User avatar

Joined: Tue Oct 14, 2008 7:16 pm
Posts: 171
Location: Investigating an unidentified ship sighted in Sector 31428
Post Re: Getting same response to push of touch sensor repeatedly
You are being a bit ambiguous. Do you mean that you have, say, four touch sensors and pressing each one gives a different action, or do you want each subsequent press of the same sensor to yield a different action.

If you mean the former, I would recommend writing a function which contains the code for each desired action (that is if you want four different things to happen, you write four different functions). Then you test for a sensor press and when it occurs, tell it to execute the desired function.

If you mean the latter, you will need a global counter, or an array of counters if you are using multiple sensors. Then when the sensor is pressed you can increment the counter and then execute the action based on what the current count is (you could also execute based on the current count and then increment, it's up to you). Note that you will have to cycle your counter, that is set it to zero rather than increment it, if it is at its max value. This may get a bit complicated depending on how many sensors you use and how many actions you want to be able to perform; it would probably be best to break the code up into several functions, some of which may be reusable depending on your mechanism.

_________________
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;
}


Mon Nov 09, 2009 7:09 pm
Profile
Rookie

Joined: Sat Nov 07, 2009 9:30 pm
Posts: 6
Post Re: Getting same response to push of touch sensor repeatedly
I *do* tend to dive right into stuff, head first. Thank you much for the help. :)

My goal is a little different than either of the two you described. I want each subsequent push of a touch sensor to yield the same behavior. Two touch sensors, each with their own designated behavior.

Thus far, I've only learned how to program the touch sensors to yield a given behavior once. Granted that could be one behavior for pushing the sensor and another for releasing it, but I'm talking about what NXT-G calls "bumping" the sensor button. I'm trying to learn how to program it to offer the same behavior again for a 2nd, 3rd, 4th, 5th..... bump of the sensor.

Is "bump" the right terminology or is that specific to NXT-G? Is there another term for the same thing that is more common in RobotC speak?

Thank you again! I really appreciate it.


Mon Nov 09, 2009 10:54 pm
Profile
Expert
User avatar

Joined: Tue Oct 14, 2008 7:16 pm
Posts: 171
Location: Investigating an unidentified ship sighted in Sector 31428
Post Re: Getting same response to push of touch sensor repeatedly
You are actually seeking the first of my two scenarios, but anyway. RobotC will tell you when the touch sensor is pressed and when it is not, it does not distinguish between the press, the release, the press-release cycle, or the press and hold, at least not that I know of (Windows programmers will doubtless be familiar with this style of message handling). To distinguish between them you must have done a fair bit of work with timers and such, I find it a bit surprising that you're getting stuck here, but no matter, sometimes the answer is so simple that completely over look it.

When we speak of testing a touch sensor we are simply getting its value using SensorValue[sensorName], where sensorName is the name you gave the sensor when you configured the bot, if the sensor is pressed the value will be 1, if not, 0. Within the main loop you simply test, using and if statements,
Code:
if(SensorValue[touch1]) //(or if(SensorValue[touch] == 1) these are equivalent) Test for touch sensor 1 being pressed
{
    SomeAction(); //call a function to perform and action
}
if(SensorValue[touch2])
{
    SomeOtherAction();
}

This should execute the same action as defined in the functions SomeAction() and SomeOtherAction() repeatedly, whenever you press the switch. Now you may have to add some kind of mechanism to prevent the action from being started multiple times, a global boolean will work for this, thusly:
Code:
bool bRunning = false;

void SomeAction() //Functions must at least be declared before they are referenced; you at least need "void SomeAction();" before the function is used.
{
    bRunning = true; //set to true when the function starts so that the function can't be started multiple times
    //Add code to perform your action
    bRunning = false; //set to false at the very end so that it can be run again
}

task main()
{
    while(true)
    {
        if(SensorValue[touch1] && !bRunning) //If the sensor is pressed and the function is not already running...
        {
            SomeAction(); //...perform some action
        }
    }
}

There are obviously some other limitations involved with this method, you will have to decide how to exactly implement all this.

P.S. If you are unfamiliar with C\C++ (experience with Java or C# semi-counts), I would recommend buying or borrowing (from a receptacle of knowledge or a fellow nerd) a simple C primer book to at least familiarize yourself with the basics of the language; bearing in mind, however, that RobotC is not standard ANSI C and thus some aspects will not apply.

_________________
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;
}


Tue Nov 10, 2009 1:12 am
Profile
Rookie

Joined: Sat Nov 07, 2009 9:30 pm
Posts: 6
Post Re: Getting same response to push of touch sensor repeatedly
Oh my gosh! :lol: You were absolutely on the mark that it was something so simple, I just didn't have a clue! Because I am so new to programming, I actually took the boolean logic *too* literally and thought that I *had* to set the SensorValue lessor than, greater than, or equal to a specific value. I couldn't see how I could set a specific value and not run into problems with pushing the sensor a random # of times. I had tried a number of things, and nothing had worked, but I don't know for sure if it was this specifically or some other part of my code.

I haven't messed with the global boolean yet, but did get the first basic part of the program running properly. Will dig into this soon. Thanks for the info.

I'm all for getting a book to get me started. I've been hesitant to do that because of all the warnings about how RobotC is not C, but I understand what you're saying about just getting a basic feel for the language. Is there any particular book you might recommend?

Thank you! :)


Tue Nov 10, 2009 11:06 pm
Profile
Expert
User avatar

Joined: Tue Oct 14, 2008 7:16 pm
Posts: 171
Location: Investigating an unidentified ship sighted in Sector 31428
Post Re: Getting same response to push of touch sensor repeatedly
Yes, sometimes the solution is so simple that it sails right over our heads. As far as touch sensors go, they can only exist in two states, pushed or not pushed. Programmatically, this translates to a 1 or 0 respectively; SensorValue[] should never return anything other than a 1 or 0 when you're getting the value of a touch sensor. Thus all you have to do is test for on (or off, or both depending on what you want to do) and respond accordingly. If you were working with, say, the sonar sensor, then you would want to check for when the value was less than or greater than or equal to a certain value, but not here since only two values are possible.

As for what book you should get, any basic C primer book should do. I'm afraid I can't really recommend a good C book because I'm a C++ programmer (FYI C++ is a descended of C; the syntax is more or less exactly the same, but C++ adds a whole new dimension using the concepts of a "class", class inheritance, and some other fun stuff), but again, any basic book should be fine.

I can, however, tell you some of the things you can safely ignore, or at least not worry too much about (don't ignore anything, at least read everything so you have some understanding of what they're talking about :bigthumb:)
  • Don't worry about pointers or anything about "dynamic memory (allocation)". RobotC doesn't support pointers or dynamic memory simply because the NXT and the other platforms RobotC supports aren't designed to allocate memory dynamically. It really sucks, but oh well. :roll:
  • Ignore any references to the standard (library) headers. RobotC replicates most of the functions you might need from these files and incorporates automatically.
  • You don't have to worry too much about string management in RobotC because it implements a "string" type which behaves more like the C++ string type which does a lot of the nitty-gritty for you. Don't even both with using arrays for string. (Read the book to understand).
  • Creating and using your own structs is a little different in RobotC, again you'll understand what I'm talking about when you read the book.
  • There are limitations on the available basic types including no type double; the new version 2 lessens the restrictions somewhat, that is adds support for some types, but you still don't have the full range.

If you want to try some of the examples a book might provide, you'll need to pick up an IDE, and possibly a compiler if the IDE doesn't come packaged with one. I would personally recommend Microsoft's Visual C++ Express Edition, it's a free version of Visual Studio which is restricted to C/C++ coding (and also restricted to .NET and basic Win32 coding, no MFC, not that that means anything to you, but FYI), but it comes with all the powerful debugging, and most of the IDE, bells and whistles of the full version and should be more than enough for your purposes. You do have to specify the file extension as ".c" when you create new code files though, this is due to the fact that it is primarily a C++ IDE and the default extension is ".cpp". If you don't feel like Microsoft or think that path might be a bit daunting, there's also NetBeans, which is primarily a Java IDE, also free, about equal in quality and capability to Visual Studio (some would say better, but... :?), but has extensions for about two dozen other languages, some very obsucre and specialized, including C/C++, PHP, Ruby(???), Groovy(?!?), Ajax, Python, etc.; you can download the C/C++ oriented (read: "only") version. The only catch is you have to download a compiler and tell NetBeans where to find it; their web site can help you with that. Beyond that there are several free IDEs out there, Bloodshed is one which is packaged with a compiler, Eclipse is another one which you will see referenced in these forums, some books will even come with a CD which has IDE/compiler software on it. Ultimately it's up to you, and you don't have to download one, but it might be a good idea to experiment with the real thing before taking on the pseudo C thing (RobotC).

This should be more than enought to set you on the right path; remeber, if you get stuck, use the forums, some one will usually come to save you around here.

_________________
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;
}


Wed Nov 11, 2009 1:19 pm
Profile
Rookie

Joined: Sat Nov 07, 2009 9:30 pm
Posts: 6
Post Re: Getting same response to push of touch sensor repeatedly
Thank you! I've been taking your advice--I got a book on the basics and downloaded the IDE you recommended, and I've been learning *alot*. And it it is very helpful to know which parts I don't need to know quite as well, or won't have direct use of in RobotC.

Thank you, thank you! I'm sure I'll be back for more help eventually.


Sun Nov 15, 2009 9:19 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 7 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.