• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Assigning one variable's range to create the range of another variable

I'm not sure if the title of the thread makes a lot of sense, so let me explain what I'm trying to do. Currently I'm reading sensors from the phone and displaying them on the app. I will also be sending these values over a network later. I'm looking at the phone's orientation, but I only care about two of the axes.

I've converted one to degrees and it can stay that way. The other one, I want to use as a speed demand and want to display and transmit it as a percentage of a range which I have predetermined. This range also has constraints where certain angle ranges need to provide zero demand. For example, from 45 degrees to 20 degrees, demand will range from 0-100%, but from 45-80 degrees needs to stay 0. There are other ranges as well. I don't know how to make this happen in code. I can see using a "if" and "else if" statements to break up the blocks, and then specifying in each statement what I need, but inside of those I don't know how to assign a percentage value to a degree value for a range like this. Here's the section of code that pertains to this that I've come up with so far. The if and first else if statement are where I'm not sure what to do. Thanks.

Code:
//Change from radians to degrees, inverting throttle
double steering = mValuesOrientation[1] * (180/Math.PI);
double throttle = mValuesOrientation[2] * (-180/Math.PI);

//Formatting to one decimal place
DecimalFormat precision = new DecimalFormat("0.0");

final CharSequence steerpos;
final CharSequence speeddemand;
double speedpercent = 0;

//Specify throttle response
if(throttle <= 45 && throttle >= 20) {
    //Range speedpercent from 1% to 100 %
}
else if(throttle >= 80 && throttle <= 120) {
    //Range speedpercent from -1% to -50%
}

else {
    speedpercent = 0;
}

//Send to text boxes
steerpos = "Steering: " + precision.format(steering);
txt1.setText(steerpos);
speeddemand = "Throttle: " + precision.format(speedpercent);
txt2.setText(speeddemand);
 
For the 20 to 45 degree range you need a scaling factor. And according to my calculations, the value is approx 4.3478.
So I'd use this algorithm -

Code:
 speedpercent = (throttle - 21) * 4.3478
 
Correction. I think, given your range, the scaling factor is 4.1666, and the code is

Code:
speedpercent = (throttle - 20) * 4.1666
 
Correction. I think, given your range, the scaling factor is 4.1666, and the code is

Code:
speedpercent = (throttle - 20) * 4.1666

Why wouldn't it just be a scaling factor of 4?

Yes, I think a scale factor will work, except one thing. You have backwards the top and bottoms of my range. In my case, the 20 degrees is supposed to be 100 % and 45 is 0....with your algorithm, 20 degrees is 0 and 45 ends up being 104.165. If the scale factor is 4, it would be correct, though still backwards. Wouldn't it be more like:

Code:
speedpercent = (throttle - 45)*(-4);
 
Ok yes those numbers seem to work better.

Thanks for you help, it's working well. For reference, the other range uses the following code:

Code:
speedpercent = -((throttle - 80)*1.25);

Negative because I want negative speed demand (reverse).
 
Back
Top Bottom