Jump to content

Please help me fix weird bug in my program


Recommended Posts

Okay. I'm making a positioning system for my robot. I'll kindof skip on the precise need for it but its very importand that I can get it to work, not just to me but for about 30 other highschool students on the team with me. I'm trying to get some coordinants off of a picture. The error is that when you select a any red or blue starting position and press set, it will make a point there. This is where the robot thinks it is supposed to start driving. The coordinant system has to change relative to this starting position so that it can take into account the different ways the robot must face depending on where it's starting. When I try to change the coordinants outputted to the inputbox at the bottom, well... just plain weird things happen. The numbers seem random and like they just aren't working. Truely strange things are happening and I don't know why or have any idea how to fix it. The coordinants listed in the "coordinants" Are what should be listed in the inputbox but they defenitely aren't so does anybody know why? here's my code.

#include <GUIConstants.au3>
#include <Math.au3>

Opt("CaretCoordMode", 0)

DirCreate ( "C:\PS" )
FileInstall("C:\Documents and Settings\HP_Administrator\Desktop\PS Beta\dot.bmp","C:\PS\dot.bmp",1)
FileInstall("C:\Documents and Settings\HP_Administrator\Desktop\PS Beta\field1.bmp","C:\PS\field1.bmp",1)


;<===Variables===>
$gui = GUICreate("test")
$first0 = ""
$first1 = ""
$x22 = ""
$y22 = ""
$x11 = ""
$y11 = ""
$maxControls = 50000
Global $controlIDs[$maxControls]
Global $nextControl = 0
$b_1 = ""
$control = 4141
 $count = 0
$text = ""
$x_old = ""
$y_old = ""
$x_location = "0"
$y_location = "0"
$Gear_x = 750
$Gear_y = 70
$gear = "Mid"
$Start_x = 890
$Start_y = 15
$blue = 4123
$red = 4126
$auto = 0
$check_x = 740
$check_y = 640
$code = "int waypoints[0][3]=" & @CRLF & "{/*   x   y   g   */"
$current = $control + 1
$current1 = ""
$line = ""
$output = ""
$head = "int waypoints[0][3]=" & @CRLF & "{/*   x   y   g   */"
$num = 0
$num1 = 10000
Dim $x_move1[$Num1]
Dim $y_move1[$Num1]
Dim $gear_num1[$Num1]
$mode = 0
$x_move2 = ""
$y_move2 = ""
$x_coord = ""
$y_coord = ""

;<===Hot Key===>
;HotKeySet( "z", "everything")

