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

source /home/ventana/source/submenu/analog_input_list2.tcl
source /home/ventana/source/submenu/analog_output_list2.tcl

#-----------------------------------
# Initialize and set variable
#-----------------------------------
set arm2 .arm_control_popup2
set arm_timer2 0
set arm_string ""
set old_arm_string ""




set shoulder_sine "Analog Input 1"
set shoulder_cosine "Analog Input 2"
set shoulder_offset 0
set shoulder_command "Analog Input 13"
set shoulder_servo "Analog Output 4"
set p_shoulder 0
set i_shoulder 0
set d_shoulder 0
set swing_sine "Analog Input 3"
set swing_cosine "Analog Input 4"
set swing_offset 0
set swing_command "Analog Input 12"
set swing_servo "Analog Output 5"
set p_swing 0
set i_swing 0
set d_swing 0
set elbow_sine "Analog Input 5"
set elbow_cosine "Analog Input 6"
set elbow_offset 0
set elbow_command "Analog Input 14"
set elbow_servo "Analog Output 6"
set p_elbow 0
set i_elbow 0
set d_elbow 0
set pitch_sine "Analog Input 7"
set pitch_cosine "Analog Input 8"
set pitch_offset 0
set pitch_command "Analog Input 15"
set pitch_servo "Analog Output 7"
set p_pitch 0
set i_pitch 0
set d_pitch 0
set yaw_sine "Analog Input 9"
set yaw_cosine "Analog Input 10"
set yaw_offset 0
set yaw_command "Analog Input 16"
set yaw_servo "Analog Output 8"
set p_yaw 0
set i_yaw 0
set d_yaw 0

