Compass: from 359 to 0 degrees - compass-geolocation

I am trying to move a robot using a compass. We use the compass to make the robot move in straight line, it uses 2 wheels and they move a bit different.
So we set a value between 0 and 359 as direction, and then check the current direction, calculate the error and fix it. Like error = current_direction - actual direction.
The problem is that if for example our init direction is 90 degrees and our robot is at 45, the error will be 45 and it will fix it. If it is 0, the error will be 90 and it will fix it. The problem is that if it moves a bit more than 0 and it goes for example to 359, the error will be -269 so instead of moving 90 in one direction it will move -269 in the other.
I use the sign of the error to decide which wheel to move to fix the direction.
any idea how to fix it?

if (error > 180) {
error -= 360;
}
if (error < -180) {
error += 360;
}

if your error is greater than 180°, you should rack it from 360 and invert the sign. Doing so, you can be sure your robot will always move in the shortest direction.

If your error is > than 180 degree simply switch your correction algorithm to calculate the correction by moving in the opposite direction. A simple if-else statement should do.

I don't know much about NXT and Mindstorm, but basically it is a common problem in circular motions. You could simply use two different coordinate systems and translate between each other, thats the most elegant way. Otherwise you could subtract 360 from your error, if the sign is negative, but that's a hack and not an elegant way to solve the problem ;-)

Related

Determine the direction an object is actually moving

In unity I am trying to compare the players actual direction with the direction they are facing and wish to move in but having major issues trying to find the actual movement direction.
I can determine the facing direction very easily with:
wishDir = transform.localEulerAngles;
But I cannot figure out how to get the objects movement direction so that I can compare. I have tried:
transform.InverseTransformDirection(rb.velocity);
I would expect this to be equal to 0,90,0 when I move to the right however it is equal to 0,0,0 (although jumps when there is acceleration).
How can I determine the direction an object is moving in?
I can determine the facing direction very easily with:
wishDir = transform.localEulerAngles;
This is already quite odd to me. localEulerAngles is a rotation in Euler space notation in degrees per axis x,y,z .. this is no "direction".
Usually if you want the direction you are looking in you would rather use transform.forward
wishDir = transform.forward;
And then
transform.InverseTransformDirection(rb.velocity);
should indeed return the direction in local space.
Note that the Debug.Log beautifies (rounds) this value to make it more human readable. If you want the exact values you could try and log e.g.
var relative = transform.InverseTransformDirection(rb.velocity);
Debug.Log(relative.ToString(F4));
which should print the values always with 4 digits after the decimal point.

How to find direction (in radians or degrees) of a physics body

I have a physics body that gets hit and rotates a lot. I want to limit the amount it can rotate (only 45 degrees in each direction or something) and I wanted to try to do that by applying torque if the direction goes past a certain point. I'm ok with the sprite wobbling back and forth (it's a bird).
I'm open to other suggestions on how to limit the rotation as well.
Thanks in advance!
Edit: I've already tried using zRotation (I printed it out and it was always 0.) I also tried using the vector but that gave me the direction the sprite was going.

I can't get the right rotation of my transform in unity 3D

Hi everyone I have some little problem in Unity 3D. I am trying to do some walking simulation I have character and I have joints while she was walking I want to get the rotation of y. But when I try it I get some results like (transform of rotation in unity is -60 for example the value I get in program 0.059871 something like that how can I read the -60 the right value?)
Maybe you take the wrong properties, the Quaternion.y is a value from 0 to 1 (CMIIW)
but if you want to get you rotation in degree you can use Vector3.y by taking
var y = gameObject.transform.localEulerAngles.y;
Also, please attach your code before asking some code related issue

Unreal Engine: Constrain a pawn's rotation between two angles

I'm working on a short Unreal Engine 4.9 blueprint for a friend, but I am not familiar with the unreal engine at all, and I'm about to pull my hair out. I've been searching online for about 2 hours, and I can't get it.
What I'm trying to do is get the roll of a object, derived from the pawn class, and lock it in between two angles. In pseudocode, do this:
if MyObect.Roll < -50,
MyObject.Roll = -50;
if MyObect.Roll > 50,
MyObject.Roll = 50;
Any sort of help or pointing in the right direction would be a huge help. I've seen some post using a player camera manager, and no luck from that thus far. Thanks in advance.
First thing to do in such a situation is print your values, e.g. using UE_LOG. You'll notice that your values are often between 0 and 360 instead of what you might expect yourself (-180 to 180).
So, you'll need to 'normalize' your angles first, to get them to be between -180 and 180. That way you can use the code you posted above :)
if (MyObject.Roll < -180.0f) MyObject.Roll += 360.0f;
else if (MyObject.Roll > 180.0f) MyObject.Roll -= 360.0f;
Hope that helps!

How to get a ball/sphere to stop

I got a field in unity3d that has some depressions in it (like small holes). The field's slope always leads towards the nearest depression.
A sphere is dropped at random somewhere in the field, rolls around a bit until it stops in one of the depressions.
The problem is, this is taking too long. It could roll around for 5-10 seconds until it stops. I'd like to stop faster.
Any ideas how I can achieve this?
Edit: The main issue is when the ball is next to the depression, but it has speed that is 90 degrees from the hole, then it starts going in circles and takes a while to stop.
Ok, after getting some advice in the comments, and experimenting, this is the way I solved it:
Apply a small measure of strength towards the depression
If the current velocity is more than 30 degrees away from the center of the depression, slow the ball (apply strength in the opposite direction of the velocity)
IF the ball gets very near the center of the depression, stop it and place it in the center
Thanks for all the tips. If anyone comes up with a better way, I'm still open to suggestions.