;<===GUI===>
GUICreate("   WÌÑÑÒVÅTÍÓÑ  P§  ß   ",1024,768,0,0)
$reset = GUICtrlCreateButton("Reset",50,20,50,30)
$save = GUICtrlCreateButton("Save",250,20,50,30)
$exit = GUICtrlCreateButton("Exit",450,20,50,30)
$input = GUICtrlCreateInput($text,50,400,650,350,"0x00201004")
GUICtrlCreateGroup("Coordinants",750,240,240,50)
GUICtrlCreateLabel("X:",760,260,10,20)
$x_pos = GUICtrlCreateInput($x_location,780,260,30,20)
GUICtrlCreateLabel("Y:",830,260,10,20)
$y_pos = GUICtrlCreateInput($y_location,850,260,30,20)
$point = GUICtrlCreateButton("Make Point",900,255,70,30)
GUICtrlCreateGroup("Gear",$Gear_x,$Gear_y,100,100)
$high = GUICtrlCreateRadio("High",$Gear_x + 20,$Gear_y + 20,60,20)
$mid = GUICtrlCreateRadio("Mid",$Gear_x + 20,$Gear_y + 45,60,20)
$low = GUICtrlCreateRadio("Low",$Gear_x + 20,$Gear_y + 70,60,20)
GUICtrlSetState($mid, $GUI_CHECKED)
GUICtrlCreateGroup("Starting Position",$Start_x,$Start_y,100,210)
$b_1 = GUICtrlCreateRadio("",$Start_x + 20,$Start_y + 20,20,20)
$b_2 = GUICtrlCreateRadio("",$Start_x + 20,$Start_y + 45,20,20)
$b_3 = GUICtrlCreateRadio("",$Start_x + 20,$Start_y + 70,20,20)
$r_1 = GUICtrlCreateRadio("",$Start_x + 20,$Start_y + 95,20,20)
$r_2 = GUICtrlCreateRadio("",$Start_x + 20,$Start_y + 120,20,20)
$r_3 = GUICtrlCreateRadio("",$Start_x + 20,$Start_y + 145,20,20)
GUICtrlCreateLabel("Blue 1",$Start_x + 40,$Start_y + 21,50,20)
GUICtrlCreateLabel("Blue 2",$Start_x + 40,$Start_y + 46,50,20)
GUICtrlCreateLabel("Blue 3",$Start_x + 40,$Start_y + 71,50,20)
GUICtrlCreateLabel("Red 1",$Start_x + 40,$Start_y + 96,50,20)
GUICtrlCreateLabel("Red 2",$Start_x + 40,$Start_y + 121,50,20)
GUICtrlCreateLabel("Red 3",$Start_x + 40,$Start_y + 146,50,20)
$set = GUICtrlCreateButton("Set",$Start_x + 20,$start_y + 170,60,30)
GUICtrlSetColor($blue,0x0000ff)
GUICtrlSetColor($blue + 1,0x0000ff)
GUICtrlSetColor($blue + 2,0x0000ff)
GUICtrlSetColor($red,0xff0000)
GUICtrlSetColor($red + 1,0xff0000)
GUICtrlSetColor($red + 2,0xff0000)
GUICtrlCreateGroup("Options",$check_x,$check_y,250,110)
GUICtrlCreateCheckbox("Accelerate",$check_x + 30,$check_y + 20,80,20)
GUICtrlCreateCheckbox("Decelerate",$check_x + 150,$check_y + 20,80,20)
GUICtrlCreateCheckbox("Brakes",$check_x + 30,$check_y + 40,80,20)
GUICtrlCreateCheckbox("Camera",$check_x + 150,$check_y + 40,80,20)
GUICtrlCreateCheckbox("End Point",$check_x + 30,$check_y + 60,80,20)
GUICtrlCreateCheckbox("Half Sec Stop",$check_x + 150,$check_y + 60,80,20)
GUICtrlCreateCheckbox("1 Sec Stop",$check_x + 30,$check_y + 80,80,20)
GUICtrlCreateCheckbox("1.5 Sec Stop",$check_x + 150,$check_y + 80,80,20)
$undo = GUICtrlCreateButton("Undo",650,20,50,30)
GUICtrlCreateInput($code,740,315,250,300,"0x00201004")
GUICtrlCreatePic("C:\PS\field1.bmp",50,70,648,315)
GUICtrlCreatePic("",0,0,1,1)


GUISetState()



$win = WinGetPos("   WÌÑÑÒVÅTÍÓÑ  P§  ß   ","")
$size1 = WinGetClientSize("   WÌÑÑÒVÅTÍÓÑ  P§  ß   ")
$offset_x = $win[2] - $size1[0] - 3
$offset_y = $win[3] - $size1[1] - 3



;<===Check GUI===>
While 1
    $dll = DllOpen("user32.dll")
        If _IsPressed("01", $dll) Then everything()
    DllClose($dll)
    Sleep(5)
    $msg = GUIGetMsg()
    If $msg = $reset Then reset()
    If $msg = $save then save()
    If $msg = $exit then exit1()
    If $msg = $undo Then MsgBox(0,"Fix this...","Kyler will fix the EVIL undo button... eventually... if he feels like it... which will be well... never.");undo()
    If $msg = $GUI_EVENT_CLOSE Then exit1()
    If $msg = $point Then point()
    If $msg = $high Then $gear = "High"
    If $msg = $mid Then $gear = "Mid"
    If $msg = $low Then $gear = "Low"
    If $msg = $set Then start()
    If $msg = $b_1 Then $auto = 1
    If $msg = $b_2 Then $auto = 2
    If $msg = $b_3 Then $auto = 3
    If $msg = $r_1 Then $auto = 4
    If $msg = $r_2 Then $auto = 5
    If $msg = $r_3 Then $auto = 6
    GetPos()
WEnd


;<===Functions===>
Func undo()
   ;MsgBox(0,"",$current1)
    $delete = 1
    Do
        GUICtrlDelete($delete + $current1 - $line)
        $delete = $delete + 1
    Until $delete = $line + 5
EndFunc


