set cruise2 .auto_cruise2	


proc auto_cruise2 { } {

    #----------------#
    # Declare variables      #
    #----------------#
    global cruise2 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.1

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

    #-----------------#
    # Creating Widgets 	#
    #-----------------#
    label $cruise2.lab40 -text {Auto Cruise} -fg blue -relief groove  -width 30
    label $cruise2.lab33 -text "Foreward Speed"
    label $cruise2.lab34 -text "Lateral Speed"
    
    entry $cruise2.ent34 -width 16 -background grey -textvariable cruise_foreward_speed -state disabled
    entry $cruise2.ent35 -width 16 -background grey -textvariable cruise_lateral_speed -state disabled 
    
    label $cruise2.lab35  -text "Foreward Speed"
    label $cruise2.lablat -text "Lateral Speed"
    label $cruise2.labcm    -text "M/Sec" -font {Halvetica 10}
    label $cruise2.lablatcm -text "M/Sec" -font {Halvetica 10}
    label $cruise2.labcm2   -text "M/Sec" -font {Halvetica 10}
    label $cruise2.lablatcm2 -text "M/Sec" -font {Halvetica 10}
    
    entry $cruise2.ent36 -width 16 -background grey -textvariable cruise_setpoint 
    entry $cruise2.entlat -width 16 -background grey -textvariable cruise_lat_setpoint 
    
    button $cruise2.but37 -text Fore \
     -command {set cruise_setpoint [manipulate_auto_cruise_setpt 2 $auto_cruise_bottom_or_water_lock 0]}
    button $cruise2.but38 -text Aft \
     -command {set cruise_setpoint [manipulate_auto_cruise_setpt -2 $auto_cruise_bottom_or_water_lock 0]}
    button $cruise2.butlatup -text Port -command \
    	{set cruise_lat_setpoint [manipulate_auto_cruise_lat_setpt -2 $auto_cruise_bottom_or_water_lock 0]}
    button $cruise2.butlatdn -text Stbd -command \
    	{set cruise_lat_setpoint [manipulate_auto_cruise_lat_setpt 2 $auto_cruise_bottom_or_water_lock 0]}
    button $cruise2.but39 -text Close -command {destroy $cruise2}
    
    radiobutton $cruise2.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 $cruise2.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 $cruise2.lab40 -x 200 -y 10 -anchor nw -bordermode ignore
    place $cruise2.lab33 -x 10 -y 45 -anchor nw -bordermode ignore
    place $cruise2.lab34 -x 300 -y 45 -anchor nw -bordermode ignore
    
    place $cruise2.ent34 -x 120 -y 45 -anchor nw -bordermode ignore
    place $cruise2.ent35 -x 400 -y 45 -anchor nw -bordermode ignore
    
    place $cruise2.lab35 -x 10 -y 95 -anchor nw -bordermode ignore
    place $cruise2.lablat -x 300 -y 95 -anchor nw -bordermode ignore
    place $cruise2.labcm -x 240 -y 95 -anchor nw -bordermode ignore
    place $cruise2.lablatcm -x 520 -y 95 -anchor nw -bordermode ignore
    place $cruise2.labcm2 -x 240 -y 45 -anchor nw -bordermode ignore
    place $cruise2.lablatcm2 -x 520 -y 45 -anchor nw -bordermode ignore
    
    place $cruise2.ent36 -x 120 -y 95 -anchor nw -bordermode ignore
    place $cruise2.entlat -x 400 -y 95 -anchor nw -bordermode ignore
    place $cruise2.but37 -x 120 -y 125 -width 57 -height 51 -anchor nw -bordermode ignore
    place $cruise2.but38 -x 180 -y 125 -width 62 -height 51 -anchor nw -bordermode ignore
    place $cruise2.butlatup -x 400 -y 125 -width 62 -height 51 -anchor nw -bordermode ignore
    place $cruise2.butlatdn -x 460 -y 125 -width 62 -height 51 -anchor nw -bordermode ignore
    place $cruise2.but39 -x 10 -y 125 -width 62 -height 51 -anchor nw -bordermode ignore
    place $cruise2.water_track_enable -x 10 -y 185 
    place $cruise2.bottom_track_enable -x 160 -y 185 
    
    bind $cruise2.ent36  <Return> { manipulate_auto_cruise_setpt 0 $auto_cruise_bottom_or_water_lock $cruise_setpoint }
    bind $cruise2.entlat <Return> { manipulate_auto_cruise_lat_setpt 0 $auto_cruise_bottom_or_water_lock $cruise_lat_setpoint }
    
    cruise_speed_loop2
}

set auto_cruise_on_old2 0

proc cruise_speed_loop2 { } {
 	global cruise2 cruise_foreward_speed cruise_lateral_speed
 	global cruise_setpoint cruise_lat_setpoint
 	global auto_cruise_on_old2 auto_cruise_on_old
 	global auto_cruise_bottom_or_water_lock
 
 	if ![winfo exists $cruise2] {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_old2 } {
 		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_loop2
 	 	

}
