Blame view

mount_tnc/astromecca/mountastro/mountutils_eqmod.py 6.62 KB
40aba612   aklotz   Ajoute les nouvea...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# -*- coding: utf-8 -*-

# #####################################################################
# #####################################################################
# #####################################################################
# Class Mountutils_eqmod
# #####################################################################
# #####################################################################
# This class add utilities for EQMOD protocol
# #####################################################################

class Mountutils_eqmod():
    
    # ==self.= Constant for error codes
    NO_ERROR = 0

    # === Current status for the command :f
    
    # --- current motions
    MOTION_JOG_FAST_POSITIVE = 0
    MOTION_JOG_FAST_NEGATIVE = 1
    MOTION_JOG_SLOW_POSITIVE = 2
    MOTION_JOG_SLOW_NEGATIVE = 3
    MOTION_STOPPED_POSITIVE  = 4
    MOTION_STOPPED_NEGATIVE  = 5
    
    # --- current moving
    MOTION_STOPPED = 0
    MOTION_MOVING  = 1

    # --- current motor
    MOTOR_OFF = 0
    MOTOR_ON  = 1

    # === Current parameters for the command :G

    # --- motion types
    G_OFFSET_FAST     = 0
    G_CONTINUOUS_SLOW = 1
    G_OFFSET_SLOW     = 2
    G_CONTINUOUS_FAST = 3
    
    # --- last motion senses
    # NB: increasing means diurnal or toward north pole for north hemisphere.
    G_SENSE_POSITIVE = 0 # increasing
    G_SENSE_NEGATIVE = 1 # decreasing


    def hexa2int(self, hexa_str):
        a1 = hexa_str[0:2]
        a2 = hexa_str[2:4]
        a3 = hexa_str[4:6]
        hexa = a3+a2+a1
        dec = int(hexa,16)
        return dec

    def int2hexa(self, dec):
        dec = int(dec)
        hexa_str = "{:06X}".format(dec)
        a1 = hexa_str[0:2]
        a2 = hexa_str[2:4]
        a3 = hexa_str[4:6]
        hexa = a3+a2+a1
        return hexa

    def decode_G(self, lastG:str):
        """
        If we get ":G100", then input lastG="00"
        This function will return the last motion_type and motion_sense
        """
        digit =  int(lastG[0])
        motion_type = self.G_OFFSET_FAST
        if digit==0:
            motion_type = self.G_OFFSET_FAST
        if digit==1:
            motion_type = self.G_CONTINUOUS_SLOW
        if digit==2:
            motion_type = self.G_OFFSET_SLOW
        if digit==3:
            motion_type = self.G_CONTINUOUS_FAST
        digit =  int(lastG[1])
        if digit==0:
            motion_sense = self.G_SENSE_POSITIVE
        else:
            motion_sense = self.G_SENSE_NEGATIVE
        return motion_type, motion_sense            
    
    def encode_G(self, motion_type, motion_sense):
        """
        From the last motion type and the last motion sense
        this function will return the digits xy to add to the ":G?xy" command.
        e.g. The output lastG="00"
        """
        lastG = ""
        digit = 0
        if motion_type == self.G_OFFSET_FAST:
            digit = 0
        elif motion_type == self.G_CONTINUOUS_SLOW:
            digit = 1
        elif motion_type == self.G_OFFSET_SLOW:
            digit = 2
        elif motion_type == self.G_CONTINUOUS_FAST:
            digit = 3
        lastG += str(digit)        
        if motion_sense == self.G_SENSE_POSITIVE:
            digit = 0
        else:
            digit = 1
        lastG += str(digit)        
        return lastG
        
    def decode_f(self, f:str):
        """
        If we get "=101", then input f="101"
        This function will return the the current motion, moving and motor
        """
        f_x =  int(f[0])
        if f_x==0:
            current_motion   = self.MOTION_JOG_SLOW_POSITIVE
        elif f_x==1:
            current_motion   = self.MOTION_STOPPED_POSITIVE # or MOTION_JOG_SLOW_POSITIVE
        elif f_x==2:
            current_motion   = self.MOTION_JOG_SLOW_NEGATIVE
        elif f_x==3:
            current_motion   = self.MOTION_STOPPED_NEGATIVE # or MOTION_JOG_SLOW_POSITIVE
        elif f_x==4:
            current_motion   = self.MOTION_JOG_FAST_POSITIVE
        elif f_x==5:
            current_motion   = self.MOTION_JOG_FAST_POSITIVE
        elif f_x==6:
            current_motion   = self.MOTION_JOG_FAST_NEGATIVE
        elif f_x==7:
            current_motion   = self.MOTION_JOG_FAST_NEGATIVE
        f_y =  int(f[1])
        if f_y==0:
            current_moving  = self.MOTION_STOPPED
        else:
            current_moving  = self.MOTION_JOGGING
        f_z =  int(f[2])
        if f_z==0:
            current_motor  = self.MOTOR_OFF
        else:
            current_motor  = self.MOTOR_ON
        return current_motion, current_moving, current_motor
        
    def encode_f(self, motion_type:int, current_motion:int, current_moving:int, current_motor:int)->str:
        """
        From the last motion_type, current motion, moving, motor
        this function will return the answer of a ":f?" command.
        e.g. The output = "101"
        """
        #print(f"(0) motion_type={motion_type} current_motion={current_motion}")        
        f_x = 1
        if motion_type==self.G_OFFSET_FAST or motion_type==self.G_OFFSET_SLOW:
            if current_motion==self.MOTION_JOG_SLOW_POSITIVE:
                f_x = 0
            elif current_motion==self.MOTION_STOPPED_POSITIVE:
                f_x = 1
            elif current_motion==self.MOTION_JOG_SLOW_NEGATIVE:
                f_x = 2
            elif current_motion==self.MOTION_STOPPED_NEGATIVE:
                f_x = 3
            elif current_motion==self.MOTION_JOG_FAST_POSITIVE:
                f_x = 4
            elif current_motion==self.MOTION_JOG_FAST_NEGATIVE:
                f_x = 6
            #print(f"(1) f_x={f_x}")
        elif motion_type==self.G_CONTINUOUS_SLOW:
            if current_motion==self.MOTION_JOG_SLOW_POSITIVE or current_motion==self.MOTION_STOPPED_POSITIVE:
                f_x = 1
            elif current_motion==self.MOTION_JOG_SLOW_NEGATIVE or current_motion==self.MOTION_STOPPED_NEGATIVE:
                f_x = 3
        elif motion_type==self.G_CONTINUOUS_FAST:
            if current_motion==self.MOTION_STOPPED_POSITIVE:
                f_x = 1
            elif current_motion==self.MOTION_STOPPED_NEGATIVE:
                f_x = 3
            elif current_motion==self.MOTION_JOG_FAST_POSITIVE:
                f_x = 5
            elif current_motion==self.MOTION_JOG_FAST_NEGATIVE:
                f_x = 7
        # ---
        #print(f"(2) current_motion={current_motion}")        
        if current_moving==self.MOTION_STOPPED:
            f_y = 0
        else:
            f_y = 1 # MOTION_MOVING
        if current_motor==self.MOTOR_OFF:
            f_z = 0
        else:
            f_z = 1 # MOTOR_ON
        f = str(f_x) + str(f_y) + str(f_z)
        #print(f"(3) f_x={f_x} f_y={f_y} f={f}")
        return f