Search code examples
cdebuggingroboticsnxt

Return Functions Don't work. Using pythagorean Theorem


#pragma config(Motor,  port1,           RightsideB,    tmotorVex393_HBridge, openLoop)
#pragma config(Motor,  port2,           RightsideF,    tmotorVex393_MC29, openLoop)
#pragma config(Motor,  port9,           LefttsideF,    tmotorVex393_MC29, openLoop)
#pragma config(Motor,  port10,          LeftsideB,     tmotorVex393_HBridge, openLoop)
//*!!Code automatically generated by 'ROBOTC' configuration wizard             !!*//

float xdiff, ydiff, firstpart, secondpart, firstpart2, secondpart2, total, lengthformula;
float getdistance(int x1, int x2,int y1, int y2){
    return x1;
    return x2;
    return y1;
    return y2;

    xdiff = x2 - x1;
    ydiff = y2 - y1;

    firstpart = (xdiff);
    return firstpart;
    secondpart = (ydiff);
    firstpart2 = pow(xdiff,2);
    secondpart2 = pow(ydiff, 2);

    total = firstpart2 + secondpart2;
    lengthformula = sqrt(total);
    return lengthformula;
}

task main()
{
    getdistance(0, 4, 0, 1);
    while (true){
        motor[RightsideB]= vexRT[Ch2];
        motor[RightsideF] = vexRT[Ch2];
        motor[LeftsideB]= vexRT[Ch3]; 
        motor[LefttsideF]= vexRT[Ch3];

    }

}

This program will run without errors, but when i run it and open debugger, the variables dont work. I have programmed in smallbasic and i know this works on it. im converting it to robot-c


Solution

  • Get rid of all the return statements in getdistance, except return lengthformula at the end. return causes the function to stop running, and immediately return the value you specify, so the rest of the function never runs and the distance never gets calculated.

    In main(), you need to assign the result to a variable:

    task main() {
        float distance = getdistance(0, 4, 0, 1);
        ...
    }
    

    Then you can use distance for whatever you need.