Darwinbots Forum

Code center => Bugs and fixes => Solved Bugs => Topic started by: PurpleYouko on October 27, 2005, 03:12:25 PM

Title: Aim getting WAAAAAY too big
Post by: PurpleYouko on October 27, 2005, 03:12:25 PM
I found a new one.

A really frickin stupid one too.

It should never exist. Whoever programmed this bit of code should change his name to Richard Head.

It was quite possibly me that did it  :redface:

Here is the code

Code: [Select]
 If Abs(.mem(aimsx) > 1256) Then .mem(aimsx) = 1256 * Sgn(.mem(aimsx)) 'new crash fix?
    If Abs(.mem(aimdx) > 1256) Then .mem(aimdx) = 1256 * Sgn(.mem(aimdx))
    If .mem(setaim) <> 32000 Then
      While .mem(setaim) > 1256
        .mem(setaim) = .mem(setaim) - 1256
      Wend
      While .mem(setaim) < 0
        .mem(setaim) = .mem(setaim) + 1256
      Wend
      .aim = .mem(setaim) / 200
      .mem(setaim) = 32000
    End If
    .aim = .aim + (.mem(aimsx) - .mem(aimdx)) / 200
    If .aim > 2 * pi Then .aim = .aim - 2 * pi
    If .aim < 0 Then .aim = 2 * pi + .aim
    .aimx = Cos(.aim)
    .aimy = Sin(.aim)
You can find it in Updvars2 in the robots module.

The error occurs on line
Code: [Select]
.aim = .aim + (.mem(aimsx) - .mem(aimdx)) / 200
Holding the mouse over ".mem(aimsx)" gives -32000
Holding the mouse over ".mem(aimdx)" gives 1256
This is obviously going to push .aim beyond the 32768 limit and give us an overflow.

I thought I had fixed this ages ago by limiting .aim to a magnitude of 1256. The code to do it is right up there in this chunk of code.

See if you can spot the stupid ass mistake in it.
Title: Aim getting WAAAAAY too big
Post by: Ulciscor on October 27, 2005, 03:17:42 PM
Code: [Select]
mem(aimsx) = 1256 * Sgn(.mem(aimsx))
Does this do what I think it does?

If the absolute value of .aim is > 1256 then multiply it by the sign? Won't you always end up with the absolute value of what .aim was to begin with?
Title: Aim getting WAAAAAY too big
Post by: PurpleYouko on October 27, 2005, 03:20:19 PM
No you will end up with a value of 1256 multiplied by the original sign of the value.

Example

-2598 will become -1256
6589 will become 1256

All it does is to cap the value without changing the sign.

[hint]Look at the condition before this[/hint]
Title: Aim getting WAAAAAY too big
Post by: Numsgil on October 27, 2005, 03:27:38 PM
I don't think I touched the aiming controls in 2.37.4 (totally code raped them in 2.4 however ;))

DL 2.4 and find where I handle turning (I thinks its called SetAImFUnc or something like that, I'd have to look to be sure) and see how I did it, while we're cracking down on bugs ;)
Title: Aim getting WAAAAAY too big
Post by: PurpleYouko on October 27, 2005, 03:31:03 PM
I have already fixed this BTW. I just want to see if anybody else can see the glaringly obvious cock up before I tell them what it is.

I am pretty sure this is code that I added myself so I'm the one who did the dirty deed.
Title: Aim getting WAAAAAY too big
Post by: Ulciscor on October 27, 2005, 03:40:01 PM
Omg! Might have just realised... it's the abs value of the condition, not the abs value of .aim used in the condition!

Please say I'm right, or I'll be well embarassed.   :redface:
Title: Aim getting WAAAAAY too big
Post by: Numsgil on October 27, 2005, 03:43:53 PM
That's what I got |)  (<-- LaForge from star trek smiely ;))
Title: Aim getting WAAAAAY too big
Post by: PurpleYouko on October 27, 2005, 03:46:47 PM
HEHEHE

Partly

You ARE right that it is the abs value of the condition that is used to determine the value of .aimsx and .aimdx.

That isn't the error though.

Look very carefully at the condition. cough "brackets" cough
Title: Aim getting WAAAAAY too big
Post by: Ulciscor on October 27, 2005, 03:48:02 PM
I'm guessing you need

Code: [Select]
(Abs(.mem(aimsx)) > 1256)
instead?
Title: Aim getting WAAAAAY too big
Post by: PurpleYouko on October 27, 2005, 03:48:18 PM
here is a close up  :D