Func start()
    HotKeySet( "z", "everything")
    $win = WinGetPos("   WÌÑÑÒVÅTÍÓÑ  P§  ß   ","")
    If $auto = 1 Then;<=== Blue 1
        $pos = MouseGetPos()
       ;MouseMove(317 + $win[0],122 + $win[1],0)
        MouseMove($offset_x + $win[0] + 50 + 264,$offset_y + $win[1] + 70 + 30,0)
        Send("z")
        MouseMove($pos[0],$pos[1],0)
    EndIf
    If $auto = 2 Then;<=== Blue 2
        $pos = MouseGetPos()
       ;MouseMove(377,122,0)
        MouseMove($offset_x + $win[0] + 50 + 324,$offset_y + $win[1] + 70 + 30,0)
        Send("z")
        MouseMove($pos[0],$pos[1],0)
    EndIf
    If $auto = 3 Then;<=== Blue 3
        $pos = MouseGetPos()
       ;MouseMove(437,122,0)
        MouseMove($offset_x + $win[0] + 50 + 384,$offset_y + $win[1] + 70 + 30,0)
        Send("z")
        MouseMove($pos[0],$pos[1],0)
    EndIf
    If $auto = 6 Then;<=== Red 3
        $pos = MouseGetPos()
       ;MouseMove(317,377,0)
        MouseMove($offset_x + $win[0] + 50 + 264,$offset_y + $win[1] + 70 + 285,0)
        Send("z")
        MouseMove($pos[0],$pos[1],0)
    EndIf
    If $auto = 5 Then;<=== Red 2
        $pos = MouseGetPos()
       ;MouseMove(377,377,0)
        MouseMove($offset_x + $win[0] + 50 + 324,$offset_y + $win[1] + 70 + 285,0)
        Send("z")
        MouseMove($pos[0],$pos[1],0)
    EndIf
    If $auto = 4 Then;<=== Red 1
        $pos = MouseGetPos()
       ;MouseMove(437,377,0)
        MouseMove($offset_x + $win[0] + 50 + 384,$offset_y + $win[1] + 70 + 285,0)
        Send("z")
        MouseMove($pos[0],$pos[1],0)
    EndIf
    HotKeySet( "z")
EndFunc

Func GetPos()
    $a = GUIGetCursorInfo()
    If $a[4] > $control - 1 And WinActive("   WÌÑÑÒVÅTÍÓÑ  P§  ß   ") Then
        If $mode = 0 Then
            $x_coord = $a[0] -50
            $y_coord = $a[1] - 70
        ElseIf $mode = 1 Then 
            $x_coord = $a[1] -70
            $y_coord = $a[0] - 50
        ElseIf $mode = 2 Then
            $x_coord = Abs($a[0] - 648 - 50)
            $y_coord = Abs($a[1] - 315 - 70)
        EndIf
        GUIctrlSetData(4108,$x_coord) 
        GUIctrlSetData(4110,$y_coord)
    EndIf
EndFunc

Func point()
    $win = WinGetPos("   WÌÑÑÒVÅTÍÓÑ  P§  ß   ","")
    HotKeySet( "z", "everything")
    $x1 = GUICtrlRead($x_pos)
    $y1 = GUICtrlRead($y_pos)
    $pos = MouseGetPos()
    $x2 =  $x1 + 50 + $offset_x + $win[0]
    $y2 = $y1 + 70 + $offset_y + $win[1]
    MouseMove($x2,$y2,0)
    Send("z")
    MouseMove($pos[0],$pos[1],0)
    HotKeySet( "z")
EndFunc


    
Func everything()
    $current = $current + 1
    $current1 = $current
    sleep(50)
    $window=GUIGetCursorInfo()
    If $window[4] > $control - 1 And WinActive("   WÌÑÑÒVÅTÍÓÑ  P§  ß   ") Then
        waypoint()
        linedraw()
        code()
        waypoint()
        sleep(100)
    EndIf
EndFunc
    
