#------------------------------------------------------------------------------#
# Include source files
#------------------------------------------------------------------------------#

#source random.tcl
source sincos_table.tcl

#------------------------------------------------------------------------------#
# Initialize fog compass graphic id
#------------------------------------------------------------------------------#

set pi 3.14
set compx 0
set compy 0
set com_name ""
set vhframe ""
set head_check_var 0
set heading_timer 0
set cover_head_id 0
set head_id 0
set auto_head_id 0
set circle_id 0
set north_id 0
set south_id 0
set east_id 0
set west_id 0
set nw_id 0
set ne_id 0
set sw_id 0
set se_id 0
set black_circle_id 0
set fog_loop_id 0
set backup_loop_id 0
set BACKUP_COMPASS 101
set backup_head_check_var 0
set backup_heading_timer 0
set turn_heading_old 0
set turn_ship_old 0
set turn_backup_old 0



set fog_deviation 0
set fog_deviation [lindex [split [get_compass_deviation]  | ] 0 ]
set compass_deviation [lindex [split [get_compass_deviation]  | ] 1 ]
set fog_declination [lindex [split [get_compass_deviation]  | ] 2 ]
set compass_declination [lindex [split [get_compass_deviation]  | ] 3 ]
set angle 0
set angle_bak 0

set primary_source_title "Fog"
set backup_source_title "KVH"

#------------------------------------------------------------------------------#
# Initialize backup compass graphic id
#------------------------------------------------------------------------------#

set cover_head_bak_id 0
set head_bak_id 0
set auto_head_bak_id 0
set circle_bak_id 0
set north_bak_id 0
set south_bak_id 0
set east_bak_id 0
set west_bak_id 0
set nw_bak_id 0
set ne_bak_id 0
set sw_bak_id 0
set se_bak_id 0
set black_circle_bak_id 0
set sub_turn_bak 0


#------------------------------------------------------------------------------#
# Change this number if we want to change the size of graph
#------------------------------------------------------------------------------#

set size 50

#------------------------------------------------------------------------------#
# Initialize the rest of the graphic id variables
#------------------------------------------------------------------------------#

for  {set i 0 } {$i <= 360 } {incr i } {
	
	set hc($i) 0
	set hcb($i) 0
	set lgreen_id($i) 0
	set lgreen_bak_id($i) 0
	set white_mark_id($i) 0
	
}


#------------------------------------------------------------------------------#
# This procedure will set the x y position to variables
# "compx" and "compy" that can be used globally in this file
#------------------------------------------------------------------------------#

proc set_compass_pos { x y } {

   global compx compy


   set compx $x
   set compy $y
}


#------------------------------------------------------------------------------#
# This procedure get the number used to define a new window path name
#------------------------------------------------------------------------------#

proc get_compass_window_path_name { name } {

	global com_name vhframe

 	set com_name $name
	set vhframe .ventana.editor.$com_name

}


#------------------------------------------------------------------------------#
# This function creates a frame to hold all the canvas and other information
#
#------------------------------------------------------------------------------#

proc display_heading_or_not { flag } {
		
	global compx compy	
	global head_surveh
	global head_diganal
	global head_elem_num
	global vhframe
	global head_check_var backup_head_check_var
	global com_name
	global size

	#-----------------------------------------------#
	# set the frame to be the current compass type
	#-----------------------------------------------#
	set vhframe .ventana.editor.$com_name
	
	if {$flag == 1} {
		
		if [winfo exists $vhframe] { destroy $vhframe }
		
		if { $com_name == "fog" } {
			set head_check_var 1
		} else {
			set backup_head_check_var 1
		}
		
		frame $vhframe -background black -width 147 -height 280
		place $vhframe -x $compx -y $compy
		
		#-----------------------------------------------#
   		# Create the canvas to draw the compass
   		#-----------------------------------------------#
		set vc $vhframe.compass
		set size_oval [expr { 2 * $size }]
   		canvas $vc -width [expr {$size_oval + 50}] -height [expr {$size_oval + 180}] \
	 		-background black
  		place $vc -x -1 -y -1
  		
		#-----------------------------------------------#
  		# Draw the graphic that are static
  		# Draw the head -> which will recursively call
    		# itself again to redraw the position
    		#-----------------------------------------------#
		draw_static_graphic  	
   	
    	    	if { $com_name == "fog" } {        	    			
   			heading
   		} else {
   			backup_heading
   		}	
   	
	} else {
	
		#-----------------------------------------------#
		# This will return from the graphic loop
		# (that redraw the canvas for update)
		#-----------------------------------------------#
				
		if { $com_name == "fog" } {    		
    	    		set head_check_var 0
 		} else {
 			set backup_head_check_var 0	
 		}
 		
 		destroy $vhframe		
    	}                                          	
}