proc arm_control_popup2 { } {

    global arm2
    global arm_timer2

    if [winfo exists $arm2] {destroy $arm2}

    global env
    set env(DISPLAY) :0.1
    toplevel $arm2 -width 32c -height 20c
    wm title $arm2 "Arm Control"

    label $arm2.labarmtitle \
        -foreground #0000b0 -height 0 -relief ridge -text {Arm Control} \
        -width 511
    button $arm2.butok \
        -command {set arm_timer2 0; send_down_arm2; destroy $arm2} -height 26 -text OK -width 53
    label $arm2.labshoulder \
        -text Shoulder
    label $arm2.labswing \
        -text Swing
    label $arm2.labelbow \
        -text Elbow
    label $arm2.labpitch \
        -text Pitch
    label $arm2.labyaw \
        -text Yaw


###############
# SHOULDER    #
###############
    frame $arm2.shoulder_frame \
        -borderwidth 2 -height 95 -relief groove -width 680
    label $arm2.shoulder_frame.labsine \
        -text Sine
    label $arm2.shoulder_frame.labcosine \
        -text Cosine
    label $arm2.shoulder_frame.laboffset \
        -text Offset
    entry $arm2.shoulder_frame.entsine \
        -background white -textvariable shoulder_sine -width 13 -state disabled
    entry $arm2.shoulder_frame.entcosine \
        -background white -textvariable shoulder_cosine -width 13  -state disabled
    entry $arm2.shoulder_frame.entoffset \
        -background white -textvariable shoulder_offset -width 13

    label $arm2.shoulder_frame.labcommand \
        -text Command
    label $arm2.shoulder_frame.labmaster \
        -text Master
    label $arm2.shoulder_frame.labfeedback \
        -text FeedBack
    entry $arm2.shoulder_frame.entcommand \
        -background white -textvariable shoulder_command -width 13 -state disabled
    entry $arm2.shoulder_frame.entmaster \
        -background #d9d9d9 -textvariable shoulder_master_value -width 13
    entry $arm2.shoulder_frame.entfeedback \
        -background #d9d9d9 -textvariable shoulder_fb_value -width 13

    label $arm2.shoulder_frame.labservo \
        -text Servo
    entry $arm2.shoulder_frame.entservo \
        -background white -textvariable shoulder_servo -width 13 -state disabled

    label $arm2.shoulder_frame.plabel \
        -text P
    label $arm2.shoulder_frame.ilabel \
        -text I
    label $arm2.shoulder_frame.dlabel \
        -text D
    entry $arm2.shoulder_frame.entp \
        -background white -textvariable p_shoulder -width 5
    entry $arm2.shoulder_frame.enti \
        -background white -textvariable i_shoulder -width 5
    entry $arm2.shoulder_frame.entd \
        -background white -textvariable d_shoulder -width 5

    label $arm2.shoulder_frame.labtimeconst \
        -text "Time Const"
    label $arm2.shoulder_frame.labtimeconstunits \
        -text "X 20mS"
    entry $arm2.shoulder_frame.enttimeconst \
        -background white -textvariable arm_time_constant -width 10

    button $arm2.shoulder_frame.butsine \
        -command { analog_input_list2 $arm2.shoulder_frame.entsine }  -height 21 -text v -width 27
    button $arm2.shoulder_frame.butcosine \
        -command { analog_input_list2 $arm2.shoulder_frame.entcosine }  -height 21 -text v -width 27
    button $arm2.shoulder_frame.butcommand \
        -command { surf_analog_input_list2 $arm2.shoulder_frame.entcommand }  -height 21 -text v -width 27
    button $arm2.shoulder_frame.butservo \
        -command { analog_output_list2 $arm2.shoulder_frame.entservo }  -height 21 -text v -width 27

##############
# SWING      #
##############
    frame $arm2.swing_frame \
        -borderwidth 2 -height 95 -relief groove -width 680
    label $arm2.swing_frame.labsine \
        -text Sine
    label $arm2.swing_frame.labcosine \
        -text Cosine
    label $arm2.swing_frame.laboffset \
        -text Offset
    entry $arm2.swing_frame.entsine \
        -background white -textvariable swing_sine -width 13 -state disabled
    entry $arm2.swing_frame.entcosine \
        -background white -textvariable swing_cosine -width 13 -state disabled
    entry $arm2.swing_frame.entoffset \
        -background white -textvariable swing_offset -width 13

    label $arm2.swing_frame.labcommand \
        -text Command
    label $arm2.swing_frame.labmaster \
        -text Master
    label $arm2.swing_frame.labfeedback \
        -text Feedback
    entry $arm2.swing_frame.entcommand \
        -background white -textvariable swing_command -width 13 -state disabled
    entry $arm2.swing_frame.entmaster \
        -background #d9d9d9 -textvariable swing_master_value -width 13
    entry $arm2.swing_frame.entfeedback \
        -background #d9d9d9 -textvariable swing_fb_value -width 13

    label $arm2.swing_frame.labservo \
        -text Servo
    entry $arm2.swing_frame.entservo \
        -background white -textvariable swing_servo -width 14 -state disabled

    label $arm2.swing_frame.plabel \
        -text P
    label $arm2.swing_frame.ilabel \
        -text I
    label $arm2.swing_frame.dlabel \
        -text D
    entry $arm2.swing_frame.entp \
        -background white -textvariable p_swing -width 5
    entry $arm2.swing_frame.enti \
        -background white -textvariable i_swing -width 5
    entry $arm2.swing_frame.entd \
        -background white -textvariable d_swing -width 5

    button $arm2.swing_frame.butsine \
        -command { analog_input_list2 $arm2.swing_frame.entsine }  -height 21 -text v -width 27
    button $arm2.swing_frame.butcosine \
        -command { analog_input_list2 $arm2.swing_frame.entcosine }  -height 21 -text v -width 27
    button $arm2.swing_frame.butcommand \
        -command { surf_analog_input_list2 $arm2.swing_frame.entcommand }  -height 21 -text v -width 27
    button $arm2.swing_frame.butservo \
        -command { analog_output_list2 $arm2.swing_frame.entservo }  -height 21 -text v -width 27

##############
# ELBOW	 #
##############
    frame $arm2.elbow_frame \
        -borderwidth 2 -height 95 -relief groove -width 680
    label $arm2.elbow_frame.labsine \
        -text Sine
    label $arm2.elbow_frame.labcosine \
        -text Cosine
    label $arm2.elbow_frame.laboffset \
        -text Offset
    entry $arm2.elbow_frame.entsine \
        -background white -textvariable elbow_sine -width 13  -state disabled
    entry $arm2.elbow_frame.entcosine \
        -background white -textvariable elbow_cosine -width 13  -state disabled
    entry $arm2.elbow_frame.entoffset \
        -background white -textvariable elbow_offset -width 13

    label $arm2.elbow_frame.labcommand \
        -text Command
    label $arm2.elbow_frame.labmaster \
         -text Master
    label $arm2.elbow_frame.labfeedback \
         -text Feedback
    entry $arm2.elbow_frame.entcommand \
        -background white -textvariable elbow_command -width 13 -state disabled
    entry $arm2.elbow_frame.entmaster \
        -background #d9d9d9 -textvariable elbow_master_value -width 13
    entry $arm2.elbow_frame.entfeedback \
        -background #d9d9d9 -textvariable elbow_fb_value -width 13

    label $arm2.elbow_frame.labservo \
        -text Servo
    entry $arm2.elbow_frame.entservo \
        -background white -textvariable elbow_servo -width 14  -state disabled

    label $arm2.elbow_frame.plabel \
        -text P
    label $arm2.elbow_frame.ilabel \
        -text I
    label $arm2.elbow_frame.dlabel \
        -text D
    entry $arm2.elbow_frame.entp \
        -background white -textvariable p_elbow -width 5
    entry $arm2.elbow_frame.enti \
        -background white -textvariable i_elbow -width 5
    entry $arm2.elbow_frame.entd \
        -background white -textvariable d_elbow -width 5

    button $arm2.elbow_frame.butsine \
        -command { analog_input_list2 $arm2.elbow_frame.entsine }  -height 21 -text v -width 27
    button $arm2.elbow_frame.butcosine \
        -command { analog_input_list2 $arm2.elbow_frame.entcosine }  -height 21 -text v -width 27
    button $arm2.elbow_frame.butcommand \
        -command { surf_analog_input_list2 $arm2.elbow_frame.entcommand }  -height 21 -text v -width 27
    button $arm2.elbow_frame.butservo \
        -command { analog_output_list2 $arm2.elbow_frame.entservo }  -height 21 -text v -width 27

##############
# PITCH      #
##############
    frame $arm2.pitch_frame \
        -borderwidth 2 -height 95 -relief groove -width 680
    label $arm2.pitch_frame.labsine \
        -text Sine
    label $arm2.pitch_frame.labcosine \
        -text Cosine
    label $arm2.pitch_frame.laboffset \
        -text Offset
    entry $arm2.pitch_frame.entsine \
        -background white -textvariable pitch_sine -width 13 -state disabled
    entry $arm2.pitch_frame.entcosine \
        -background white -textvariable pitch_cosine -width 13 -state disabled
    entry $arm2.pitch_frame.entoffset \
        -background white -textvariable pitch_offset -width 13

    label $arm2.pitch_frame.labcommand \
        -text Command
    label $arm2.pitch_frame.labmaster \
        -text Master
    label $arm2.pitch_frame.labfeedback \
        -text Feedback
    entry $arm2.pitch_frame.entcommand \
        -background white -textvariable pitch_command -width 13 -state disabled
    entry $arm2.pitch_frame.entmaster \
        -background #d9d9d9 -textvariable pitch_master_value -width 13
    entry $arm2.pitch_frame.entfeedback \
        -background #d9d9d9 -textvariable pitch_fb_value -width 13

    label $arm2.pitch_frame.labservo \
        -text Servo
    entry $arm2.pitch_frame.entservo \
        -background white -textvariable pitch_servo -width 14 -state disabled

    label $arm2.pitch_frame.plabel \
        -text P
    label $arm2.pitch_frame.ilabel \
        -text I
    label $arm2.pitch_frame.dlabel \
        -text D
    entry $arm2.pitch_frame.entp \
        -background white -textvariable p_pitch -width 5
    entry $arm2.pitch_frame.enti \
        -background white -textvariable i_pitch -width 5
    entry $arm2.pitch_frame.entd \
        -background white -textvariable d_pitch -width 5

    button $arm2.pitch_frame.butsine \
        -command { analog_input_list2 $arm2.pitch_frame.entsine }  -height 21 -text v -width 27
    button $arm2.pitch_frame.butcosine \
        -command { analog_input_list2 $arm2.pitch_frame.entcosine }  -height 21 -text v -width 27
    button $arm2.pitch_frame.butcommand \
        -command { surf_analog_input_list2 $arm2.pitch_frame.entcommand }  -height 21 -text v -width 27
    button $arm2.pitch_frame.butservo \
        -command { analog_output_list2 $arm2.pitch_frame.entservo }  -height 21 -text v -width 27

###############
# YAW         #
###############
    frame $arm2.yaw_frame \
        -borderwidth 2 -height 95 -relief groove -width 680
    label $arm2.yaw_frame.labsine \
        -text Sine
    label $arm2.yaw_frame.labcosine \
        -text Cosine
    label $arm2.yaw_frame.laboffset \
        -text Offset
    entry $arm2.yaw_frame.entsine \
        -background white -textvariable yaw_sine -width 13 -state disabled
    entry $arm2.yaw_frame.entcosine \
        -background white -textvariable yaw_cosine -width 13 -state disabled
    entry $arm2.yaw_frame.entoffset \
        -background white -textvariable yaw_offset -width 13

    label $arm2.yaw_frame.labcommand \
        -text Command
    label $arm2.yaw_frame.labmaster \
        -text Master
    label $arm2.yaw_frame.labfeedback \
        -text Feedback
    entry $arm2.yaw_frame.entcommand \
        -background white -textvariable yaw_command -width 13 -state disabled
    entry $arm2.yaw_frame.entmaster \
        -background #d9d9d9 -textvariable yaw_master_value -width 13
    entry $arm2.yaw_frame.entfeedback \
        -background #d9d9d9 -textvariable yaw_fb_value -width 13

    label $arm2.yaw_frame.labservo \
        -text Servo
    entry $arm2.yaw_frame.entservo \
        -background white -textvariable yaw_servo -width 14 -state disabled

    label $arm2.yaw_frame.plabel \
        -text P
    label $arm2.yaw_frame.ilabel \
        -text I
    label $arm2.yaw_frame.dlabel \
        -text D
    entry $arm2.yaw_frame.entp \
        -background white -textvariable p_yaw -width 5
    entry $arm2.yaw_frame.enti \
        -background white -textvariable i_yaw -width 5
    entry $arm2.yaw_frame.entd \
        -background white -textvariable d_yaw -width 5

    button $arm2.yaw_frame.butsine \
        -command { analog_input_list2 $arm2.yaw_frame.entsine }  -height 21 -text v -width 27
    button $arm2.yaw_frame.butcosine \
        -command { analog_input_list2 $arm2.yaw_frame.entcosine }  -height 21 -text v -width 27
    button $arm2.yaw_frame.butcommand \
        -command { surf_analog_input_list2 $arm2.yaw_frame.entcommand }  -height 21 -text v -width 27
    button $arm2.yaw_frame.butservo \
        -command { analog_output_list2 $arm2.yaw_frame.entservo }  -height 21 -text v -width 27

    #-----------------------------------------------------

    ###################
    # SETTING GEOMETRY
    ###################


    place $arm2.labarmtitle \
        -x 10 -y 10 -width 760 -height 20 -anchor nw -bordermode ignore

    place $arm2.shoulder_frame \
        -x 80 -y 60 -anchor nw -bordermode ignore
    place $arm2.swing_frame \
        -x 80 -y 160 -anchor nw -bordermode ignore
    place $arm2.elbow_frame \
        -x 80 -y 260 -anchor nw -bordermode ignore
    place $arm2.pitch_frame \
        -x 80 -y 360 -anchor nw -bordermode ignore
    place $arm2.yaw_frame \
        -x 80 -y 460 -anchor nw -bordermode ignore

    place $arm2.labshoulder \
        -x 5 -y 80 -anchor nw -bordermode ignore
    place $arm2.labswing \
        -x 5 -y 180 -anchor nw -bordermode ignore
    place $arm2.labelbow \
        -x 5 -y 280 -anchor nw -bordermode ignore
    place $arm2.labpitch \
        -x 5 -y 380 -anchor nw -bordermode ignore
    place $arm2.labyaw \
        -x 5 -y 480 -anchor nw -bordermode ignore

############
# Shoulder #
############

###### Shoulder COLUMN 1
    place $arm2.shoulder_frame.labsine \
        -x 15 -y 10 -anchor nw -bordermode ignore
    place $arm2.shoulder_frame.labcosine \
        -x 5 -y 35 -anchor nw -bordermode ignore
    place $arm2.shoulder_frame.laboffset \
        -x 5 -y 60 -anchor nw -bordermode ignore
    place $arm2.shoulder_frame.entsine \
        -x 55 -y 10 -anchor nw -bordermode ignore
    place $arm2.shoulder_frame.entcosine \
        -x 55 -y 35 -anchor nw -bordermode ignore
    place $arm2.shoulder_frame.entoffset \
        -x 55 -y 60 -anchor nw -bordermode ignore
    place $arm2.shoulder_frame.butsine \
        -x 155 -y 10 -width 27 -height 21 -anchor nw -bordermode ignore
    place $arm2.shoulder_frame.butcosine \
        -x 155 -y 35 -width 27 -height 21 -anchor nw -bordermode ignore
#####Shoulder COLUMN 2
    place $arm2.shoulder_frame.labcommand \
        -x 190 -y 10 -anchor nw -bordermode ignore
    place $arm2.shoulder_frame.entcommand \
        -x 255 -y 10 -anchor nw -bordermode ignore
    place $arm2.shoulder_frame.labmaster \
        -x 200 -y 35 -anchor nw -bordermode ignore
    place $arm2.shoulder_frame.entmaster \
        -x 255 -y 35 -anchor nw -bordermode ignore
    place $arm2.shoulder_frame.labfeedback \
        -x 190 -y 60 -anchor nw -bordermode ignore
    place $arm2.shoulder_frame.entfeedback \
        -x 255 -y 60 -anchor nw -bordermode ignore
    place $arm2.shoulder_frame.butcommand \
        -x 355 -y 10 -width 27 -height 21 -anchor nw -bordermode ignore


#####Shoulder COLUMN 3

    place $arm2.shoulder_frame.labservo \
        -x 400 -y 10 -anchor nw -bordermode ignore
    place $arm2.shoulder_frame.entservo \
        -x 445 -y 10 -anchor nw -bordermode ignore
    place $arm2.shoulder_frame.butservo \
        -x 555 -y 10 -width 27 -height 21 -anchor nw -bordermode ignore
    place $arm2.shoulder_frame.labtimeconst \
        -x 370 -y 60 -anchor nw -bordermode ignore
    place $arm2.shoulder_frame.labtimeconstunits \
        -x 520 -y 60 -anchor nw -bordermode ignore
    place $arm2.shoulder_frame.enttimeconst \
        -x 445 -y 60 -anchor nw -bordermode ignore

#####Shoulder COLUMN 4
    place $arm2.shoulder_frame.plabel \
        -x 600 -y 10
    place $arm2.shoulder_frame.ilabel \
        -x 600 -y 35
    place $arm2.shoulder_frame.dlabel \
        -x 600 -y 60
    place $arm2.shoulder_frame.entp \
        -x 620 -y 10
    place $arm2.shoulder_frame.enti \
        -x 620 -y 35
    place $arm2.shoulder_frame.entd \
        -x 620 -y 60
    place $arm2.shoulder_frame.labtimeconst \
        -x 370 -y 60 -anchor nw -bordermode ignore
    place $arm2.shoulder_frame.enttimeconst \
        -x 445 -y 60 -anchor nw -bordermode ignore
#############
# Swing     #
#############

 ######Swing COLUMN 1
    place $arm2.swing_frame.labsine \
        -x 15 -y 10 -anchor nw -bordermode ignore
    place $arm2.swing_frame.labcosine \
        -x 5 -y 35 -anchor nw -bordermode ignore
    place $arm2.swing_frame.laboffset \
        -x 5 -y 60 -anchor nw -bordermode ignore
    place $arm2.swing_frame.entsine \
        -x 55 -y 10 -anchor nw -bordermode ignore
    place $arm2.swing_frame.entcosine \
        -x 55 -y 35 -anchor nw -bordermode ignore
    place $arm2.swing_frame.entoffset \
        -x 55 -y 60 -anchor nw -bordermode ignore
    place $arm2.swing_frame.butsine \
        -x 155 -y 10 -width 27 -height 21 -anchor nw -bordermode ignore
    place $arm2.swing_frame.butcosine \
        -x 155 -y 35 -width 27 -height 21 -anchor nw -bordermode ignore
#####COLUMN 2
    place $arm2.swing_frame.labcommand \
        -x 190 -y 10 -anchor nw -bordermode ignore
    place $arm2.swing_frame.entcommand \
        -x 255 -y 10 -anchor nw -bordermode ignore
    place $arm2.swing_frame.labmaster \
        -x 200 -y 35 -anchor nw -bordermode ignore
    place $arm2.swing_frame.entmaster \
        -x 255 -y 35 -anchor nw -bordermode ignore
    place $arm2.swing_frame.labfeedback \
        -x 190 -y 60 -anchor nw -bordermode ignore
    place $arm2.swing_frame.entfeedback \
        -x 255 -y 60 -anchor nw -bordermode ignore
    place $arm2.swing_frame.butcommand \
        -x 355 -y 10 -width 27 -height 21 -anchor nw -bordermode ignore


#####COLUMN 3

    place $arm2.swing_frame.labservo \
        -x 400 -y 10 -anchor nw -bordermode ignore
    place $arm2.swing_frame.entservo \
        -x 445 -y 10 -anchor nw -bordermode ignore
    place $arm2.swing_frame.butservo \
        -x 555 -y 10 -width 27 -height 21 -anchor nw -bordermode ignore

#####COLUMN 4
    place $arm2.swing_frame.plabel \
        -x 600 -y 10
    place $arm2.swing_frame.ilabel \
        -x 600 -y 35
    place $arm2.swing_frame.dlabel \
        -x 600 -y 60
    place $arm2.swing_frame.entp \
        -x 620 -y 10
    place $arm2.swing_frame.enti \
        -x 620 -y 35
    place $arm2.swing_frame.entd \
        -x 620 -y 60


##############
# Elbow      #
##############

######Elbow COLUMN 1
    place $arm2.elbow_frame.labsine \
        -x 15 -y 10 -anchor nw -bordermode ignore
    place $arm2.elbow_frame.labcosine \
        -x 5 -y 35 -anchor nw -bordermode ignore
    place $arm2.elbow_frame.laboffset \
        -x 5 -y 60 -anchor nw -bordermode ignore
    place $arm2.elbow_frame.entsine \
        -x 55 -y 10 -anchor nw -bordermode ignore
    place $arm2.elbow_frame.entcosine \
        -x 55 -y 35 -anchor nw -bordermode ignore
    place $arm2.elbow_frame.entoffset \
        -x 55 -y 60 -anchor nw -bordermode ignore
    place $arm2.elbow_frame.butsine \
        -x 155 -y 10 -width 27 -height 21 -anchor nw -bordermode ignore
    place $arm2.elbow_frame.butcosine \
        -x 155 -y 35 -width 27 -height 21 -anchor nw -bordermode ignore
##### Elbow COLUMN 2
    place $arm2.elbow_frame.labcommand \
        -x 190 -y 10 -anchor nw -bordermode ignore
    place $arm2.elbow_frame.entcommand \
        -x 255 -y 10 -anchor nw -bordermode ignore
    place $arm2.elbow_frame.labmaster \
        -x 200 -y 35 -anchor nw -bordermode ignore
    place $arm2.elbow_frame.entmaster \
        -x 255 -y 35 -anchor nw -bordermode ignore
    place $arm2.elbow_frame.labfeedback \
        -x 190 -y 60 -anchor nw -bordermode ignore
    place $arm2.elbow_frame.entfeedback \
        -x 255 -y 60 -anchor nw -bordermode ignore
    place $arm2.elbow_frame.butcommand \
        -x 355 -y 10 -width 27 -height 21 -anchor nw -bordermode ignore


#####Elbow COLUMN 3

    place $arm2.elbow_frame.labservo \
        -x 400 -y 10 -anchor nw -bordermode ignore
    place $arm2.elbow_frame.entservo \
        -x 445 -y 10 -anchor nw -bordermode ignore
    place $arm2.elbow_frame.butservo \
        -x 555 -y 10 -width 27 -height 21 -anchor nw -bordermode ignore

#####Elbow COLUMN 4
    place $arm2.elbow_frame.plabel \
        -x 600 -y 10
    place $arm2.elbow_frame.ilabel \
        -x 600 -y 35
    place $arm2.elbow_frame.dlabel \
        -x 600 -y 60
    place $arm2.elbow_frame.entp \
        -x 620 -y 10
    place $arm2.elbow_frame.enti \
        -x 620 -y 35
    place $arm2.elbow_frame.entd \
        -x 620 -y 60

##############
# Pitch      #
##############

######Pitch COLUMN 1
    place $arm2.pitch_frame.labsine \
        -x 15 -y 10 -anchor nw -bordermode ignore
    place $arm2.pitch_frame.labcosine \
        -x 5 -y 35 -anchor nw -bordermode ignore
    place $arm2.pitch_frame.laboffset \
        -x 5 -y 60 -anchor nw -bordermode ignore
    place $arm2.pitch_frame.entsine \
        -x 55 -y 10 -anchor nw -bordermode ignore
    place $arm2.pitch_frame.entcosine \
        -x 55 -y 35 -anchor nw -bordermode ignore
    place $arm2.pitch_frame.entoffset \
        -x 55 -y 60 -anchor nw -bordermode ignore
    place $arm2.pitch_frame.butsine \
        -x 155 -y 10 -width 27 -height 21 -anchor nw -bordermode ignore
    place $arm2.pitch_frame.butcosine \
        -x 155 -y 35 -width 27 -height 21 -anchor nw -bordermode ignore
#####Pitch COLUMN 2
    place $arm2.pitch_frame.labcommand \
        -x 190 -y 10 -anchor nw -bordermode ignore
    place $arm2.pitch_frame.entcommand \
        -x 255 -y 10 -anchor nw -bordermode ignore
    place $arm2.pitch_frame.labmaster \
        -x 200 -y 35 -anchor nw -bordermode ignore
    place $arm2.pitch_frame.entmaster \
        -x 255 -y 35 -anchor nw -bordermode ignore
    place $arm2.pitch_frame.labfeedback \
        -x 190 -y 60 -anchor nw -bordermode ignore
    place $arm2.pitch_frame.entfeedback \
        -x 255 -y 60 -anchor nw -bordermode ignore
    place $arm2.pitch_frame.butcommand \
        -x 355 -y 10 -width 27 -height 21 -anchor nw -bordermode ignore


#####Pitch COLUMN 3

    place $arm2.pitch_frame.labservo \
        -x 400 -y 10 -anchor nw -bordermode ignore
    place $arm2.pitch_frame.entservo \
        -x 445 -y 10 -anchor nw -bordermode ignore
    place $arm2.pitch_frame.butservo \
        -x 555 -y 10 -width 27 -height 21 -anchor nw -bordermode ignore

#####Pitch COLUMN 4
    place $arm2.pitch_frame.plabel \
        -x 600 -y 10
    place $arm2.pitch_frame.ilabel \
        -x 600 -y 35
    place $arm2.pitch_frame.dlabel \
        -x 600 -y 60
    place $arm2.pitch_frame.entp \
        -x 620 -y 10
    place $arm2.pitch_frame.enti \
        -x 620 -y 35
    place $arm2.pitch_frame.entd \
        -x 620 -y 60

##############
# Yaw        #
##############

######Yaw COLUMN 1
    place $arm2.yaw_frame.labsine \
        -x 15 -y 10 -anchor nw -bordermode ignore
    place $arm2.yaw_frame.labcosine \
        -x 5 -y 35 -anchor nw -bordermode ignore
    place $arm2.yaw_frame.laboffset \
        -x 5 -y 60 -anchor nw -bordermode ignore
    place $arm2.yaw_frame.entsine \
        -x 55 -y 10 -anchor nw -bordermode ignore
    place $arm2.yaw_frame.entcosine \
        -x 55 -y 35 -anchor nw -bordermode ignore
    place $arm2.yaw_frame.entoffset \
        -x 55 -y 60 -anchor nw -bordermode ignore
    place $arm2.yaw_frame.butsine \
        -x 155 -y 10 -width 27 -height 21 -anchor nw -bordermode ignore
    place $arm2.yaw_frame.butcosine \
        -x 155 -y 35 -width 27 -height 21 -anchor nw -bordermode ignore
#####Yaw COLUMN 2
    place $arm2.yaw_frame.labcommand \
        -x 190 -y 10 -anchor nw -bordermode ignore
    place $arm2.yaw_frame.entcommand \
        -x 255 -y 10 -anchor nw -bordermode ignore
    place $arm2.yaw_frame.labmaster \
        -x 200 -y 35 -anchor nw -bordermode ignore
    place $arm2.yaw_frame.entmaster \
        -x 255 -y 35 -anchor nw -bordermode ignore
    place $arm2.yaw_frame.labfeedback \
        -x 190 -y 60 -anchor nw -bordermode ignore
    place $arm2.yaw_frame.entfeedback \
        -x 255 -y 60 -anchor nw -bordermode ignore
    place $arm2.yaw_frame.butcommand \
        -x 355 -y 10 -width 27 -height 21 -anchor nw -bordermode ignore


#####Yaw COLUMN 3

    place $arm2.yaw_frame.labservo \
        -x 400 -y 10 -anchor nw -bordermode ignore
    place $arm2.yaw_frame.entservo \
        -x 445 -y 10 -anchor nw -bordermode ignore
    place $arm2.yaw_frame.butservo \
        -x 555 -y 10 -width 27 -height 21 -anchor nw -bordermode ignore

#####Yaw COLUMN 4
    place $arm2.yaw_frame.plabel \
        -x 600 -y 10
    place $arm2.yaw_frame.ilabel \
        -x 600 -y 35
    place $arm2.yaw_frame.dlabel \
        -x 600 -y 60
    place $arm2.yaw_frame.entp \
        -x 620 -y 10
    place $arm2.yaw_frame.enti \
        -x 620 -y 35
    place $arm2.yaw_frame.entd \
        -x 620 -y 60

    #------------- OK, CLOSE button -------------------

    place $arm2.butok \
        -x 5 -y 530 -width 55 -height 26 -anchor nw -bordermode ignore


    #--------- Bind the Offset ----------------------
    bind $arm2.shoulder_frame.entoffset <Return> { send_down_arm2 }
    bind $arm2.swing_frame.entoffset <Return> { send_down_arm2 }
    bind $arm2.elbow_frame.entoffset <Return> { send_down_arm2 }

    bind $arm2.pitch_frame.entoffset <Return> { send_down_arm2 }

    bind $arm2.yaw_frame.entoffset <Return> { send_down_arm2 }

    #--------- Bind the P I D ----------------------

    bind $arm2.shoulder_frame.entp <Return> { send_down_arm2 }
    bind $arm2.shoulder_frame.enti <Return> { send_down_arm2 }
    bind $arm2.shoulder_frame.entd <Return> { send_down_arm2 }

    bind $arm2.swing_frame.entp <Return> { send_down_arm2 }
    bind $arm2.swing_frame.enti <Return> { send_down_arm2 }
    bind $arm2.swing_frame.entd <Return> { send_down_arm2 }


    bind $arm2.elbow_frame.entp <Return> { send_down_arm2 }
    bind $arm2.elbow_frame.enti <Return> { send_down_arm2 }
    bind $arm2.elbow_frame.entd <Return> { send_down_arm2 }


    bind $arm2.pitch_frame.entp <Return> { send_down_arm2 }
    bind $arm2.pitch_frame.enti <Return> { send_down_arm2 }
    bind $arm2.pitch_frame.entd <Return> { send_down_arm2 }

    bind $arm2.yaw_frame.entp <Return> { send_down_arm2 }
    bind $arm2.yaw_frame.enti <Return> { send_down_arm2 }
    bind $arm2.yaw_frame.entd <Return> { send_down_arm2 }


    # Loop to update the master and feedback values
    set arm_timer2 1
    loop_master_feedback2


# end of proc "arm_control_popup2"
}




