View unanswered posts | View active topics It is currently Thu Apr 17, 2014 8:14 pm






Reply to topic  [ 11 posts ] 
Release for pointers? 
Author Message
Rookie

Joined: Fri May 20, 2011 6:01 pm
Posts: 20
Post Release for pointers?
I have been trying for a fairly long time to embed functions in structs, unions enums etc, and have been told that I would need pointer support to do so. I have also been told that pointer support is scheduled for the near future. Does anyone know if pointer support has been released or when it will be released? The kind of functionality I'm looking for (maybe this doesn't require pointers, I don't know) is fairly similar to method implementation in javascript, but really what I'm looking for is something more like:
Code:
typedef struct{
int test=testing();
}e;

e main;//create an instance of "e"

void testing(int a){
       return a*3;
}

task main(){
main.test(4);
}


So basically I just want to use this kind of interface to better organize code into set categories, but objective functionality would also be nice. Also, if there is any better way to do this, I would love to know. Thank you.


Wed Dec 07, 2011 5:15 pm
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1343
Post Re: Release for pointers?
There are many tricks in implementing object oriented code with RobotC. It would be nice if RobotC supports pointers but at least it does support passing structures by reference. That's the closest thing to pointer support for now. However, if I understand your code correctly, you want to store a function pointer in a structure. That's currently not possible. Like I said, there are tricks to work around that to make the code still object oriented but not like the syntax you want it. Without pointer support, instead of <object>.<method>(params), you need to use the syntax like <method>(<object>, params). <object> is instantiation of a structure that got passed to the method by reference. I have implemented a robotics library with this kind of code construct. Let me know if you want to know more details about how it's done.


Wed Dec 07, 2011 7:00 pm
Profile
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3105
Location: Rotterdam, The Netherlands
Post Re: Release for pointers?
I would be interested.

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


Thu Dec 08, 2011 3:04 am
Profile WWW
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1343
Post Re: Release for pointers?
In C++, you define an object as a class. A class contains both data and methods. The implementation of a class in C++ is actually a data structure (vtable) that contains both the data fields and pointers to the methods. When you instantiate a C++ class object, a new vtable for the object structure is created with all the method pointers initialized to point to the method functions. So multiple objects of the same class are just copies of this vtable data structure so each one has their own private copy of the data fields but points to the same set of method functions.
I think the easiest way is to show you the difference between a C++ and RobotC implementations of one of our library modules. For example, in our library, we have an IR Seeker module. So in C++ syntax, if you were to define an IRSeeker class and define two IRSeeker sensors, you would do something like this in C++.
Code:
class IRSeeker
{
private:
    tSensors m_sensorID;
    float m_prevACDir;
    int m_acStrength[5];

public:
    // Constructor
    IRSeeker()
    {
    }

    // Destructor
    ~IRSeeker()
    {
    }

    void Init(tSensors sensorID)
    {
        m_sensorID = sensorID;
        m_prevACDir = 0.0;
    }

    float GetACDir(void)
    {
        float value;
        int dir;

        dir = HTIRS2readACDir(m_sensorID);
        value = (float)dir;
        if (dir == 0)
        {
            value = m_prevACDir;
        }
        else if (HTIRS2readAllACStrength(
                 m_sensorID,
                 m_acStrength[0],
                 m_acStrength[1],
                 m_acStrength[2],
                 m_acStrength[3],
                 m_acStrength[4]))
        {
            dir = (dir - 1)/2;
            if ((dir < 4) &&
                (m_acStrength[dir] != 0) &&
                (m_acStrength[dir + 1] != 0))
            {
                value += (float)(m_acStrength[dir + 1] -
                                 m_acStrength[dir])/
                                 max(m_acStrength[dir],
                                     m_acStrength[dir + 1]);
            }
        }
        m_prevACDir = value;
        return value;
    }
};

IRSeeker g_leftIRSeeker;
IRSeeker g_rightIRSeeker;

task main()
{
    g_leftIRSeeker.Init(leftIRSeekerSensor);
    g_rightIRSeeker.Init(rightIRSeekerSensor);
    while (true)
    {
        nxtDisplayTextLine(0, "leftIR=%5.1f", g_leftIRSeeker.GetACDir());
        nxtDisplayTextLine(1, "rightIR=%5.1f", g_rightIRSeeker.GetACDir());
        wait1Msec(100);
    }
}

In RobotC, we define objects as structures. There is no method (function) in the structure but the methods associated with the objects are global functions that take the instances of the structure as their parameter. So in essence, it looks like this:
Code:
typedef struct
{
    tSensors m_sensorID;
    float m_prevACDir;
    int m_acStrength[5];
} IRSeeker;

