#------------------------------------------------------------------------------#
# 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_name2 ""
set v2hframe ""
set head2_check_var 0
set heading2_timer 0
set cover_head2_id2 0
set head2_id2 0
set auto_head2_id2 0
set circle_id2 0
set north_id2 0
set south_id2 0
set east_id2 0
set west_id2 0
set nw_id2 0
set ne_id2 0
set sw_id2 0
set se_id2 0
set black_circle_id2 0
set fog_loop_id2 0
set turn_heading_old2 0
set turn_backup_old2 0
set BACKUP_COMPASS 101
set angle2 0
set angle_bak2 0
set sub_turn2 0
set sub_turn_bak2 0
set turn_ship_old2 0

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

set cover_head2_bak_id2 0
set head2_bak_id2 0
set auto_head2_bak_id2 0
set circle_bak_id2 0
set north_bak_id2 0
set south_bak_id2 0
set east_bak_id2 0
set west_bak_id2 0
set nw_bak_id2 0
set ne_bak_id2 0
set sw_bak_id2 0
set se_bak_id2 0
set backup_loop_id2 0
set backup_head2_check_var 0
set backup_heading2_timer 0
set black_circle_bak_id2 0

set primary_source_title2 "Fog"
set backup_source_title2 "KVH"

#------------------------------------------------------------------------------#
# 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_id2($i) 0
	set lgreen_bak_id2($i) 0
	set white_mark_id2($i) 0
	
}



#------------------------------------------------#
# This procedure convert angle from degree to radiant                   #
#------------------------------------------------#

proc conv_to_rad { rad_angle } {
    global pi
    expr { $pi / 180 * $rad_angle }
}



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

proc set_compass2_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_compass2_window_path_name { name } {

	global com_name2 v2hframe
	
	set com_name2 $name
	set v2hframe .ventana2.editor.$com_name2

}



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

proc display_heading2_or_not { flag } {
		
	global compx
	global compy
	global head2_surveh
	global head2_diganal
	global head2_elem_num
	global v2hframe
	global head2_check_var backup_head2_check_var
	global com_name2
	global size

	#-----------------------------------------------#
 	# set the frame to be the current compass type
 	#-----------------------------------------------#
	set v2hframe .ventana2.editor.$com_name2
	
	if {$flag == 1} {
		if [winfo exists $v2hframe] { destroy $v2hframe }
		
		if { $com_name2 == "fog" } {
			set head2_check_var 1
		} else {
			set backup_head2_check_var 1
		}
		
		frame $v2hframe -background black -width 147 -height 280
		place $v2hframe -x $compx -y $compy
		
   		#-----------------------------------------------#
   		# Create the canvas to draw the compass
   		#-----------------------------------------------#
		set v2c $v2hframe.compass
		set size_oval [expr { 2 * $size }]
		canvas $v2c -width [expr {$size_oval + 50}] -height [expr {$size_oval + 180}] \
			-background black
		place $v2c -x -1 -y -1

		#-----------------------------------------------#
  		# Draw the graphic parts that are static
  		# Draw the head -> which will recursively call
    		# itself again to redraw the position
    		#-----------------------------------------------#
    	    	draw_static_graphic2  	
    	    	
   		if { $com_name2 == "fog" } {        	    			
			heading2
		} else {
   			backup_heading2
   		}	
   	
	} else {
		
		#-----------------------------------------------#
		# This will return from the graphic loop
		# (that redraw the canvas for update)
		#-----------------------------------------------#
				
		if { $com_name2 == "fog" } {    		
    	    		set head2_check_var 0
 		} else {
 			set backup_head2_check_var 0	
 		}
 		
 		destroy $v2hframe		
    	}                                          	
}



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


