Hi Jonathan,
The main problem here is the way you are creating the objects DigitalOut, AnalogIn and Serial. These want to be part of the Sonar object, so you actually have to pass the constructor variables (e.g. the pin names) to them as part of the Sonar constructor. By example:
class SonarRangeFinder
{
private:
DigitalOut SOutput(PinName);
AnalogIn AInput(PinName);
Serial SInput(PinName);
becomes
class SonarRangeFinder
{
private:
DigitalOut SOutput; // no constructor arguments here
AnalogIn AInput;
Serial SInput;
and the body of your sonar constructor:
SonarRangeFinder::SonarRangeFinder(int choice, PinName tx, PinName rx)
{
DigitalOut SOutput(tx);
AnalogIn AInput(rx);
Serial SInput(tx, rx);
becomes
SonarRangeFinder::SonarRangeFinder(int choice, PinName tx, PinName rx) : SOutput(tx), AInput(rx), SInput(tx, rx)
{
Note the notation which says how to construct the objects Sonar contains, before calling the Sonar constructor itself.
This will then initialise them in the way you are intending.
A couple of other things from this code:
response = SInput.scanf();
x = atof(response);
isn't a valid way to use scanf (or at least it won't give you the results you want!). You probably should try something like:
SInput.scanf("%f", &x);
Also, you have multiple objects trying to use the same pins. That won't really work, as the pins have to be configured to do something (e.g. be a serial port, or an analog port). Not sure if this is a requirement of what you are trying to do, but it'd be worth looking at a bit more.
Simon
I'm trying to encapsulate an AnalogIn and Serial object in a class for an ultrasonic rangefinder that has several output options. The idea is to have the user select the mode and pins with the parameters and then the query function will return the output from the selected output option. For example, the user might create SonarRangeFinder SRF(0, p13, p14) and then call Query() and the method will return the value from the analog input of the range finder.
There are several errors in here, but the one I can't get around is "Nonstandard form for taking the address of a member function" and "expression must have class type" at "x = AInput.read();". Does anyone know how to correct these errors?
#ifndef SonarRangeFinderLibrary
#define SonarRangeFinderLibrary
#include "mbed.h"
class SonarRangeFinder
{
private:
DigitalOut SOutput(PinName);
AnalogIn AInput(PinName);
Serial SInput(PinName);
int Mode;
int Unit;
public:
SonarRangeFinder(int, PinName, PinName);
void SelectUnit(int);
float Query();
};
#endif
#include "SonarRangeFinderB.h"
SonarRangeFinder::SonarRangeFinder(int choice, PinName tx, PinName rx)
{
DigitalOut SOutput(tx);
AnalogIn AInput(rx);
Serial SInput(tx, rx);
Mode = choice;
Unit = 0;
}
void SonarRangeFinder::SelectUnit(int choice)
{
Unit = choice;
}
float SonarRangeFinder::Query()
{
char response[5];
float x;
switch (Mode)
{
case 0:
x = AInput.read();
break;
case 1:
SOutput.write(1);
wait_ms(1);//hold pin high for at least 20 us to signal a serial query
SOutput.write(0);
wait_ms(100);//wait for a response
response = SInput.scanf();
x = atof(response);
break;
}
return x;
}