0

This code intermittently works. It's running on a small microcontroller. It will work fine even after restarting the processor, but if I change some part of the code, it breaks. This makes me think that it's some kind of pointer bug or memory corruption. What's happening is the coordinate, p_res.pos.x is sometimes read as 0 (the incorrect value) and 96 (the correct value) when it is passed to write_circle_outlined. y seems to be correct most of the time. If anyone can spot anything obviously wrong please point it out!

int demo_game()
{
    long int d;
    int x, y;
    struct WorldCamera p_viewer;
    struct Point3D_LLA p_subj;
    struct Point2D_CalcRes p_res;
    p_viewer.hfov = 27;
    p_viewer.vfov = 32;
    p_viewer.width = 192;
    p_viewer.height = 128;
    p_viewer.p.lat = 51.26f;
    p_viewer.p.lon = -1.0862f;
    p_viewer.p.alt = 100.0f;
    p_subj.lat = 51.20f;
    p_subj.lon = -1.0862f;
    p_subj.alt = 100.0f;
    while(1)
    {
        fill_buffer(draw_buffer_mask, 0x0000);
        fill_buffer(draw_buffer_level, 0xffff);
        compute_3d_transform(&p_viewer, &p_subj, &p_res, 10000.0f);
        x = p_res.pos.x;
        y = p_res.pos.y;
        write_circle_outlined(x, y, 1.0f / p_res.est_dist, 0, 0, 0, 1);
        p_viewer.p.lat -= 0.0001f; 
        //p_viewer.p.alt -= 0.00001f; 
        d = 20000;
        while(d--);
    }
    return 1;
}

The code for compute_3d_transform is:

void compute_3d_transform(struct WorldCamera *p_viewer, struct Point3D_LLA *p_subj, struct Point2D_CalcRes *res, float cliph)
{
    // Estimate the distance to the waypoint. This isn't intended to replace
    // proper lat/lon distance algorithms, but provides a general indication
    // of how far away our subject is from the camera. It works accurately for 
    // short distances of less than 1km, but doesn't give distances in any
    // meaningful unit (lat/lon distance?)
    res->est_dist = hypot2(p_viewer->p.lat - p_subj->lat, p_viewer->p.lon - p_subj->lon);
    // Save precious cycles if outside of visible world.
    if(res->est_dist > cliph)
        goto quick_exit;
    // Compute the horizontal angle to the point. 
    // atan2(y,x) so atan2(lon,lat) and not atan2(lat,lon)!
    res->h_angle = RAD2DEG(angle_dist(atan2(p_viewer->p.lon - p_subj->lon, p_viewer->p.lat - p_subj->lat), p_viewer->yaw));
    res->small_dist = res->est_dist * 0.0025f; // by trial and error this works well.
    // Using the estimated distance and altitude delta we can calculate
    // the vertical angle.
    res->v_angle = RAD2DEG(atan2(p_viewer->p.alt - p_subj->alt, res->est_dist));
    // Normalize the results to fit in the field of view of the camera if
    // the point is visible. If they are outside of (0,hfov] or (0,vfov]
    // then the point is not visible.
    res->h_angle += p_viewer->hfov / 2;
    res->v_angle += p_viewer->vfov / 2;
    // Set flags.
    if(res->h_angle < 0 || res->h_angle > p_viewer->hfov)
        res->flags |= X_OVER;
    if(res->v_angle < 0 || res->v_angle > p_viewer->vfov)
        res->flags |= Y_OVER;
    res->pos.x = (res->h_angle / p_viewer->hfov) * p_viewer->width;
    res->pos.y = (res->v_angle / p_viewer->vfov) * p_viewer->height;
    return;
quick_exit:
    res->flags |= X_OVER | Y_OVER;
    return;
}

Structure for the results:

typedef struct Point2D_Pixel { unsigned int x, y; };

// Structure for storing calculated results (from camera transforms.)
typedef struct Point2D_CalcRes
{
    struct Point2D_Pixel pos;
    float h_angle, v_angle, est_dist, small_dist;
    int flags;
};

The code is part of an open source project of mine so it's okay to post a lot of code here.

11
  • d = 20000; while(d--); - I hope this is just a little test?
    – EboMike
    Nov 29, 2010 at 22:14
  • 1
    As for "sometimes works" - are you properly initializing ALL variables?
    – EboMike
    Nov 29, 2010 at 22:14
  • Which part of the code do you change, which causes it to fail? Nov 29, 2010 at 22:17
  • 7
    Could this statement be any more vague? "if I change some part of the code, it breaks." What part of the code do you change? What do you change it to? How does it break when you change it?
    – abelenky
    Nov 29, 2010 at 22:18
  • What are the 'flags' used for? Shouldn't the calling code do something differently if they are set? Whenever 'res->est_dist > cliph', the calculation for the position gets skipped, so it shouldn't be surprising if it contains garbage values. Nov 29, 2010 at 22:19

4 Answers 4

4

I see some of your calculation depends on p_viewer->yaw, but I do not see any intialization for p_viewer->yaw. Is this your problem?

1
  • Something I didn't even think of. I wasn't even thinking of uninitialize variables, I was suspecting a faulty micro! Seems to be working now. I'll mark this as the accepted answer if the problem appears to be fixed. Thanks from a newbie! :)
    – Thomas O
    Nov 29, 2010 at 22:34
2

A couple of things that seem sketchy:

  • You can return from compute_3d_transform without setting many of the fields in p_res/res but the caller never checks for this situation.

  • You consistently read from res->flags without initializing it first.

2
  • When you say read from res->flags what do you mean? I am only setting bits.
    – Thomas O
    Nov 29, 2010 at 22:34
  • 2
    You are setting bits without first clearing it. foo |= bar is essentially the same as foo = foo | bar, hence you are effectively reading the uninitialized value. You might want to add a res->flags = 0; line near the top of compute_3d_transform. Nov 29, 2010 at 22:40
1

Whenever the output differs, it possibly means some value is not initialized and the outcome depends on the garbage value present in a variable. Keeping that in mind, I looked for uninitialized variables. the structure p_res is not initialized.

if(res->est_dist > cliph)
        goto quick_exit;

that means if condition may turn out to be true or false depending on what garbage value is stored in res->est_dist. When if condition turns out to true, it goes straight to quick_exit label and doesn't update p_res.pos.x. If condition turned out to be false then its updated.

1

When I used to program C, I would use a divide and conquer debugging technique for this kind of problem to try to isolate the offending operation (paying attention to whether the symptoms change as debugging code is added, which is indicative of dangling pointer type bugs).

Essentially, start with the first line where the value is known to be good (and prove that it is consistently good at that line). Then identify where is it known to be bad. Then approx. halfway between the two points insert a test to see if it's bad. If not, then insert a test halfway between the mid-point and the known bad location, if it is bad then insert a test halfway between the mid-point and the known good location, and so on.

If the line identified is itself a function call, this process can be repeated in that called function, and so on.

When using this kind of approach, it's important to minimize the amount of added code and the artificial "noise", which can create timing changes.

Use this if you don't have (or can't use) an interactive debugger, or if the problem does not manifest when using one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.