proc draw_static_graphic2 { } {

	#-----------------------------------------------#
	# Declare and initialize variables		
	#-----------------------------------------------#
	global v2hframe
	global com_name2
	global size
        global primary_source_title2 backup_source_title2
	
	set size_oval [expr { 2 * $size }]

	#-----------------------------------------------#
	# Set canvas v2c
	#-----------------------------------------------#
	set v2c $v2hframe.compass

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


	if {$com_name2 == "fog" } {


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

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

	# end if 	
	} else {
   	
		#-----------------------------------------------#
		# Text value for heading
		#-----------------------------------------------#
		label $v2c.headlabel -fg white -bg black -textvariable backup_source_title2
		entry $v2c.heading2 -bg black -fg white -state disabled -relief flat -width 10 \
   			-borderwidth 0 -textvariable angle_bak2
		place $v2c.headlabel -x [expr {$size - 45 }] -y [expr {$size_oval + 55}]	
		place $v2c.heading2 -x [expr {$size + 15 }] -y [expr {$size_oval + 55}]
   	
		#-----------------------------------------------#
		# Text value for auto heading2
		#-----------------------------------------------#
		label $v2c.auto_headlabel -fg white -bg black -text "Auto "
		entry $v2c.auto_heading2 -bg black -fg white -state disabled -relief flat -width 10 \
			-borderwidth 0 -textvariable auto_angle_bak
		place $v2c.auto_headlabel -x [expr {$size - 45 }] -y [expr {$size_oval + 95}]	
		place $v2c.auto_heading2 -x [expr {$size + 15 }] -y [expr {$size_oval + 95}]
	
		#-----------------------------------------------#
		# Ship Vehicle display
		#-----------------------------------------------#

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

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

	
	# end else
	}




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

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

	place $v2c.ship_turn_lab -x [expr {$size + 40 }] -y [expr {$size_oval + 115}]
	place $v2c.ship_turn_arr -x [expr {$size + 40 }] -y [expr {$size_oval + 125}]
 	place $v2c.ship_turn -x [expr {$size + 64 }] -y [expr {$size_oval + 115}]
   	


        	
# end procedure
}



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



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