[span style=\'font-size:14pt;line-height:100%\']If Abs(.mem(aimsx) > 1256) Then [/span]
Title: Aim getting WAAAAAY too big
Post by: Numsgil on October 27, 2005, 03:48:35 PM
Nope you lost me ;)
Title: Aim getting WAAAAAY too big
Post by: PurpleYouko on October 27, 2005, 03:48:55 PM
Quote
I'm guessing you need

Code: [Select]
(Abs(.mem(aimsx)) > 1256)
instead?
Nope. Sorry
Title: Aim getting WAAAAAY too big
Post by: PurpleYouko on October 27, 2005, 03:49:37 PM
Quote
Nope you lost me ;)
Oh come on. It's obvious. What is that condition going to do?
Title: Aim getting WAAAAAY too big
Post by: PurpleYouko on October 27, 2005, 03:50:45 PM
look at what is INSIDE the ABS() brackets.
Title: Aim getting WAAAAAY too big
Post by: PurpleYouko on October 27, 2005, 03:51:15 PM
...that shouldn't be.....
Title: Aim getting WAAAAAY too big
Post by: Numsgil on October 27, 2005, 03:51:27 PM
You tell me the ansewr and I'll let you know if I saw it or not  :lol:
Title: Aim getting WAAAAAY too big
Post by: Ulciscor on October 27, 2005, 03:51:32 PM
How can you take the absolute value of a boolean?!
Title: Aim getting WAAAAAY too big
Post by: PurpleYouko on October 27, 2005, 03:51:47 PM
:P
Title: Aim getting WAAAAAY too big
Post by: PurpleYouko on October 27, 2005, 03:52:10 PM
What do I have to do? Hold your hand?
Title: Aim getting WAAAAAY too big
Post by: Numsgil on October 27, 2005, 03:52:32 PM
The only blaring mistake I see is that it should be If Abs(.mem(aimsx)) > 1256 Then
Title: Aim getting WAAAAAY too big
Post by: Ulciscor on October 27, 2005, 03:53:12 PM
Yes! I agree!
Title: Aim getting WAAAAAY too big
Post by: PurpleYouko on October 27, 2005, 03:54:34 PM
WOOHOOO  :clap:  :party:

You got it.

The way it was written, it didn't matter how big .aimsx got. The function always returned 0 as the condition. I.e. it NEVER fulfilled the condition and limited .aimsx to 1256.
Title: Aim getting WAAAAAY too big
Post by: Numsgil on October 27, 2005, 03:55:18 PM
Weren't we saying that way back at the beginning though?  I thought there was something else I wasn't seeing.
Title: Aim getting WAAAAAY too big
Post by: PurpleYouko on October 27, 2005, 03:56:21 PM
So here is the crash fix for those of out there who give a crap.

Code: [Select]
If Abs(.mem(aimsx) > 1256) Then .mem(aimsx) = 1256 * Sgn(.mem(aimsx)) 'new crash fix?
   If Abs(.mem(aimdx) > 1256) Then .mem(aimdx) = 1256 * Sgn(.mem(aimdx))

becomes
Code: [Select]
If Abs(.mem(aimsx)) > 1256 Then .mem(aimsx) = 1256 * Sgn(.mem(aimsx)) 'new crash fix?
   If Abs(.mem(aimdx)) > 1256 Then .mem(aimdx) = 1256 * Sgn(.mem(aimdx))
Title: Aim getting WAAAAAY too big
Post by: Ulciscor on October 27, 2005, 03:57:06 PM
Quote
I'm guessing you need

Code: [Select]
(Abs(.mem(aimsx)) > 1256)
instead?
is different from

Code: [Select]
If Abs(.mem(aimsx)) > 1256 Then
How?
Title: Aim getting WAAAAAY too big
Post by: Numsgil on October 27, 2005, 03:59:49 PM
An extra set of parenthesese  :lol:
Title: Aim getting WAAAAAY too big
Post by: Ulciscor on October 27, 2005, 04:00:21 PM
Ah they can make all the difference!  :ph43r:
Title: Aim getting WAAAAAY too big
Post by: PurpleYouko on October 27, 2005, 04:00:43 PM
Sorry Ulc. I didn't notice you had moved a bracket. All I noticed was the extra one before the ABS.

The way you wrote it should work OK but the outer bracket set is just superfluous.
Title: Aim getting WAAAAAY too big
Post by: Numsgil on October 27, 2005, 04:04:24 PM
Ah, isn't coding wonderful ;)