Func reset()
    $auto = 0
    $delete = 1
    $mode = 0
    Do
        GUICtrlDelete($delete + $control)
        $delete = $delete + 1
    Until $delete = 5000
    $count = 0
   ;GUICtrlDelete($control)
   ;GUICtrlCreatePic(@DesktopDir & "\field1.bmp",50,70,648,315)
    $text = ""
    GUICtrlSetData(4105,$text)
    GUICtrlSetState($b_1, $GUI_UNCHECKED)
    GUICtrlSetState($b_2, $GUI_UNCHECKED)
    GUICtrlSetState($b_3, $GUI_UNCHECKED)
    GUICtrlSetState($r_1, $GUI_UNCHECKED)
    GUICtrlSetState($r_2, $GUI_UNCHECKED)
    GUICtrlSetState($r_3, $GUI_UNCHECKED)
    $num = 0
    $head = "int waypoints[" & $num  & "][3]=" & @CRLF & "{/*   x   y   g   */"
    $output = "" 
    $output1 = $head &  $output & @CRLF & "};"
    GUICtrlSetData(4140,$output1)
EndFunc

Func save()
    ClipPut($text)
EndFunc

Func exit1()
    Exit
EndFunc

Func linedraw()
        $win = WinGetPos("   WÌÑÑÒVÅTÍÓÑ  P§  ß   ")
    If $count = 0 Then 
        $first = MouseGetPos()
        $x11 = $first[0]  - $offset_x - $win[0]
        $y11 = $first[1]  - $offset_y - $win[1]
       ;MsgBox(0,"",$first[0] & $first[1])
    EndIf
    If $count = 1 Then 
        $second = MouseGetPos()
        $x22 = $second[0]  - $offset_x - $win[0]
        $y22 = $second[1] - $offset_y - $win[1]
        GUICtrlCreateLine($x11,$y11,$x22,$y22,2)
        $x11 = $x22
        $y11 = $y22
    EndIf
    If $count = 2 Then 
        $second = MouseGetPos()
        $x11 = $x22
        $y11 = $y22
        $x22 = $second[0]  - $offset_x - $win[0]
        $y22 = $second[1] - $offset_y - $win[1]
        $count = 1
        GUICtrlCreateLine($x11,$y11,$x22,$y22,2)
    EndIf
    $count = $count + 1
EndFunc

Func waypoint()
    $window = GUIGetCursorInfo()
    $x = $window[0] - 4
    $y = $window[1] - 4
    GUICtrlCreatePic("C:\PS\dot.bmp",$x,$y,9,9)
EndFunc