#------------------------------------------------------------------------------#
# This procedure will draw the compass that show the :
# 	- heading	angle in degrees
# 	- auto heading	angle in degrees
#	- sub_turn       	'+' if clockwise and '-' otherwise
#	- hist              	list of the heading history values in degree
#------------------------------------------------------------------------------#



proc draw_static_graphic { } {
     	
	#-----------------------------------------------#
	# Declare and initialize variables		
	#-----------------------------------------------#
	
	global vhframe
	global com_name
	global size
        global primary_source_title backup_source_title

        set size_oval [expr { 2 * $size }]

	#-----------------------------------------------#
	# Set canvas vc
	#-----------------------------------------------#
	set vc $vhframe.compass

	#-----------------------------------------------#
	# Create the outer circle for compass
	#-----------------------------------------------#
	set outer_circle_id [$vc create oval 15 15 \
		[expr { $size_oval + 35 } ] [expr { $size_oval + 35 } ] -outline blue  ]


       	if {$com_name == "fog" } {
		#-----------------------------------------------#
		# Text value for heading
		#-----------------------------------------------#
	   	label $vc.headlabel -fg white -bg black -textvariable primary_source_title
   		entry $vc.heading -bg black -fg white -state disabled -relief flat -width 10 \
   			-borderwidth 0 -textvariable angle
	   	place $vc.headlabel -x [expr {$size - 45 }] -y [expr {$size_oval + 55}]	
   		place $vc.heading -x [expr {$size + 15 }] -y [expr {$size_oval + 55}]

	   	#-----------------------------------------------#
   		# Text value for auto heading
	   	#-----------------------------------------------#
   		label $vc.auto_headlabel -fg white -bg black -text "Auto "
	   	entry $vc.auto_heading -bg black -fg white -state disabled -relief flat -width 10 \
   			-borderwidth 0 -textvariable auto_angle
	   	place $vc.auto_headlabel -x [expr {$size - 45 }] -y [expr {$size_oval + 95}]	
   		place $vc.auto_heading -x [expr {$size + 15 }] -y [expr {$size_oval + 95}]
        	
		#-----------------------------------------------#
		# Vehicle turns display
		#-----------------------------------------------#
		
		entry $vc.sub_turn -bg black -fg white -state disabled -relief flat -width 3 \
   			-borderwidth 0 -textvariable sub_turn
		place $vc.sub_turn -x [expr {$size + 15 }] -y [expr {$size_oval + 115}]
   				
	   	#------------------------------------------------#
   		# Title of the compass
	   	#------------------------------------------------#
		label $vc.title -text "Primary" -bg black -fg yellow
	 	place $vc.title -x [expr {$size - 45 }] -y [expr {$size_oval + 140}]

   	
  		#------------------------------------------------#
	   	# Button to reset the number of turns
   		#------------------------------------------------#
	   	button $vc.reset_turn -text "Reset Turn"  \
			-command { global sub_turn ship_turn
				 set sub_turn 0
				 set ship_turn 0
				 }
		place $vc.reset_turn -width 75 -x [expr {$size + 15 }] -y [expr {$size_oval + 140}]
 	
 	# end if 	
   	} else {
   	
   		#-----------------------------------------------#
   		# Text value for heading
   		#-----------------------------------------------#
   		label $vc.headlabel -fg white -bg black -textvariable backup_source_title
   		entry $vc.heading -bg black -fg white -state disabled -relief flat -width 10 \
   			-borderwidth 0 -textvariable angle_bak
  		place $vc.headlabel -x [expr {$size - 45 }] -y [expr {$size_oval + 55}]	
   		place $vc.heading -x [expr {$size + 15 }] -y [expr {$size_oval + 55}]

   	
   		#-----------------------------------------------#
   		# Text value for auto heading
   		#-----------------------------------------------#
   		label $vc.auto_headlabel -fg white -bg black -text "Auto "
   		entry $vc.auto_heading -bg black -fg white -state disabled -relief flat -width 10 \
   			-borderwidth 0 -textvariable auto_angle_bak
 		place $vc.auto_headlabel -x [expr {$size - 45 }] -y [expr {$size_oval + 95}]	
  	 	place $vc.auto_heading -x [expr {$size + 15 }] -y [expr {$size_oval + 95}]
	   	
		#-----------------------------------------------#
		# Ship Vehicle display
		#-----------------------------------------------#

		entry $vc.sub_turn -bg black -fg white -state disabled -relief flat -width 3 \
   			-borderwidth 0 -textvariable sub_turn_bak
	   	place $vc.sub_turn -x [expr {$size + 15 }] -y [expr {$size_oval + 115}]
		
		#-----------------------------------------------#
	   	# Title of the compass
	   	#-----------------------------------------------#
	  	label $vc.title -text "Backup" -bg black -fg yellow -wraplength 50
	   	place $vc.title -x [expr {$size - 45 }] -y [expr {$size_oval + 140}]

 	  	#-----------------------------------------------#
  		# Button to reset the number of sub_turn
   		#-----------------------------------------------#
   		button $vc.reset_turn -text "Reset Turn"  \
			-command {	global sub_turn_bak ship_turn
						set sub_turn_bak 0
						set ship_turn 0
					 }
		place $vc.reset_turn -width 75 \
 	  		-x [expr {$size + 15 }] -y [expr {$size_oval + 140}]


	# end else
 	}

	#-----------------------------------------------#
	# Display value for ship heading
	#-----------------------------------------------#
	label $vc.ship_label -fg white -bg black -text "Ship"
	entry $vc.ship_heading -bg black -fg white -state disabled -relief flat -width 10 \
		-borderwidth 0 -textvariable ship_angle
	place $vc.ship_label -x [expr {$size - 45 }] -y [expr {$size_oval + 75}]	
	place $vc.ship_heading -x [expr {$size + 15 }] -y [expr {$size_oval + 75}]

	#-----------------------------------------------#
   	# Turns Label
	#-----------------------------------------------#
   	label $vc.turnlabel -fg white -bg black -text "Turns "
	place $vc.turnlabel -x [expr {$size - 45 }] -y [expr {$size_oval + 115}]	
   	

	#-----------------------------------------------#
	# Ship turns display
	#-----------------------------------------------#
	label $vc.ship_turn_lab -bg black -fg white -font {Halvetica 8} -text "Ship"	
	label $vc.ship_turn_arr -bg black -fg white -font {Halvetica 8} -text "--->"	
	entry $vc.ship_turn -bg black -fg white -state disabled -relief flat -width 3 \
  		-borderwidth 0 -textvariable ship_turn
        place $vc.ship_turn_lab -x [expr {$size + 40 }] -y [expr {$size_oval + 115}]
	place $vc.ship_turn_arr -x [expr {$size + 40 }] -y [expr {$size_oval + 125}]
	place $vc.ship_turn -x [expr {$size + 64 }] -y [expr {$size_oval + 115}]
   	
	}