void IRSeekerInit(IRSeeker &irSeeker, tSensors sensorID)
{
    irSeeker.m_sensorID = sensorID;
    irSeeker.m_prevACDir = 0.0;
}

float IRSeekerGetACDir(IRSeeker &irSeeker)
{
    float value;
    int dir;

    dir = HTIRS2readACDir(irSeeker.m_sensorID);
    value = (float)dir;
    if (dir == 0)
    {
        value = irSeeker.m_prevACDir;
    }
    else if (HTIRS2readAllACStrength(
             irSeeker.m_sensorID,
             irSeeker.m_acStrength[0],
             irSeeker.m_acStrength[1],
             irSeeker.m_acStrength[2],
             irSeeker.m_acStrength[3],
             irSeeker.m_acStrength[4]))
    {
        dir = (dir - 1)/2;
        if ((dir < 4) &&
            (irSeeker.m_acStrength[dir] != 0) &&
            (irSeeker.m_acStrength[dir + 1] != 0))
        {
            value += (float)(irSeeker.m_acStrength[dir + 1] -
                             irSeeker.m_acStrength[dir])/
                             max(irSeeker.m_acStrength[dir],
                                 irSeeker.m_acStrength[dir + 1]);
        }
    }
    irSeeker.m_prevACDir = value;
    return value;
}

IRSeeker g_leftIRSeeker;
IRSeeker g_rightIRSeeker;

task main()
{
    IRSeekerInit(g_leftIRSeeker, leftIRSeekerSensor);
    IRSeekerInit(g_rightIRSeeker, rightIRSeekerSensor);
    while (true)
    {
        nxtDisplayTextLine(0, "leftIR=%5.1f", IRSeekerGetACDir(g_leftIRSeeker));
        nxtDisplayTextLine(1, "rightIR=%5.1f", IRSeekerGetACDir(g_rightIRSeeker));
        wait1Msec(100);
    }
}

If you want to see more examples, please refer to the source code of our library.
http://proj.titanrobotics.net/hg/Ftc/20 ... 11a/ftclib


Thu Dec 08, 2011 5:37 am
Profile
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3105
Location: Rotterdam, The Netherlands
Post Re: Release for pointers?
The thought of this did cross my mind a while back and Aswin sent me some code that did something like that, too but I think it would be a fairly monumental task to convert about 60 drivers this way. It's a very nice way of keeping the data together, for sure.

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


Thu Dec 08, 2011 7:55 am
Profile WWW
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1343
Post Re: Release for pointers?
The conversion should be fairly mechanical. Conversion of all 60 drivers at the same time may be momumental but you can probably do it incrementally a few at a time :). And of course, the real concern is for people who are using the drivers must also change their code correspondingly. But it's also fairly mechanical.


Last edited by MHTS on Thu Dec 08, 2011 1:38 pm, edited 1 time in total.



Thu Dec 08, 2011 1:23 pm
Profile
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3105
Location: Rotterdam, The Netherlands
Post Re: Release for pointers?
It would be for a 3.0 release of the driver, certainly not a minor update. I won't change it for this FTC season.

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


Thu Dec 08, 2011 1:37 pm
Profile WWW
Moderator
Moderator
User avatar

Joined: Tue Sep 14, 2010 9:19 pm
Posts: 496
Post Re: Release for pointers?
You could spit up the task if you'd like. I'm sure a number of people would be happy to help out. I know I'm willing to volunteer.

_________________
sudo rm -rf /


Thu Dec 08, 2011 1:43 pm
Profile
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3105
Location: Rotterdam, The Netherlands
Post Re: Release for pointers?
Sure, help is always welcome. I want to figure out how I want to do it first :) 80% of my time developing these drivers is spent thinking about how I want to implement it, 5% coding it up and 15% documenting it all.

I'll get back to you when I've got a couple of example drivers I'm happy with :)

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


Thu Dec 08, 2011 1:46 pm
Profile WWW
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1343
Post Re: Release for pointers?
I wouldn't mind helping out either. :)


Thu Dec 08, 2011 1:49 pm
Profile
Expert

Joined: Mon Oct 27, 2008 9:59 pm
Posts: 137
Post Re: Release for pointers?
If you want to hold off until the end of this FTC season, then there is a chance that new pointer functionality will actually be released in ROBOTC; the plan as I recall was for the 3.x life-cycle.

That said, feel free to add me to your list of volunteers! :bigthumb:


Tue Dec 13, 2011 9:43 am
Profile
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.