Func code()
    If $auto = 0 Then $start_pos = ""
    If $auto = 1 Then $start_pos = " at Blue 1"
    If $auto = 2 Then $start_pos = " at Blue 2"
    If $auto = 3 Then $start_pos = " at Blue 3"
    If $auto = 4 Then $start_pos = " at Red 1"
    If $auto = 5 Then $start_pos = " at Red 2"
    If $auto = 6 Then $start_pos = " at Red 3"
    $window=GUIGetCursorInfo()
    $win = WinGetPos("   WÌÑÑÒVÅTÍÓÑ  P§  ß   ")
    $pos = MouseGetPos()
    $x_move = $window[0]; - 50
    $y_move = $window[1]; - 70
    $distance = ""
    If $gear = "Low" Then $gear_num = 1
    If $gear = "Mid" Then $gear_num = 2
    If $gear = "High" Then $gear_num = 3
    
    If $auto = 4 Or $auto = 5 Or $auto = 6 And $num = 0 Then 
       ;$x_move1[$num] = $y_move - 70
       ;$y_move1[$num] = $x_move - 50
        $x_move1[$num] = $window[1] - 70
        $y_move1[$num] = $window[0] - 50
        $gear_num1[$num] = $gear_num
        $mode = 1
       ;MsgBox(0,"",$x_move)
    ElseIf $auto = 1 Or $auto = 2 Or $auto = 3 And $num = 0 Then
       ;$x_move1[$num] = Abs($x_move - 648 - 50)
       ;$y_move1[$num] = Abs($y_move - 315 - 70)
        $x_move1[$num] = Abs($window[0] - 648 - 50)
        $y_move1[$num] = Abs($window[1] - 315 - 70)
        $gear_num1[$num] = $gear_num
        $mode = 2
    ElseIf $auto = 0 Then
       ;$x_move1[$num] = $x_move - 50
       ;$y_move1[$num] = $y_move - 70
        $x_move1[$num] = $window[0] - 50
        $y_move1[$num] = $window[1] - 70
        $gear_num1[$num] = $gear_num
        $mode = 0
    EndIf
    
    If $x_move1[$num] = "" Then $x_move1[$num] = 0
    If $y_move1[$num] = "" Then $y_move1[$num] = 0
    
    If $text <> "" Then
        $text = GUICtrlRead(4105)
        $distance = Sqrt ( ($x_move - $x_old)^2 + ($y_move - $y_old)^2 )
        $feet1 = Floor($distance / 12) 
        $feet2 = Round($distance - ($feet1 * 12),2)
        $feet = " OR " & $feet1 & "'" & $feet2 & '" '
        $feet3 = $feet1 & "'" & $feet2 & '" '
        $value = Round(ATan(($y_move - $y_old)/($x_move - $x_old)) * -57.2957795130823,4)
       ;If $x_move - $x_old > 0 Then $value = $value + 180
        If $distance <> 0 Then $text = $text & @CRLF & $num & ":                " & $x_move1[$num] & "        " & $y_move1[$num] & "             " & $gear & "              " & $feet3 & "          " & $value & "   " & $window[0]; "                                                                                      Begin point " & $start_pos 
       ;If $distance <> 0 Then $text = $text & "(" & $x_move2 & "," & $y_move2 & ")" & @CRLF & "Distance is: " & $distance & " OR " & $feet & "." & @CRLF & "Angle is: " &  $value & " º." & @CRLF & "Moving to this point in " & $gear & " gear." & @CRLF & @CRLF
    EndIf
   ;If $text = "" Then $text = "(" & $x_move2 & "," & $y_move2 & ")" & @CRLF & "Begin Point" & $start_pos & "." & @CRLF & @CRLF
    If $text = "" Then $text = "Point           X             Y            Gear            Distance          Angle             Notes" & @CRLF & $num & ":               " & $x_move1[$num] & "        " & $y_move1[$num]  & "                                                                                       Begin point " & $start_pos 
    $x_old = $x_move
    $y_old = $y_move
    $head = "int waypoints[" & $num + 1 & "][3]=" & @CRLF & "{/*    x   y   g   */"
    $output = $output & @CRLF & "/*" & $num & "*/  { " & $x_move1[$num] & ",          " & $y_move1[$num] & ",           " & $gear_num1[$num] & "        }," 
    $output1 = $head &  $output & @CRLF & "};"
    GUICtrlSetData(4105,$text)
    GUICtrlSetData(4140,$output1)
    $num = $num + 1

EndFunc

Func GUICtrlCreateLine($x1, $y1, $x2, $y2, $size = 1, $color = "0x00ff00")
    $line = 1
    If $gear = "Low" Then $color = "0xff0000"
    If $gear = "Mid" Then $color = "0xffff00"
    If $gear = "High" Then $color = "0x00ff00"
    $deltaX = $x2 - $x1
    $deltaY = $y2 - $y1
    $length = _Ceil (Sqrt($deltaX * $deltaX + $deltaY * $deltaY))
    $incDeltaX = $deltaX / $length
    $incDeltaY = $deltaY / $length
    For $i = 0 To $length
        GUICtrlDelete($controlIDs[$nextControl])
        $controlIDs[$nextControl] = GUICtrlCreateLabel("", $x1 + $incDeltaX * $i, $y1 + $incDeltaY * $i, $size, $size)
        GUICtrlSetBkColor($controlIDs[$nextControl], $color)
        $nextControl = Mod($nextControl + 1, $maxControls)
        $current = $current + 1
        $line = $line + 1
    Next
   ;MsgBox(0,"",$current)
    Return 1
EndFunc ;==>GUICtrlCreateLine

Func _IsPressed($s_hexKey, $v_dll = 'user32.dll')
    Local $a_R = DllCall($v_dll, "int", "GetAsyncKeyState", "int", '0x' & $s_hexKey)
    If Not @error And BitAND($a_R[0], 0x8000) = 0x8000 Then Return 1
    Return 0
EndFunc  ;==>_IsPressed

Sorry that its as messy as it is... but it is very important for me and for others that I can fix this. Any and all help is VERY appreciated because i truely don't know what has gone wrong. I'll also attach a compiled version if anybody wants to try the program and see the problems themselves. Also if anybody knows why the green dots (waypoints / dot.bmp file) don't always show up, can they help me fix that too? Also, if there are any other suggestings you have, don't hesitate to suggest it.

thanks and i hope you guys can help!

P.S. If I have to, I can add some comments tomorrow but I just want to see if anybody can help overnight.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...