Help with CSonar on VEXPro.

Sorry to bother everyone again, but I could use some help setting up the VEX Ultrasonic sensors on my VEXPro.

There are several example programs, but they have routines written from scratch and don’t use the CSonar class. I tried writing a program using the commands from that class, but it had errors. I was able to fix all of them except for two. The errors involve the “Fire” and “GetVal” commands.

I was hoping there would be an example that I could look at and learn from. If someone could help me out again - I would really appreciate it!
I could use the routines in the examples, but I would really like to learn how to use the CSonar class.

I’m sorry - I just wanted to bump up my post before it rolls off the portal.

I was really hoping that someone would be able to help me with this. I’ve tried to figure it out myself, but there is something I’m missing and don’t understand. :frowning:

Sorry, somehow I missed your first post. Good thing you bumped it!

Post or PM me your code and I’ll have a look. I haven’t tried using the CSonar class yet, but I’m happy to give it a spin.


  • Dean

Here is a sample I just whipped up. It just loops, printing the range to the console once a second.

#include "sonar.h"

int main() {
    // Input to Port 15, Output to Port 16
    CSonar ping = CSonar::CSonar(14,15);
    int inches;

    while (1) {
        inches = ping.Fire(true);	// Wait for reading
        printf("%d inches\n", inches);
        sleep(1 /*second*/);


  • Dean

Thank you Dean, I really appreciate it. I’ll give it a try - this is way easier than the examples!

I knew I was missing something - I just needed to see an example. I have most of it, but I keep getting an error message “Cannot Call Member Function ‘int CSonar::Fire(bool)’ Without Object” on the “Fire” command line.

Setting up an object like you do for the other classes using a reference didn’t work. I’m assuming “ping” is the object and “CSonar ping” is setting this up? How did you know to do it this way?:confused:

I suspect you’ve mixed two steps here in one line. CSonar is a class, but you need an instance of that class to call Fire() first, and then you can call Fire() on that instance.

It can be confusing, and there is more than one way to accomplish the same thing.

In the example above, the variable “ping” holds an object of class CSonar. This variable is declared with “CSonar ping”, and it is filled in with an actual object when I call the constructor “= CSonar::CSonar(14,15);”. Constructors are just functions you can call to create an object of some class. Destructors are just the opposite - a function you call to dispose of an unneeded object.

It is a C++ convention that constructors are named the same as the class they are constructing, which is why you see CSonar on the line three times: The first time it is specifying the type of the variable “ping”. The second time it is naming the class that we are about to call into. The third time, it is the name of the function (aka “method”) we are calling within the class CSonar.

Once you’ve got the object, you can talk to it by accessing it’s methods and member variables as if they were structure elements: object.variable, or object.method().

This is just stuff you will learn over time. libqwerk doesn’t dig deep into C++, so you will just be dealing with a few objects unless you decide to go create a bunch of your own design.


  • Dean

The GPIO class had, presumably, been created behind the scenes as many other classes will rely on it. The documentation explained how to get a reference to the existing object. It would not make much sense to have many instances of a GPIO object as there is only one set of IO ports.

From the class documentation

To instantiate this class:

// by pointer
CQEGpioInt *pio = CQEGpioInt::GetPtr();
// or by reference
CQEGpioInt &io = CQEGpioInt::GetRef();

The CSonar class, as you discovered, has to be instantiated. You could of course have more than one sonar connected using different ports, you would need a new instance for each one. Again, from the documentation.

CSonar::CSonar ( unsigned int inputPort,
unsigned int outputPort

Constructor for a sonar module.

inputPort the digital port number of the “INPUT” connector of the sonar module. Can be 0 through 15 corresponding to digital connector 1 through 16, respectively.
outputPort the digital port number of the “OUTPUT” connector of the sonar module. Can be 0 through 15 corresponding to digital connector 1 through 16, respectively.

If you did have several sonars, there is another class used for managing them all, presumably so that they did not send their “pings” at the same time.

Constructor & Destructor Documentation
CSonarController::CSonarController ( )

default constructor.

CSonarController::CSonarController ( unsigned int numSonar,
CSonar * sonarArray],
unsigned int fireIntervalMs

This constructor takes the necessary arguments to set up a working sonar array.

numSonar the number of sonars in the sonar array.
sonarArray an array of pointers to CSonar instantiations. The order of the CSonar pointers determines the firing order of the sonar array.
fireIntervalMs the amount of time in milliseconds to wait between subsequent sonar firings. This value should not be below 10 ms.

0 if success -1 if error.

I’m sorry I didn’t see your first message or I would have posted my code. I’m also sorry I didn’t get back last night. I was working on my program and it was late when I got done. BTW - it works great now!

Thank you for the great explaination - it was easy for me to understand and make sense of. I have figured out “how” to do a lot of things - just not all of the “why’s”. I still need to learn all of the terms too, but I’m starting to pick things up.

Have you looked at this one yet. I was thinking about trying it with a couple Ultrasonic Sensors, but I’m not sure how to put it all together.
I know what arrays are and I was reading about pointers this afternoon. I’m pretty sure about the CSonar instantiations too.

Any help or suggestions would be great. Usually, all I need is an example to look at along with instructions I can understand then I can start figuring things out for myself. That’s why I wish the documentation would have included examples.

Thanks again for all of the help,