proc heading2 { } {

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

	global com_name2
	global hc head2_id2 ship_id2 auto_head2_id2 black_circle_id2
	global head2_check_var  heading2_timer
	global angle2 ship_angle auto_angle sub_turn2
	global lgreen_id2 circle_id2 cover_head2_id2
	global north_id2 south_id2 east_id2 west_id2
	global turns_heading_new2 turns_heading_old2 turn_ship_old2 turn_ship_new2
	global size ship_turn2 auto_angle_dvl
        global fog_deviation fog_declination compass_deviation
      	global head_source_input primary_source_title2
	
	
	#-----------------------------------------------#
	# Get the angle for heading from serial port
	#-----------------------------------------------#
	
	set hist [split [get_head_hist] |]
	if { $head_source_input == "FOG GYRO" } {
		set primary_source_title2 "Fog"
		if { ($fog_deviation != "") && ($fog_deviation != "-") } {
	        	set temp_angle [lindex [ split [expr [get_head] + $fog_deviation] . ] 0]
			set angle2 [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 angle2 [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_title2 "KVH"
		if { ($compass_deviation > -1) && ($compass_deviation < 1000) } {	
	        	set temp_angle [lindex [ split [expr [get_aux_heading] + $compass_deviation] . ] 0]
			set angle2 [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 angle2 [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_title2 "DVL"
        	set temp_angle [get_dvl_heading]
        	set angle2 [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 v2c
	#-----------------------------------------------#

	set v2c .ventana2.editor.fog.compass

	#-----------------------------------------------#
	# Check if the button name has already exists
	#-----------------------------------------------#
	
	if [ winfo exists $v2c] {
   	
		$v2c delete $head2_id2
		$v2c delete $auto_head2_id2
		$v2c delete $black_circle_id2
		$v2c delete $circle_id2
		$v2c delete $cover_head2_id2
		$v2c delete $north_id2
		$v2c delete $south_id2
		$v2c delete $west_id2
		$v2c delete $east_id2
		
   	
		for {set count 0} {$count <= 360 } {incr count } {
			$v2c delete $lgreen_id2($count)
		}

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

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

	#-----------------------------------------------#
	# update the timer
	#-----------------------------------------------#
	incr heading2_timer
	if { $heading2_timer >= 100 } {
		redisplay_heading2
		return
	}


	#-----------------------------------------------#
	# x y line coordinates for heading2
	#-----------------------------------------------#
	set x [expr { $size1* [my_sin -$angle2] }]
	set y [expr { $size1* [my_cos $angle2] }]
	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 heading2     	
	#-----------------------------------------------#
	set auto_teta_display [expr {$angle2 - $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 lines 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 + $angle2)}] ] }]
		set yl [expr { ($size + $len) * [my_cos [expr {($i + $angle2)}] ] }]
		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_id2($i) [$v2c create line $xl1 $yl1 $xl2 $yl2  -fill green ]
		} else {
			set lgreen_id2($i) [$v2c create line $xl1 $yl1 $xl2 $yl2 -fill blue ]
 		}


	# end for loop
	}


	set xl [expr { ($size + $len + 10 ) * [my_sin -$angle2] }]
	set yl [expr { ($size + $len + 10 ) * [my_cos $angle2] }]
	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 {-($angle2 + 90) }]] }]
	set ye [expr { ($size + $len + 10 ) * [my_cos [expr {($angle2 + 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_id2 [$v2c create text $xl1 $yl1 -text N  -fill green]
	set east_id2 [ $v2c create text  $xe1 $ye1   -text W -fill green ]	
	set south_id2 [ $v2c create text  $xl2 $yl2  -text S -fill green ]
	set west_id2 [ $v2c create text  $xe2 $ye2   -text E -fill green ]


	#-----------------------------------------------#
	# Draw the heading2 and auto heading2
	#-----------------------------------------------#
	set head2_id2 [$v2c create line 75 15 75 75 -arrow first -width 3 -fill green ]

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

	#-----------------------------------------------#
	# Draw the inner blue circle
	#-----------------------------------------------#
	set circle_id2 [$v2c 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_id2 [$v2c 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 }
           	
		#---------------------------------------#
		# x y line coordinates for history     	
		#---------------------------------------#

		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) [$v2c create oval $xhist1 $yhist1 $xhist2 $yhist2 -fill white]
     	                            	
		incr count	
		incr temp -1                                                           	
	
	# end for loop
	}


	#-----------------------------------------------#
	# Count the number of sub_turn the vehicle made      	
	#-----------------------------------------------#
	
	set turn_heading_new2 $angle2
	if { $turn_heading_new2 > 0 && $turn_heading_new2 < 90 &&
		$turn_heading_old2 > 270 && $turn_heading_old2 < 360 } {
   	
		set sub_turn2 [expr ( $sub_turn2 + 1 ) ]
   	
	} elseif { $turn_heading_new2 > 270 && $turn_heading_new2 < 360 &&
			$turn_heading_old2 > 0 && $turn_heading_old2 < 90 } {
   	
		set sub_turn2 [expr ( $sub_turn2 - 1 ) ]   	
	}

	set turn_heading_old2 $turn_heading_new2
	

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

   	set turn_ship_new2 $ship_angle
   	if { $turn_ship_new2 >= 0 && $turn_ship_new2 < 90 &&
   		$turn_ship_old2 > 270 && $turn_ship_old2 < 360 } {
   	
   	set ship_turn2 [expr ( $ship_turn2 + 1 ) ]
   	
   	} elseif { $turn_ship_new2 > 270 && $turn_ship_new2 < 360 &&
   		     $turn_ship_old2 >= 0 && $turn_ship_old2 < 90 } {
   	
   		set ship_turn2 [expr ( $ship_turn2 - 1 ) ]   	
   	}

   	set turn_ship_old2 $turn_ship_new2



	#----------------------------#
	# The switch is turned off
	#----------------------------#

	if { [get_auto_head_status] <= 0 }  {
		set auto_angle ""
	}

	#-----------------------------------------------#
	# Loop again
	#-----------------------------------------------#
	
	global fog_loop_id2
	set fog_loop_id2 [after 200 heading2]


# end of procedure "heading2"
}



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




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

proc backup_heading2 { } {

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

	global com_name2
	
	global hcb head2_bak_id2 auto_head2_bak_id2 black_circle_bak_id2
	global backup_head2_check_var backup_heading2_timer
	global angle_bak2 ship_angle auto_angle_bak sub_turn_bak2
	global lgreen_bak_id2 circle_bak_id2 cover_head2_bak_id2
	global north_bak_id2 south_bak_id2 east_bak_id2 west_bak_id2
	global turn_backup_old2 turn_backup_new2 turn_ship_old2 turn_ship_new2
 	global ship_turn2 auto_angle_dvl
 	global compass_deviation compass_declination fog_deviation fog_declination
 	global sec_head_source_input backup_source_title2
 	
	#-----------------------------------------------#
	# Get the angle for backup heading from C++
	#-----------------------------------------------#
        set temp_angle [get_aux_heading] 	
	if { $sec_head_source_input == "FOG GYRO" } {
		set backup_source_title2 "Fog"
		if { ($fog_deviation != "") && ($fog_deviation != "-") } {
	        	set temp_angle [lindex [ split [expr [get_head] + $fog_deviation] . ] 0]
			set angle_bak2 [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_bak2 [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_title2 "KVH"
		if { ($compass_deviation > -1) && ($compass_deviation < 1000) } {	
	        	set temp_angle [lindex [ split [expr [get_aux_heading] + $compass_deviation] . ] 0]
			set angle_bak2 [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_bak2 [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_title2 "DVL"
        	set temp_angle [get_dvl_heading]
        	set angle_bak2 [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 v2c
	#-----------------------------------------------#
	set v2c .ventana2.editor.backup_comp.compass


	#-----------------------------------------------#
	# Check if the button name has already exists
	#-----------------------------------------------#
	
	if [ winfo exists $v2c] {

		$v2c delete $head2_bak_id2
		$v2c delete $auto_head2_bak_id2
		$v2c delete $black_circle_bak_id2
		$v2c delete $circle_bak_id2
		$v2c delete $cover_head2_bak_id2
		$v2c delete $north_bak_id2
		$v2c delete $south_bak_id2
		$v2c delete $west_bak_id2
		$v2c delete $east_bak_id2
   	
		for {set count 0} {$count <= 360 } {incr count } {
			$v2c delete $lgreen_bak_id2($count)
		}

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

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

	#-----------------------------------------------#
	# update the timer
	#-----------------------------------------------#
	
	incr backup_heading2_timer
	if { $backup_heading2_timer >= 200 } {
		redisplay_backup_heading2
		return
	}

	#-----------------------------------------------#
	# set 'teta' and 'auto_teta' angle_bak in radian
	#-----------------------------------------------#
	set teta [conv_to_rad $angle_bak2]
	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 lines for every 10 degree angle
	#-----------------------------------------------#
	
	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_id2($i) [$v2c create line $xl1 $yl1 $xl2 $yl2  -fill green ]
		} else {
			set lgreen_bak_id2($i) [$v2c 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_id2 [$v2c create text $xl1 $yl1 -text N  -fill green]
	set east_bak_id2 [ $v2c create text  $xe1 $ye1   -text W -fill green ]
	set south_bak_id2 [ $v2c create text  $xl2 $yl2  -text S -fill green ]
	set west_bak_id2 [ $v2c create text  $xe2 $ye2   -text E -fill green ]


	#-----------------------------------------------#
	# Draw the heading2 and auto heading2
	#-----------------------------------------------#
	
	set head2_bak_id2 [$v2c create line 75 15 75 75 -arrow first -width 3 -fill green ]
	
	if { [get_auto_head_status] > 0 }  {
		set auto_head2_bak_id2 [$v2c create line $autox1 $autoy1 75 75  -arrow first -width 3 -fill red ]
	}

	#-----------------------------------------------#
	# Draw the inner blue circle
	#-----------------------------------------------#
	set circle_bak_id2 [$v2c 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_id2 [$v2c 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 sub_turn the vehicle made      	
	#-----------------------------------------------#
	
	set turn_backup_new2 $angle_bak2
	if { $turn_backup_new2 >= 0 && $turn_backup_new2 < 90 
		&& $turn_backup_old2 > 270 && $turn_backup_old2 <= 360 } {
   	        	set sub_turn_bak2 [expr ( $sub_turn_bak2 + 1 ) ]
   	} elseif { $turn_backup_new2 > 270 && $turn_backup_new2 <= 360 && 
   			$turn_backup_old2 >= 0 && $turn_backup_old2 < 90 } {
   			set sub_turn_bak2 [expr ( $sub_turn_bak2 - 1 ) ]   	
	}

	set turn_backup_old2 $turn_backup_new2


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

	set turn_ship_new2 $ship_angle
	if { $turn_ship_new2 >= 0 && $turn_ship_new2 < 90 && $turn_ship_old2 > 270 && $turn_ship_old2 < 360 } {
   		set ship_turn2 [expr ( $ship_turn2 + 1 ) ]
  	} elseif { $turn_ship_new2 > 270 && $turn_ship_new2 < 360 && $turn_ship_old2 >= 0 && $turn_ship_old2 < 90 } {
   		set ship_turn2 [expr ( $ship_turn2 - 1 ) ]   	
	}

   	set turn_ship_old2 $turn_ship_new2


	#-----------------------------------------------#
	# The switch is turned off
	#-----------------------------------------------#

	if { [get_auto_head_status] <= 0 }  {
		set auto_angle_bak ""
	}
     	
	#-----------------------------------------------#
	# Loop again
	#-----------------------------------------------#
	
	global backup_loop_id2
	set backup_loop_id2 [after 500 backup_heading2]

	
	
# end of procedure "backup_heading2"
}




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

proc redisplay_heading2 { } {
	
	global head2_check_var
	global heading2_timer fog_loop_id2
	global com_name2

	#-----------------------------------------------#
	# Note : 100 is the type for fog heading
    	#        2   is the screen number
    	#-----------------------------------------------#

	set temp [split [get_xy_pos 100 2] |]
	set x [lindex $temp 0]
	set y [lindex $temp 1]
	
	if {$head2_check_var == 1} {
	
		#-----------------------------------------------#
		# Kill the loop first
		#-----------------------------------------------#
		
		after cancel fog_loop_id2
		set heading2_timer 0    		
		
		#-----------------------------------------------#
		# Restart the loop again
		#-----------------------------------------------#
		
		set com_name2 "fog"
		destroy .ventana2.editor.fog
		set_compass2_pos $x $y
		display_heading2_or_not 1
 		
		return
	
	# end if	
	}                  	
	
# end proc
}


proc redisplay_backup_heading2 { } {

	global backup_head2_check_var
	global backup_heading2_timer
	global com_name2 BACKUP_COMPASS
	global backup_loop_id2

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

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

	if {$backup_head2_check_var == 1} {
		
		#-----------------------------------------------#
		# Kill the loop first
		#-----------------------------------------------#

		after cancel backup_loop_id2
		set backup_heading2_timer 0
		
		#-----------------------------------------------#
		# Restart the loop again
		#-----------------------------------------------#
	
		set com_name2 "backup_comp"
		destroy .ventana2.editor.backup_comp
		set_compass2_pos $x $y
		display_heading2_or_not 1
 		
		return
   	
	# end if
	}

# end proc
}


