Does Vexcode text support interfaces?

Does Vexcode text support interfaces?

Please define “interfaces”.

2 Likes

lol, my dad knows more about programming than I do and was going to teach me some stuff but wanted to know if “Vexcode text support interfaces.” Basically, we want to create a virtual inertial sensor that could factor into a drivetrain but we could control the variable of this nonexistent sensor from the code itself, like average multiple inertial sensors and put the value from them into a drivetrain.

Now we speak, awesome!
“Interface” is a general concept, which C++ (and thus VexCode text) supports, but calls it differently. In general, you’re trying to do an abstraction of a heading provider, i.e. describe an interface (contract) of an entity providing heading. This could be achieved like this, for example:

class HeadingProvider {
public:
    virtual float getHeading() = 0;
};

Now you have defined the “interface” for the heading provides, an abstract class with one purely virtual function.
(Edit: incomplete, will follow up)

3 Likes

Next, you provide an implementation, say GyroHeadingProvider:

class GyroHeadingProvider : public HeadingProvider {
    vex::gyro m_gyro;
public:
    virtual float getHeading() {
        return m_gyro.value(vex::rotationUnits:deg);
    }
};

(of course with proper constructors, passing in the port etc.)
In a similar fashion, you can implement other providers that are more advanced, like doing a fusion of multiple sensors or measurement methods.
The rest of your code would then only use the HeadingProvider interface, even when dealing with different implementations. Say you have a chassis class that implements driving straight and taking turns. You’d need to configure it with a HeadingProvider instead of directly using vex::gyro:

class Chassis {
    HeadingProvider *m_heading;
public:
    Chassis(HeadingProvider *heading) : m_heading(heading) {}
    void turn(float toAngle) {
        while (heading->getHeading() < toAngle) {...}
    }
};

HeadingProvider *h = new GyroHeadingProvider(...);
Chassis *ch = new Chassis(h);
ch->turn(50);

I really like the approach you’re describing, that’s what my team does (even though using PROS and not VexCode). They have one implementation using the old ADI gyro, one provider that uses dead reckoning of the drivebase motors, one that uses ADI gyro as an analog yaw rate sensor and now, with the new V5 gyro, one that uses the V5 gyro. Switching from ADI to V5 was a one-line change in calling the constructor…

3 Likes

One more interesting observation (since you mentioned drivetrain and I can assume you’re talking about the vex::drivetrain (or vex::smartdrive):
Now that V5 supports 2 very different gyros (ADI avd V5), the smart engineers at Vex were facing the very same issue and used the very same approach. If you inspect the VexCode SDK, namely include/vex_device.h, you’ll find a declaration of a class guido, which is pretty much (a much better) abstract heading provider. Both vex::gyro and vex::inertial do extend (implement) the internal vex::guido class, while vex::smartdrive internally uses pointer to the vex::guido for it’s turn functions. Unfortunately, they didn’t export that generic API and instead have one set of vex::gyro-based ctors and another set of vex::intertial-based. If they allowed the users to pass in a guido reference (or a pointer), you’d be able to plug your implementation right in there.

That is the power of proper abstraction!

3 Likes

In general, the answer to “does vexcode text support [c++ language feature]” is “yes” - an industry-standard compiler (clang IIRC) is used, you’re writing full-on normal c++.

4 Likes

Thanks for all the replies, I think that answers my question.

2 Likes