set cruise .auto_cruise
set cruise_foreward_speed 0
set cruise_lateral_speed 0
set cruise_setpoint 0
set cruise_lat_setpoint 0
set auto_cruise_bottom_or_water_lock 0

proc auto_cruise { } {

    #----------------#
    # Declare variables      #
    #----------------#
    global cruise cruise_setpoint cruise_lat_setpoint
    global auto_cruise_bottom_or_water_lock
    global cruise_foreward_speed cruise_lateral_speed

    
    #----------------#
    # Initialize variables      #
    #----------------#
    	
    set cruise_setpoint [manipulate_auto_cruise_setpt 0 $auto_cruise_bottom_or_water_lock 0]
    set cruise_lat_setpoint [manipulate_auto_cruise_lat_setpt 0 $auto_cruise_bottom_or_water_lock 0]

    #------------------#
    # Create a new window     #
    #------------------#
    global env
    set env(DISPLAY) :0

    if [winfo exists $cruise] {destroy $cruise}
    toplevel $cruise  -width 200m -height 8c
    wm title $cruise "Auto Cruise"

    #-----------------#
    # Creating Widgets 	#
    #-----------------#
    label $cruise.lab40 -text {Auto Cruise} -fg blue -relief groove  -width 30
    label $cruise.lab33 -text "Foreward Speed"
    label $cruise.lab34 -text "Lateral Speed" 
    
    entry $cruise.ent34 -width 16 -background grey -textvariable cruise_foreward_speed -state disabled
    entry $cruise.ent35 -width 16 -background grey -textvariable cruise_lateral_speed -state disabled 
    
    label $cruise.lab35  -text "Foreaft Setpoint"
    label $cruise.lablat -text "Lateral Setpoint"    
    label $cruise.labcm    -text "M/Sec" -font {Halvetica 10}
    label $cruise.lablatcm -text "M/Sec" -font {Halvetica 10}       
    label $cruise.labcm2   -text "M/Sec" -font {Halvetica 10}
    label $cruise.lablatcm2 -text "M/Sec" -font {Halvetica 10}
    
    entry $cruise.ent36 -width 16 -background grey -textvariable cruise_setpoint 
    entry $cruise.entlat -width 16 -background grey -textvariable cruise_lat_setpoint 
    
    button $cruise.but37 -text Fore \
     -command {set cruise_setpoint [manipulate_auto_cruise_setpt 2 $auto_cruise_bottom_or_water_lock 0]}
    button $cruise.but38 -text Aft \
     -command {set cruise_setpoint [manipulate_auto_cruise_setpt -2 $auto_cruise_bottom_or_water_lock 0]}
    button $cruise.butlatup -text Port -command \
        {set cruise_lat_setpoint [manipulate_auto_cruise_lat_setpt -2 $auto_cruise_bottom_or_water_lock 0]}
    button $cruise.butlatdn -text Stbd -command \
         {set cruise_lat_setpoint [manipulate_auto_cruise_lat_setpt 2 $auto_cruise_bottom_or_water_lock 0]}
    button $cruise.but39 -text Close  -command {destroy $cruise}
    
    radiobutton $cruise.bottom_track_enable \
       	-text {Bottom Track} -variable auto_cruise_bottom_or_water_lock -value 0 \
       	-command {
       	if { [get_auto_cruise] } {
       	put_shut_off_auto_cruise
       	} else {
       	set cruise_setpoint [manipulate_auto_cruise_setpt 0 $auto_cruise_bottom_or_water_lock 0]}
       	}
    radiobutton $cruise.water_track_enable \
       	-text {Water Track} -variable auto_cruise_bottom_or_water_lock -value 1 \
    	-command {
       	if { [get_auto_cruise] } {
       	put_shut_off_auto_cruise
       	} else {
       	set cruise_setpoint [manipulate_auto_cruise_setpt 0 $auto_cruise_bottom_or_water_lock 0]}
       	}
       	
    ###################
    # SETTING GEOMETRY #
    #####################
    place $cruise.lab40 -x 200 -y 10 -anchor nw -bordermode ignore
    place $cruise.lab33 -x 10 -y 45 -anchor nw -bordermode ignore
    place $cruise.lab34 -x 300 -y 45 -anchor nw -bordermode ignore
    
    place $cruise.ent34 -x 120 -y 45 -anchor nw -bordermode ignore
    place $cruise.ent35 -x 400 -y 45 -anchor nw -bordermode ignore
    
    place $cruise.lab35 -x 10 -y 95 -anchor nw -bordermode ignore
    place $cruise.lablat -x 300 -y 95 -anchor nw -bordermode ignore
    place $cruise.labcm -x 240 -y 95 -anchor nw -bordermode ignore
    place $cruise.lablatcm -x 520 -y 95 -anchor nw -bordermode ignore 
    place $cruise.labcm2 -x 240 -y 45 -anchor nw -bordermode ignore
    place $cruise.lablatcm2 -x 520 -y 45 -anchor nw -bordermode ignore        
    place $cruise.ent36 -x 120 -y 95 -anchor nw -bordermode ignore
    place $cruise.entlat -x 400 -y 95 -anchor nw -bordermode ignore
    place $cruise.but37 -x 120 -y 125 -width 57 -height 51 -anchor nw -bordermode ignore
    place $cruise.but38 -x 180 -y 125 -width 62 -height 51 -anchor nw -bordermode ignore
    place $cruise.butlatup -x 400 -y 125 -width 57 -height 51 -anchor nw -bordermode ignore
    place $cruise.butlatdn -x 460 -y 125 -width 62 -height 51 -anchor nw -bordermode ignore
    place $cruise.but39 -x 10 -y 125 -width 62 -height 51 -anchor nw -bordermode ignore
    place $cruise.water_track_enable -x 10 -y 185 
    place $cruise.bottom_track_enable -x 160 -y 185 

    bind $cruise.ent36  <Return> { manipulate_auto_cruise_setpt 0 $auto_cruise_bottom_or_water_lock $cruise_setpoint }
    bind $cruise.entlat <Return> { manipulate_auto_cruise_lat_setpt 0 $auto_cruise_bottom_or_water_lock $cruise_lat_setpoint }
    
    #---------------------------------------------------------------#
    # This will update the vehicle speed value
    #---------------------------------------------------------------#

    cruise_speed_loop

}