################################################################################
#
# FOG GYRO HEADING
#
################################################################################



#------------------------------------------------------------------------------#
# This procedure animate the heading display
#------------------------------------------------------------------------------#

proc heading { } {

	#-----------------------------------------------#
	# Declare variables 
	#-----------------------------------------------#

	global com_name
	global hc head_id ship_id auto_head_id black_circle_id
	global head_check_var  heading_timer
	global angle ship_angle auto_angle sub_turn 
	global lgreen_id circle_id cover_head_id
	global north_id south_id east_id west_id
	global turn_heading_new turn_heading_old turn_ship_new turn_ship_old
	global size ship_turn auto_angle_dvl
       	global fog_deviation fog_declination compass_deviation
      	global head_source_input primary_source_title
      	
      	#-----------------------------------------------#
	# Get the heading history from c++ function
	#-----------------------------------------------#
	
	set hist [split [get_head_hist] |]
	set auto_angle_dvl [get_auto_setpt]
	if { $head_source_input == "FOG GYRO" } {
		set primary_source_title "Fog"
		if { ($fog_deviation != "") && ($fog_deviation != "-") } {
	        	set temp_angle [lindex [ split [expr [get_head] + $fog_deviation] . ] 0]
			set angle [expr ($temp_angle%360) ]
			set temp_angle [lindex [ split [expr [get_auto_setpt] + $fog_deviation] . ] 0]
        		set auto_angle [expr ($temp_angle%360) ]
	        } else {
        		set temp_angle [lindex [ split [expr [get_head]] . ] 0]
			set angle [expr ($temp_angle%360) ]
			set temp_angle [lindex [ split [expr [get_auto_setpt]] . ] 0]
        		set auto_angle [expr ($temp_angle%360) ]
        	}
        } elseif { $head_source_input == "BACKUP COMPASS" } {
                set primary_source_title "KVH"
		if { ($compass_deviation > -1) && ($compass_deviation < 1000) } {	
	        	set temp_angle [lindex [ split [expr [get_aux_heading] + $compass_deviation] . ] 0]
			set angle [expr ($temp_angle%360) ]
			set temp_angle [lindex [ split [expr [get_auto_setpt] + $compass_deviation] . ] 0]
	        	set auto_angle [expr ($temp_angle%360) ]
        	} else {
		    set temp_angle [lindex [ split [expr [get_aux_heading]] . ] 0]
			set angle [expr ($temp_angle%360) ]
			set temp_angle [lindex [ split [expr [get_auto_setpt]] . ] 0]
	        	set auto_angle [expr ($temp_angle%360) ]    
		}
        } elseif { $head_source_input == "DVL" } {
                set primary_source_title "DVL"
        	set temp_angle [get_dvl_heading]
        	set angle [lindex [ split $temp_angle . ] 0 ]
        	set auto_angle $auto_angle_dvl
        }
	
	set ship_angle [get_ship_head]		
	set size_oval [expr { 2 * $size }]
	set size1 60

	#-----------------------------------------------#
	# Set variable vc
	#-----------------------------------------------#
	set vc .ventana.editor.fog.compass


	#-----------------------------------------------#
	# Check if the button name has already exists
	#-----------------------------------------------#
	if [ winfo exists $vc] {
   	
	   	$vc delete $head_id
	   	$vc delete $auto_head_id
	   	$vc delete $black_circle_id
	   	$vc delete $circle_id
	   	$vc delete $cover_head_id
	   	$vc delete $north_id
	   	$vc delete $south_id
	   	$vc delete $west_id
	   	$vc delete $east_id
 	
 	  	for {set count 0} {$count <= 360 } {incr count } {
 	  		$vc delete $lgreen_id($count)
 	    	}
	
	   	for {set count 0} {$count <= 70 } {incr count } {
 	  		$vc delete $hc($count)
 	    	}
	}

   	if {![ winfo exists $vc] || $head_check_var == 0} {
    		after cancel fog_loop_id
    		return
	}


	#-----------------------------------------------#
	# update the timer
	#-----------------------------------------------#
	incr heading_timer
	if { $heading_timer >= 100 } {
		redisplay_heading
		return
	}

	#--------------------------------------#
	# x y line coordinates for heading     #
	#--------------------------------------#

   	set x [expr { $size1* [my_sin -$angle]  }]
   	set y [expr { $size1* [my_cos $auto_angle] }]

 #################################################################


	set x1 [expr {$size + $x + 25}]
	set y1 [expr {$size - $y + 25}]
	set x2 [expr {$size- $x + 25}]
	set y2 [expr {$size + $y + 25}]

	#-------------------------------------------#
	# x y line coordinates for auto heading     #
	#-------------------------------------------#

	set auto_teta_display [expr {$angle - $auto_angle}]
	if  { $auto_teta_display < 0 } {
		set auto_teta_display [expr {$auto_teta_display + 360 }]
	}

	set autox [expr { $size1* [my_sin -$auto_teta_display] }]
	set autoy [expr { $size1* [my_cos $auto_teta_display] }]


   #################################################################


	set autox1 [expr {$size + $autox + 25}]
	set autoy1 [expr {$size - $autoy + 25}]
	set autox2 [expr {$size - $autox + 25}]
	set autoy2 [expr {$size + $autoy + 25}]


	#-----------------------------------------------#
	# Creating line for every 10 degree angle
	#-----------------------------------------------#

	for { set i 0 } { $i < 360 } {incr i 10} {

		# the length of marks
		set len 10

		# Now, draw the longer ones into canvas

		set xl [expr { ($size + $len) * [my_sin [expr {-($i + $angle)}] ] }]
		set yl [expr { ($size + $len) * [my_cos [expr {($i + $angle)}] ] }]

      ##################################################################

      	
		set xl1 [expr { $size + $xl  + 25}]
		set yl1 [expr { $size - $yl  + 25}]
		set xl2 [expr { $size - $xl  + 25}]
		set yl2 [expr { $size + $yl  + 25}]

		#-----------------------------------------------#
		# Draw the little green mark
		#-----------------------------------------------#
	
		if {$i == 90 || $i == 180 || $i == 270 || $i == 360 } {
			set lgreen_id($i) [$vc create line $xl1 $yl1 $xl2 $yl2  -fill green ]
		} else {
			set lgreen_id($i) [$vc create line $xl1 $yl1 $xl2 $yl2 -fill blue ]
		}


	# end for loop
	}

     set xl [expr { ($size + $len + 10 ) * [my_sin -$angle] }]
     set yl [expr { ($size + $len + 10 ) * [my_cos $angle] }]

      ##################################################################

      set xl1 [expr { $size + $xl  + 25}]
      set yl1 [expr { $size - $yl  + 25}]
      set xl2 [expr { $size - $xl  + 25}]
      set yl2 [expr { $size + $yl  + 25}]

      set xe [expr { ($size + $len + 10 ) * [my_sin  [expr {-($angle + 90) }]] }]
      set ye [expr { ($size + $len + 10 ) * [my_cos [expr {($angle + 90) }] ] }]

      ##################################################################

      set xe1 [expr { $size + $xe  + 25}]
      set ye1 [expr { $size - $ye  + 25}]
      set xe2 [expr { $size - $xe  + 25}]
      set ye2 [expr { $size + $ye  + 25}]

	#-----------------------------------------------#
	# Create text to show the direction (N, E, S, W)
	#-----------------------------------------------#

	set north_id [$vc create text $xl1 $yl1 -text N  -fill green]
	set east_id [ $vc create text  $xe1 $ye1   -text W -fill green ]
	set south_id [ $vc create text  $xl2 $yl2  -text S -fill green ]
	set west_id [ $vc create text  $xe2 $ye2   -text E -fill green ]

	
	#-----------------------------------------------#
	# Draw the heading and auto heading
	#-----------------------------------------------#
	set head_id [$vc create line 75 15 75 75 -arrow first -width 3 -fill green ]

 	if { [get_auto_head_status] > 0 }  {
		set auto_head_id [$vc create line $autox1 $autoy1 75 75  -arrow first -width 3 -fill red ]
	}

	#-----------------------------------------------#
	# Draw the inner blue circle
	#-----------------------------------------------#
	set circle_id [$vc create oval 25 25 \
		[expr { $size_oval + 25 } ] [expr { $size_oval + 25 } ] -outline blue  ]

	#-----------------------------------------------#
	# To cover the middle part of circle with black
	#-----------------------------------------------#
	set black_circle_id [$vc create oval 27 27 \
		[expr { $size_oval + 23 } ] [expr { $size_oval + 23 } ] -fill black  -width 3]
                                               	
	#-----------------------------------------------#
	# Now draw the history
	#-----------------------------------------------#
	set temp $size
	set count 0
  	
	#-----------------------------------------------#   
	# Initialize the first variable for 	
	# the first variable 		
	#-----------------------------------------------#
	set t(0) 0

	for {set i 1} {$i <= 50 } {incr i} {
		set j [expr {$i - 1}]
		set e1 [lindex $hist $i]
		set e2 [lindex $hist $j ]
		
		if {$e1 == "" || $e2 == "" } { break }
   	
		set t($i) [expr {$e1 - $e2 + $t($j) }]
		
	}

	for {set i 0} {$i < 50 } {incr i} {

		set item $t($i)
		if {$item == "" } { break }

		set xhist [expr { $temp* [my_sin $item] }]
		set yhist [expr { $temp* [my_cos $item] }]
  	
  	
  	################################################################

		set xhist1 [expr {$size + $xhist + 25 }]
		set yhist1 [expr {$size - $yhist + 25 }]  	
		set xhist2 [expr {$xhist1 + 3 }]
		set yhist2 [expr {$yhist1 + 3 }]                                	
  	
		set hc($count) [$vc create oval $xhist1 $yhist1 $xhist2 $yhist2 -fill white]
     	
		incr count
		incr temp -1            
	
	# end for loop                                               	
	}


	#-----------------------------------------------#
	# Count the number of turn the vehicle made
	#-----------------------------------------------#
   
	set turn_heading_new $angle
	if { $turn_heading_new >= 0 && $turn_heading_new < 90 &&
   		$turn_heading_old > 270 && $turn_heading_old < 360 } {
   	
		set sub_turn [expr ( $sub_turn + 1 ) ]
   	
	} elseif { $turn_heading_new > 270 && $turn_heading_new < 360 &&
			$turn_heading_old >= 0 && $turn_heading_old < 90 } {
   	
		set sub_turn [expr ( $sub_turn - 1 ) ]   	
	}

	set turn_heading_old $turn_heading_new


	#-----------------------------------------------#
	# Count the number of turn the ship made
	#-----------------------------------------------#

	set turn_ship_new $ship_angle
	if { $turn_ship_new >= 0 && $turn_ship_new < 90 &&
		$turn_ship_old > 270 && $turn_ship_old < 360 } {
   	
		set ship_turn [expr { $ship_turn + 1 } ]
   	
	} elseif { $turn_ship_new > 270 && $turn_ship_new < 360 &&
			$turn_ship_old >= 0 && $turn_ship_old < 90 } {
   	
		set ship_turn [expr ( $ship_turn - 1 ) ]   	
	}

	set turn_ship_old $turn_ship_new






	#-----------------------------------------------#
	# The switch is turned off
	#-----------------------------------------------#
   	
	if { [get_auto_head_status] <= 0 }  {
		set auto_angle ""
   	}

	#-----------------------------------------------#
	# Loop again
	#-----------------------------------------------#
	
	
	global fog_loop_id
	set fog_loop_id [after 200 heading]

  # end of procedure "heading"
}





