1. Download our Official Android App: Forums for Android!

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

Discussion in 'Android Development' started by thecause17, Oct 13, 2017.

  1. thecause17

    thecause17 Newbie
    Thread Starter
    Rank:
    None
    Points:
    26
    Posts:
    12
    Joined:
    Oct 9, 2017

    Oct 9, 2017
    12
    1
    26
    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 (Text):
    1.  
    2. //Change from radians to degrees, inverting throttle
    3. double steering = mValuesOrientation[1] * (180/Math.PI);
    4. double throttle = mValuesOrientation[2] * (-180/Math.PI);
    5.  
    6. //Formatting to one decimal place
    7. DecimalFormat precision = new DecimalFormat("0.0");
    8.  
    9. final CharSequence steerpos;
    10. final CharSequence speeddemand;
    11. double speedpercent = 0;
    12.  
    13. //Specify throttle response
    14. if(throttle <= 45 && throttle >= 20) {
    15.     //Range speedpercent from 1% to 100 %
    16. }
    17. else if(throttle >= 80 && throttle <= 120) {
    18.     //Range speedpercent from -1% to -50%
    19. }
    20.  
    21. else {
    22.     speedpercent = 0;
    23. }
    24.  
    25. //Send to text boxes
    26. steerpos = "Steering: " + precision.format(steering);
    27. txt1.setText(steerpos);
    28. speeddemand = "Throttle: " + precision.format(speedpercent);
    29. txt2.setText(speeddemand);
     

    Advertisement

  2. LV426

    LV426 I say we take off and nuke this place from orbit
    Moderator
    Rank:
     #11
    Points:
    1,988
    Posts:
    7,871
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    7,871
    11,504
    1,988
    Male
    Software developer
    South West of England
    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 (Text):
    1.  
    2.  speedpercent = (throttle - 21) * 4.3478
    3.  
     
  3. LV426

    LV426 I say we take off and nuke this place from orbit
    Moderator
    Rank:
     #11
    Points:
    1,988
    Posts:
    7,871
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    7,871
    11,504
    1,988
    Male
    Software developer
    South West of England
    Correction. I think, given your range, the scaling factor is 4.1666, and the code is

    Code (Text):
    1.  
    2. speedpercent = (throttle - 20) * 4.1666
    3.  
     
    thecause17 likes this.
  4. thecause17

    thecause17 Newbie
    Thread Starter
    Rank:
    None
    Points:
    26
    Posts:
    12
    Joined:
    Oct 9, 2017

    Oct 9, 2017
    12
    1
    26
    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 (Text):
    1.  
    2. speedpercent = (throttle - 45)*(-4);
    3.  
     
  5. LV426

    LV426 I say we take off and nuke this place from orbit
    Moderator
    Rank:
     #11
    Points:
    1,988
    Posts:
    7,871
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    7,871
    11,504
    1,988
    Male
    Software developer
    South West of England
    Ok yes those numbers seem to work better.
     
  6. thecause17

    thecause17 Newbie
    Thread Starter
    Rank:
    None
    Points:
    26
    Posts:
    12
    Joined:
    Oct 9, 2017

    Oct 9, 2017
    12
    1
    26
    Thanks for you help, it's working well. For reference, the other range uses the following code:

    Code (Text):
    1.  
    2. speedpercent = -((throttle - 80)*1.25);
    3.  
    Negative because I want negative speed demand (reverse).
     

Share This Page

Loading...