#---------------------------------------------------------------#
# This function will loop in order to update the vehicle speed
# coming from DVL system
#---------------------------------------------------------------#

set auto_cruise_on_old 0

proc cruise_speed_loop { } {
 	global cruise cruise_foreward_speed cruise_lateral_speed
 	global cruise_setpoint cruise_lat_setpoint
 	global auto_cruise_on_old auto_cruise_on2
 	global auto_cruise_bottom_or_water_lock
 	
 	if ![winfo exists $cruise] {return}
 	
 	set cd [get_vehicle_speed]
		
	set cd_list [ split $cd | ]
 	
 	set cruise_foreward_speed [lindex $cd_list 0]
 	set cruise_lateral_speed [lindex $cd_list 1]
 	
 	set auto_cruise_on [get_auto_cruise]
 	if { $auto_cruise_on != $auto_cruise_on_old } {
 		set cruise_setpoint [manipulate_auto_cruise_setpt 0 $auto_cruise_bottom_or_water_lock 0]
 		set cruise_lat_setpoint [manipulate_auto_cruise_lat_setpt 0 $auto_cruise_bottom_or_water_lock 0]
 		set auto_cruise_on_old $auto_cruise_on
 		set auto_cruise_on_old2 $auto_cruise_on
 		}
 	after 200 cruise_speed_loop
 	 	

}

set shut_off_autocruise .shut_off_autocruise
proc put_shut_off_auto_cruise { } {
	
	global shut_off_autocruise
	global env
	set env(DISPLAY) :0
	
	if [winfo exists $shut_off_autocruise] {destroy $shut_off_autocruise}	
			
	toplevel $shut_off_autocruise
	wm title $shut_off_autocruise "Warning Pilot Proceedure Required"
	label $shut_off_autocruise.lable -text "Cycle the Auto Cruise after change to send this to the Sub"
			
	button $shut_off_autocruise.ok -text "Ok" -command { destroy $shut_off_autocruise	} \
		-width 5 -height 2
			
	button $shut_off_autocruise.cancel -text "Cancel" -command {destroy $shut_off_autocruise } \
		-width 5 -height 2	
			
	pack $shut_off_autocruise.lable -side top -pady 2m -padx 2m
	pack $shut_off_autocruise.ok -side left  -pady 2m -padx 2m
	pack $shut_off_autocruise.cancel -side left  -pady 2m -padx 2m	

}