proc loop_master_feedback2 { } {

	global arm2 arm_timer2
	global shoulder_master_value shoulder_fb_value
	global swing_master_value swing_fb_value
	global elbow_master_value elbow_fb_value
	global pitch_master_value pitch_fb_value
	global yaw_master_value yaw_fb_value
	
	if {![winfo exists $arm2] || $arm_timer2 == 0} {return}
	
	#puts "arm_control_popup2.tcl: updating arm master and feedback "
	
	#set mf 0|1|2|3|4|5|6|7|8|9
	set mf [ret_arm_string]
	set mf_list [ split $mf | ]
	
	set shoulder_master_value [lindex $mf_list 0]
	set shoulder_fb_value [lindex $mf_list 1]
	set swing_master_value [lindex $mf_list 2]
 	set swing_fb_value [lindex $mf_list 3]
 	set elbow_master_value [lindex $mf_list 4]
 	set elbow_fb_value [lindex $mf_list 5]
 	set pitch_master_value [lindex $mf_list 6]
 	set pitch_fb_value [lindex $mf_list 7]
 	set yaw_master_value [lindex $mf_list 8]
	set yaw_fb_value [lindex $mf_list 9]
	
	after 500 [list loop_master_feedback2]
}


proc send_down_arm2 { } {

	global arm2 arm_string old_arm_string arm_timer2	
	global shoulder_sine shoulder_cosine shoulder_offset shoulder_command shoulder_master_value shoulder_fb_value shoulder_servo p_shoulder i_shoulder d_shoulder
 	global swing_sine swing_cosine swing_offset swing_command swing_master_value swing_fb_value swing_servo p_swing i_swing d_swing
 	global elbow_sine elbow_cosine elbow_offset elbow_command elbow_master_value elbow_fb_value elbow_servo p_elbow i_elbow d_elbow
  	global pitch_sine pitch_cosine pitch_offset pitch_command pitch_master_value pitch_fb_value pitch_servo p_pitch i_pitch d_pitch
  	global yaw_sine yaw_cosine yaw_offset yaw_command yaw_master_value yaw_fb_value yaw_servo p_yaw i_yaw d_yaw

  	set shoulder_master_value 0
	set shoulder_fb_value 0
	set swing_master_value 0
	set swing_fb_value 0
	set elbow_master_value 0
	set elbow_fb_value 0
  	set pitch_master_value 0
	set pitch_fb_value 0
	set yaw_master_value 0
	set yaw_fb_value 0


   	set a1 [lindex $shoulder_sine 2]
	set a2 [lindex $shoulder_cosine 2]
	set a3 [lindex $shoulder_command 2]
	set a4 [lindex $shoulder_servo 2]
	
	set b1 [lindex $swing_sine 2]
	set b2 [lindex $swing_cosine 2]
	set b3 [lindex $swing_command 2]
	set b4 [lindex $swing_servo 2]
	
	set c1 [lindex $elbow_sine 2]
	set c2 [lindex $elbow_cosine 2]
	set c3 [lindex $elbow_command 2]
	set c4 [lindex $elbow_servo 2]
	
	set d1 [lindex $pitch_sine 2]
	set d2 [lindex $pitch_cosine 2]
	set d3 [lindex $pitch_command 2]
	set d4 [lindex $pitch_servo 2]
	
	set e1 [lindex $yaw_sine 2]
	set e2 [lindex $yaw_cosine 2]
	set e3 [lindex $yaw_command 2]
	set e4 [lindex $yaw_servo 2]

	if { $a1 == "" || $a2 == "" || $a3 == "" || $a4 == ""  || $b1 == ""  || $b2 == "" || $b3 == "" \
  	|| $b4 == "" || $c1 == ""  || $c2 == "" ||  $c3 == "" ||  $c4 == "" || $d1 == "" || $d2 == "" || $d3 == "" || $d4 == "" \
        	|| $e1 == "" || $e2 == "" || $e3 == "" || $e4 == "" } {
        		
        		set status [tk_dialog  .arm_warning2 { } {Some of the fields are empty.  Close the Arm Control window anyway ?} warning 0 {Yes } { No} ]
       		switch $status {   0 {set arm_timer2 0; destroy $arm2; return }
       				1 {return} }
        		
        		
        	}
     	
       	
        	# arm_string is a "single" string containing all the values

        	set arm_string  "$a1|$a2|$shoulder_offset|$a3|$shoulder_master_value|$shoulder_fb_value|$a4|$p_shoulder|$i_shoulder|$d_shoulder|$b1|$b2|$swing_offset|$b3|$swing_master_value|$swing_fb_value|$b4|$p_swing|$i_swing|$d_swing|$c1|$c2|$elbow_offset|$c3|$elbow_master_value|$elbow_fb_value|$c4|$p_elbow|$i_elbow|$d_elbow|$d1|$d2|$pitch_offset|$d3|$pitch_master_value|$pitch_fb_value|$d4|$p_pitch|$i_pitch|$d_pitch|$e1|$e2|$yaw_offset|$e3|$yaw_master_value|$yaw_fb_value|$e4|$p_yaw|$i_yaw|$d_yaw"
   		set old_arm_string $arm_string
        	
        	# Save the new values to database
        	set_arm $a1 $a2 $shoulder_offset $a3 $shoulder_master_value $shoulder_fb_value $a4 $p_shoulder $i_shoulder $d_shoulder $b1 $b2 $swing_offset $b3 $swing_master_value $swing_fb_value $b4 $p_swing $i_swing $d_swing $c1 $c2 $elbow_offset $c3 $elbow_master_value $elbow_fb_value $c4 $p_elbow $i_elbow $d_elbow $d1 $d2 $pitch_offset $d3 $pitch_master_value $pitch_fb_value $d4 $p_pitch $i_pitch $d_pitch $e1 $e2 $yaw_offset $e3 $yaw_master_value $yaw_fb_value $e4 $p_yaw $i_yaw $d_yaw

        	
        	# save all the information as a single string so it's easier when we want to write it to
		# configuration file
	        saving_arm_string $arm_string
}