################################################################################
#
# BACKUP HEADING
#
################################################################################


proc backup_heading { } {
	
	#-----------------------------------------------#
	# Declare variables 
	#-----------------------------------------------#
	
	global com_name
	global hcb head_bak_id  auto_head_bak_id black_circle_bak_id
	global backup_head_check_var  backup_heading_timer
	global angle_bak ship_angle_bak auto_angle_bak sub_turn_bak 
	global lgreen_bak_id circle_bak_id cover_head_bak_id
	global north_bak_id south_bak_id east_bak_id west_bak_id
	global turn_backup_old turn_backup_new turn_ship_old turn_ship_new
 	global ship_turn auto_angle_dvl
	global compass_deviation compass_declination fog_deviation fog_declination
	global sec_head_source_input backup_source_title

	if { $sec_head_source_input == "FOG GYRO" } {
		set backup_source_title "Fog"
		if { ($fog_deviation != "") && ($fog_deviation != "-") } {
	        	set temp_angle [lindex [ split [expr [get_head] + $fog_deviation] . ] 0]
			set angle_bak [expr ($temp_angle%360) ]
			set temp_angle [lindex [ split [expr [get_auto_setpt] + $fog_deviation] . ] 0]
        		set auto_angle_bak [expr ($temp_angle%360) ]
	        } else {
        		set temp_angle [lindex [ split [expr [get_head]] . ] 0]
			set angle_bak [expr ($temp_angle%360) ]
			set temp_angle [lindex [ split [expr [get_auto_setpt]] . ] 0]
        		set auto_angle_bak [expr ($temp_angle%360) ]
        	}
        } elseif { $sec_head_source_input == "BACKUP COMPASS" } {
                set backup_source_title "KVH"
		if { ($compass_deviation > -1) && ($compass_deviation < 1000) } {	
	        	set temp_angle [lindex [ split [expr [get_aux_heading] + $compass_deviation] . ] 0]
			set angle_bak [expr ($temp_angle%360) ]
			set temp_angle [lindex [ split [expr [get_auto_setpt] + $compass_deviation] . ] 0]
	        	set auto_angle_bak [expr ($temp_angle%360) ]
        	} else {
		    set temp_angle [lindex [ split [expr [get_aux_heading]] . ] 0]
			set angle_bak [expr ($temp_angle%360) ]
			set temp_angle [lindex [ split [expr [get_auto_setpt]] . ] 0]
	        	set auto_angle_bak [expr ($temp_angle%360) ]
		}
        } elseif { $sec_head_source_input == "DVL" } {
                set backup_source_title "DVL"
        	set temp_angle [get_dvl_heading]
        	set angle_bak [lindex [ split $temp_angle . ] 0 ]
        	set auto_angle_bak $auto_angle_dvl
        }

	set ship_angle [get_ship_head]




   	#-----------------------------------------------#
	# Declare and initialize variable 'size' here
	#-----------------------------------------------#
	global size

	set size_oval [expr { 2 * $size }]

	#-----------------------------------------------#
	# Set variable vc
	#-----------------------------------------------#
	set vc .ventana.editor.backup_comp.compass

	#-----------------------------------------------#
	# Check if the button name has already exists
	#-----------------------------------------------#
	if [ winfo exists $vc] {
   	
	   	$vc delete $head_bak_id
   		$vc delete $auto_head_bak_id
	   	$vc delete $black_circle_bak_id
   		$vc delete $circle_bak_id
	   	$vc delete $cover_head_bak_id
   		$vc delete $north_bak_id
	   	$vc delete $south_bak_id
   		$vc delete $west_bak_id
	   	$vc delete $east_bak_id
   	
   		for {set count 0} {$count <= 360 } {incr count } {
   			$vc delete $lgreen_bak_id($count)
	     	}

	   	for {set count 0} {$count <= 70 } {incr count } {
   			$vc delete $hcb($count)
	     	}
   		# end if [winfo exists $vc]
	}

	#-----------------------------------------------#
	# Kill the loop if the container doesn't exist
	#-----------------------------------------------#
	
   	if {![winfo exists $vc] || $backup_head_check_var == 0} {
   		after cancel backup_loop_id
    		return
   	}


	#-----------------------------------------------#
	# update the timer
	#-----------------------------------------------#
	incr backup_heading_timer
	if { $backup_heading_timer >= 200 } {
   		redisplay_backup_heading
	   	return
   	}


   	#-----------------------------------------------#
   	# set 'teta' and 'auto_teta' angle_bak in radian
   	#-----------------------------------------------#
   	set teta [conv_to_rad $angle_bak]
   	set auto_teta [conv_to_rad $auto_angle_bak]

   	set size1 60

   	#-----------------------------------------------#
   	# x y line coordinates for heading
   	#-----------------------------------------------#

   	set x [expr { $size1*sin(-$teta) }]
   	set y [expr { $size1*cos($teta) }]
   	set x1 [expr {$size + $x + 25}]
   	set y1 [expr {$size - $y + 25}]
   	set x2 [expr {$size- $x + 25}]
   	set y2 [expr {$size + $y + 25}]


   	#-----------------------------------------------#
   	# x y line coordinates for auto heading     	
   	#-----------------------------------------------#

   	set auto_teta_display [expr {$teta - $auto_teta} ]
   	if  { $auto_teta_display < 0 } {
   		set auto_teta_display [expr {$auto_teta_display + 6.28}]
   	}


   	set autox [expr { $size1*sin(-$auto_teta_display) }]
   	set autoy [expr { $size1*cos($auto_teta_display) }]
   	set autox1 [expr {$size + $autox + 25}]
   	set autoy1 [expr {$size - $autoy + 25}]
   	set autox2 [expr {$size - $autox + 25}]
   	set autoy2 [expr {$size + $autoy + 25}]


   	#-----------------------------------------------#
   	# Creating line for every 10 degree angle_bak
   	#-----------------------------------------------#

   	for { set i 0 } { $i < 360 } {incr i 10} {

		#-----------------------------------------------#
		# convert angle_bak 'i' from degrees to radian
		#-----------------------------------------------#

		set j [conv_to_rad $i]

		#-----------------------------------------------#
		# the length of marks
		#-----------------------------------------------#
		set len 10

		#-----------------------------------------------#
		# Now, draw the longer ones into canvas
		#-----------------------------------------------#

		set xl [expr { ($size + $len) *sin(-($j + $teta)) }]
		set yl [expr { ($size + $len) *cos($j + $teta) }]
		set xl1 [expr { $size + $xl  + 25}]
		set yl1 [expr { $size - $yl  + 25}]
		set xl2 [expr { $size - $xl  + 25}]
		set yl2 [expr { $size + $yl  + 25}]

		#-----------------------------------------------#
		# Draw the little green mark
		#-----------------------------------------------#

		if {$i == 90 || $i == 180 || $i == 270 || $i == 360 } {
			set lgreen_bak_id($i) [$vc create line $xl1 $yl1 $xl2 $yl2  -fill green ]
		} else {  	
			set lgreen_bak_id($i) [$vc create line $xl1 $yl1 $xl2 $yl2 -fill blue ]
		}


   		# end for loop
	}


	set xl [expr { ($size + $len + 10 ) *sin(-$teta) }]
	set yl [expr { ($size + $len + 10 ) *cos($teta) }]
	set xl1 [expr { $size + $xl  + 25}]
	set yl1 [expr { $size - $yl  + 25}]
	set xl2 [expr { $size - $xl  + 25}]
	set yl2 [expr { $size + $yl  + 25}]
	set xe [expr { ($size + $len + 10 ) *sin(-($teta + 1.571)) }]
	set ye [expr { ($size + $len + 10 ) *cos($teta + 1.571) }]
	set xe1 [expr { $size + $xe  + 25}]
	set ye1 [expr { $size - $ye  + 25}]
	set xe2 [expr { $size - $xe  + 25}]
	set ye2 [expr { $size + $ye  + 25}]

	#----------------------------------------------------#
	# Create text to show the direction (N, E, S, W)
	#----------------------------------------------------#
	set north_bak_id [$vc create text $xl1 $yl1 -text N  -fill green]
	set east_bak_id [ $vc create text  $xe1 $ye1   -text W -fill green ]
	set south_bak_id [ $vc create text  $xl2 $yl2  -text S -fill green ]
	set west_bak_id [ $vc create text  $xe2 $ye2   -text E -fill green ]


	#---------------------------------------------#	
	# Draw the heading and auto heading           #
	#---------------------------------------------#

	set head_bak_id [$vc create line 75 15 75 75 -arrow first -width 3 -fill green ]
	if { [get_auto_head_status] > 0 }  {
		set auto_head_bak_id [$vc create line $autox1 $autoy1 75 75  -arrow first -width 3 -fill red ]
	}

	#-----------------------------------------------#
	# Draw the inner blue circle
	#-----------------------------------------------#
	set circle_bak_id [$vc create oval 25 25 [expr { $size_oval + 25 } ] [expr { $size_oval + 25 } ] -outline blue  ]

	#--------------------------------------------------#
	# To cover the middle part of circle with black
	#--------------------------------------------------#
	set black_circle_bak_id [$vc create oval 27 27 [expr { $size_oval + 23 } ] [expr { $size_oval + 23 } ] -fill black  -width 3]
                                               	
	#-----------------------------------------------#
	# Now draw the history
	#-----------------------------------------------#
	set temp $size
	set count 0
  	
	#-----------------------------------------------#
	# Count the number of turns the vehicle made      	
	# using the backup compass
	#-----------------------------------------------#

	global turn_backup_old

	set turn_backup_new $angle_bak

	if { $turn_backup_new >= 0 && $turn_backup_new < 90 &&  $turn_backup_old > 270 && $turn_backup_old < 360 } {
   	        set sub_turn_bak [expr ( $sub_turn_bak + 1 ) ]
	} elseif { $turn_backup_new > 270 && $turn_backup_new < 360 &&	$turn_backup_old >= 0 && $turn_backup_old < 90 } {
   		set sub_turn_bak [expr ( $sub_turn_bak - 1 ) ]
   	}

	set turn_backup_old $turn_backup_new



	#-----------------------------------------------#
	# Count the number of turn the ship made
	#-----------------------------------------------#

	set turn_ship_new $ship_angle
	if { $turn_ship_new >= 0 && $turn_ship_new < 90 && $turn_ship_old > 270 && $turn_ship_old < 360 } {
		set ship_turn [expr ( $ship_turn + 1 ) ]
   	} elseif { $turn_ship_new > 270 && $turn_ship_new < 360 && $turn_ship_old >= 0 && $turn_ship_old < 90 } {
		set ship_turn [expr ( $ship_turn - 1 ) ]   	
	}

	set turn_ship_old $turn_ship_new

	#-----------------------------------------------#
	# The switch is turned off
	#-----------------------------------------------#
	if { [get_auto_head_status] <= 0 }  {
		set auto_angle_bak ""
   	}
   	   	
   	   		
	#-----------------------------------------------#
	# Loop again
	#-----------------------------------------------#
	
	global backup_loop_id
	set backup_loop_id [after 200 backup_heading]


# end of procedure "backup_heading"
}



