Convert ABS_X and ABS_Y into meaningful values - raspberry-pi

I am new to mapping a gaming controller and I am attempting to make sense of the ABX_X and ABS_Y values that return from the joystick. My goal is to take those values and turn them into "move forward", "move back", "turn" etc. I cannot seem to figure out what the formula is for converting the values that come in to actions. Below is my ABSInfo data (from Python InputDevice) and also some sample values. Any help to steer me in the correct direction would be very helpful.
ABS Info Capabilities
('EV_ABS', 3): [(('ABS_X', 0), AbsInfo(value=32768, min=0, max=65535, fuzz=0, flat=4095, resolution=0)), (('ABS_Y', 1), AbsInfo(value=32768, min=0, max=65535, fuzz=0, flat=4095, resolution=0))
Sample Values For Y
Y VALUE: 3101
Y VALUE: 5241
Y VALUE: 6931
Y VALUE: 9597
Y VALUE: 12059
Y VALUE: 14990
Y VALUE: 17467
Y VALUE: 19878
Y VALUE: 32768
Sample values for X:
X VALUE: 23596
X VALUE: 14042
X VALUE: 0
X VALUE: 818
X VALUE: 5943
X VALUE: 17212
X VALUE: 32768

evdev provides axis values exactly as they are received from the device, unscaled. The input_absinfo struct (AbsInfo in python) gives the bounds information to let you normalize joystick inputs.
(('ABS_X', 0), AbsInfo(value=32768, min=0, max=65535, fuzz=0, flat=4095, resolution=0)),
Supposing you wanted your joystick to give a value of -1 at its minimum and +1 at its maximum, you can normalize ABS_X using the min and max values in AbsInfo:
normalizedValue = 2.0 * (value - min) / (max - min) - 1.0

Related

Extrapolate Animation Curve (Endless game balancing curve)

I need a curve editor to make balancing of endless game then I tried to use AnimationCurve.
I need to set a curve to a certain range ex. [0;1] and if I want a value over 1, the result of the Evaluation have to extrapolate the curve. I want to be able to compute Y from X and X from Y.
The problem is AnimationCurve have only 3 WrapMode (Clamp, PingPong, Loop).
How to extrapolate an AnimationCurve ?
Is there a better tool to make curve with extrapolation (post and pre curve) ?
For real extrapolation I think you'd have to implement your own system based on Bézier mathematics. Me at least am not aware of unity providing it out of the box.
A work around for it could be to just define values beyond the 0 to 1 range to cover the extents, animation curves do allow this, I don't think there are to many issues with that.
Another solution, to stay in 0 to 1 range still but achieve the same effect, would be to model the curve from 0 to 1 so that it would cover extreme values within that range and remap the time for curve evaluation given by the object to a 0 to 1 range.
E.g.:
// define range extents
float rangeMin = -5f, rangeMax = 5f;
var range = 10f;
// range could be calculated at runtime if necessary:
// [to] (higher value) - [from] (lower value) = [range]
// 5f - -5f = 10f
var timeRaw = 0; // variable provided value
var time01 = (timeRaw - rangeMin) / range;
// reult by timeRaw = 0: (0 - -5) / 10 = 0.5
// reult by timeRaw = 5: (5 - -5) / 10 = 1.0
// reult by timeRaw = -5: (-5 - -5) / 10 = 0.0
Combining both solutions allow you to cover even more extreme values.

How do I randomize a position negatively?

So I have to randomize an x position. But for some reason the middle is 0 and therefore I need to do negative max x and positive max x. It's a little hard to explain so here is the code:
let height = UInt32(self.view!.frame.height)
let nHeight = ((height - height) - height)
let randomXPosition = Int(arc4random_uniform(UInt32(nHeight)) + height)
This doesn't give me any errors but when I run it crashes. I need a way to randomize the value for negative max X and positive max X so that the object is randomized in the screen.
If you need to generate a random number between -x and x then just create a random number between 0 and 2x and subtract x. arc4random_uniform(x * 2) - x.

Recalculate values on different scale in chart

I want to display some values inside a chart-like tool based on pixels.
Problem is that the left xAxis has a max scale of 200 pixel. Inside that pixel square i want to display different altitude values that can range from 200m-1500m or 324m-724m or anything else.
So i need to recalculate the orignal values by a factor to display them inside this chart. Haven't find the right solution yet. Any hints ?
You have range of Y-coordinates 0..YMax (200 for your case) and data range Data_Low..Data_High (find min and max values).
To map data range to axis range, use linear formula:
Y = (Value - Data_Low) * YMax / (Data_High - Data_Low)
If axis starts from YMin, use
Y = YMin + (Value - Data_Low) * (YMax - YMin) / (Data_High - Data_Low)

How to floor a floating point number? from 0.999 to 0.99

I want to truncate any value that appears after two decimal places in Matlab/Octave.
I do not want the value 1. I instead want the value to be 0.99 after "rounding/flooring".
Please use only built-in functions to accomplish this task.
Scale up the number, apply rounding, scale the result down by the same value:
x = 0.999;
y = floor (100 * x) / 100;

Graph with logarythimic y axis in Swift

My goal is to create a graph similar to a picture below. I actually managed to implement it with combination of a magic numbers and set scale (0.001 - 1000). So to summarize I am looking for a formula that will calculate right position to plot lines on logarithmic y scale for range of predefined values.
Y axis: logarithmic scale
X axis: Integers
Any help will be welcome!
I solved it thanks to help of #DietrichEpp. Here is the function that calculates Y coordinates given:
screenY0 - min point on y axis
screenY1 - max point on y axis
dataY0 - value responding to the top of the scale
dataY1 - value responding to the bottom of the scale
func convert(data: Double, screenY0:CGFloat, screenY1:CGFloat, dataY0:Double, dataY1:CGFloat) ->CGFloat{
return screenY0 + (log(CGFloat(data)) - log(CGFloat(dataY0))) / (log(CGFloat(dataY1)) - log(CGFloat(dataY0))) * (screenY1 - screenY0)
}