#------------------------------------------------------------------------------#
# This procedure redisplay the graphic after certain amount of time
#------------------------------------------------------------------------------#

proc redisplay_heading { } {
	
	global head_check_var
	global heading_timer  fog_loop_id
	global com_name
	
	#--------------------------------------------#
	# Note : 100 is the type for fog heading  #
	#        1   is the screen number            #
	#--------------------------------------------#

	set temp [split [get_xy_pos 100 1] |]
 	set x [lindex $temp 0]
  	set y [lindex $temp 1]
        #puts "new_compass.tcl: what is fog x y : $temp"
  	

	if {$head_check_var == 1} {
			                                                                              			
        	#---------------------------------------------#
        	# Kill the loop first
        	#---------------------------------------------#
		after cancel fog_loop_id
		set heading_timer 0
			
		set com_name "fog"
		destroy .ventana.editor.fog
		set_compass_pos $x $y
 		display_heading_or_not 1
 		return
   	}
}


proc redisplay_backup_heading { } {
	
	global backup_head_check_var
	global backup_heading_timer
 	global com_name
 	global BACKUP_COMPASS

	#--------------------------------------------#
	# Note : 101 is the type for backup heading  #
	#        1   is the screen number            #
	#--------------------------------------------#

	set temp [split [get_xy_pos 101 1] |]
	set x [lindex $temp 0]
	set y [lindex $temp 1]

	#puts "new_compass.tcl: what is backup x y : $temp"
  	
	if {$backup_head_check_var == 1} {
		set backup_heading_timer 0
		set com_name "backup"
		destroy .ventana.editor.backup_comp
		set_compass_pos $x $y
 		display_heading_or_not 1
 		return
   	}
}

