@@ -1,230 +1,230 | |||
|
1 | 1 | from django.db import models |
|
2 | 2 | from apps.main.models import Configuration |
|
3 | 3 | # Create your models here. |
|
4 | 4 | |
|
5 | 5 | from django.core.validators import MinValueValidator, MaxValueValidator |
|
6 | 6 | from django.core.exceptions import ValidationError |
|
7 | 7 | |
|
8 | 8 | from devices.dds import api, data |
|
9 | 9 | |
|
10 | 10 | ENABLE_TYPE = ( |
|
11 | 11 | (False, 'Disabled'), |
|
12 | 12 | (True, 'Enabled'), |
|
13 | 13 | ) |
|
14 | 14 | MOD_TYPES = ( |
|
15 | 15 | (0, 'Single Tone'), |
|
16 | 16 | (1, 'FSK'), |
|
17 | 17 | (2, 'Ramped FSK'), |
|
18 | 18 | (3, 'Chirp'), |
|
19 | 19 | (4, 'BPSK'), |
|
20 | 20 | ) |
|
21 | 21 | |
|
22 | 22 | class DDSConfiguration(Configuration): |
|
23 | 23 | |
|
24 | 24 | DDS_NBITS = 48 |
|
25 | 25 | |
|
26 |
clock = models.FloatField(verbose_name='Clock In |
|
|
26 | clock = models.FloatField(verbose_name='Clock In (MHz)',validators=[MinValueValidator(5), MaxValueValidator(75)], null=True, default=60) | |
|
27 | 27 | multiplier = models.PositiveIntegerField(verbose_name='Multiplier',validators=[MinValueValidator(1), MaxValueValidator(20)], default=4) |
|
28 | 28 | |
|
29 | 29 | frequencyA_Mhz = models.DecimalField(verbose_name='Frequency A (MHz)', validators=[MinValueValidator(0), MaxValueValidator(150)], max_digits=19, decimal_places=16, null=True, default=49.9200) |
|
30 | 30 | frequencyA = models.BigIntegerField(verbose_name='Frequency A (Decimal)',validators=[MinValueValidator(0), MaxValueValidator(2**DDS_NBITS-1)], blank=True, null=True) |
|
31 | 31 | |
|
32 | 32 | frequencyB_Mhz = models.DecimalField(verbose_name='Frequency B (MHz)', validators=[MinValueValidator(0), MaxValueValidator(150)], max_digits=19, decimal_places=16, blank=True, null=True) |
|
33 | 33 | frequencyB = models.BigIntegerField(verbose_name='Frequency B (Decimal)',validators=[MinValueValidator(0), MaxValueValidator(2**DDS_NBITS-1)], blank=True, null=True) |
|
34 | 34 | |
|
35 | 35 | phaseA_degrees = models.FloatField(verbose_name='Phase A (Degrees)', validators=[MinValueValidator(0), MaxValueValidator(360)], default=0) |
|
36 | 36 | |
|
37 | 37 | phaseB_degrees = models.FloatField(verbose_name='Phase B (Degrees)', validators=[MinValueValidator(0), MaxValueValidator(360)], blank=True, null=True) |
|
38 | 38 | |
|
39 | 39 | modulation = models.PositiveIntegerField(verbose_name='Modulation Type', choices = MOD_TYPES, default = 0) |
|
40 | 40 | |
|
41 | 41 | amplitude_enabled = models.BooleanField(verbose_name='Amplitude Control', choices=ENABLE_TYPE, default=False) |
|
42 | 42 | |
|
43 | 43 | amplitudeI = models.PositiveIntegerField(verbose_name='Amplitude CH1',validators=[MinValueValidator(0), MaxValueValidator(2**12-1)], blank=True, null=True) |
|
44 | 44 | amplitudeQ = models.PositiveIntegerField(verbose_name='Amplitude CH2',validators=[MinValueValidator(0), MaxValueValidator(2**12-1)], blank=True, null=True) |
|
45 | 45 | |
|
46 | 46 | |
|
47 | 47 | def get_nbits(self): |
|
48 | 48 | |
|
49 | 49 | return self.DDS_NBITS |
|
50 | 50 | |
|
51 | 51 | def clean(self): |
|
52 | 52 | |
|
53 | 53 | if self.modulation in [1,2,3]: |
|
54 | 54 | if self.frequencyB is None or self.frequencyB_Mhz is None: |
|
55 | 55 | raise ValidationError({ |
|
56 | 56 | 'frequencyB': 'Frequency modulation has to be defined when FSK or Chirp modulation is selected' |
|
57 | 57 | }) |
|
58 | 58 | |
|
59 | 59 | if self.modulation in [4,]: |
|
60 | 60 | if self.phaseB_degrees is None: |
|
61 | 61 | raise ValidationError({ |
|
62 | 62 | 'phaseB': 'Phase modulation has to be defined when BPSK modulation is selected' |
|
63 | 63 | }) |
|
64 | 64 | |
|
65 | 65 | self.frequencyA_Mhz = data.binary_to_freq(self.frequencyA, self.clock*self.multiplier) |
|
66 | 66 | self.frequencyB_Mhz = data.binary_to_freq(self.frequencyB, self.clock*self.multiplier) |
|
67 | 67 | |
|
68 | 68 | def verify_frequencies(self): |
|
69 | 69 | |
|
70 | 70 | return True |
|
71 | 71 | |
|
72 | 72 | def parms_to_dict(self): |
|
73 | 73 | |
|
74 | 74 | parameters = {} |
|
75 | 75 | |
|
76 | 76 | parameters['device_id'] = self.device.id |
|
77 | 77 | |
|
78 | 78 | parameters['clock'] = float(self.clock) |
|
79 | 79 | parameters['multiplier'] = int(self.multiplier) |
|
80 | 80 | |
|
81 | 81 | parameters['frequencyA'] = int(self.frequencyA) |
|
82 | 82 | parameters['frequencyA_Mhz'] = float(self.frequencyA_Mhz) |
|
83 | 83 | |
|
84 | 84 | parameters['phaseA'] = data.phase_to_binary(self.phaseA_degrees) |
|
85 | 85 | parameters['phaseA_degrees'] = float(self.phaseA_degrees) |
|
86 | 86 | |
|
87 | 87 | parameters['modulation'] = int(self.modulation) |
|
88 | 88 | parameters['amplitude_enabled'] = bool(self.amplitude_enabled) |
|
89 | 89 | |
|
90 | 90 | if self.frequencyB: |
|
91 | 91 | parameters['frequencyB'] = int(self.frequencyB) |
|
92 | 92 | parameters['frequencyB_Mhz'] = float(self.frequencyB_Mhz) |
|
93 | 93 | else: |
|
94 | 94 | parameters['frequencyB'] = 0 |
|
95 | 95 | parameters['frequencyB_Mhz'] = 0 |
|
96 | 96 | |
|
97 | 97 | if self.phaseB_degrees: |
|
98 | 98 | parameters['phaseB_degrees'] = float(self.phaseB_degrees) |
|
99 | 99 | parameters['phaseB'] = data.phase_to_binary(self.phaseB_degrees) |
|
100 | 100 | else: |
|
101 | 101 | parameters['phaseB_degrees'] = 0 |
|
102 | 102 | parameters['phaseB'] = 0 |
|
103 | 103 | |
|
104 | 104 | if self.amplitudeI: |
|
105 | 105 | parameters['amplitudeI'] = int(self.amplitudeI) |
|
106 | 106 | else: |
|
107 | 107 | parameters['amplitudeI'] = 0 |
|
108 | 108 | |
|
109 | 109 | if self.amplitudeQ: |
|
110 | 110 | parameters['amplitudeQ'] = int(self.amplitudeQ) |
|
111 | 111 | else: |
|
112 | 112 | parameters['amplitudeQ'] = 0 |
|
113 | 113 | |
|
114 | 114 | return parameters |
|
115 | 115 | |
|
116 | 116 | def parms_to_text(self): |
|
117 | 117 | |
|
118 | 118 | my_dict = self.parms_to_dict() |
|
119 | 119 | |
|
120 | 120 | text = data.dict_to_text(my_dict) |
|
121 | 121 | |
|
122 | 122 | return text |
|
123 | 123 | |
|
124 | 124 | def dict_to_parms(self, parameters): |
|
125 | 125 | |
|
126 | 126 | self.clock = parameters['clock'] |
|
127 | 127 | self.multiplier = parameters['multiplier'] |
|
128 | 128 | self.frequencyA = parameters['frequencyA'] |
|
129 | 129 | self.frequencyB = parameters['frequencyB'] |
|
130 | 130 | self.frequencyA_Mhz = parameters['frequencyA_Mhz'] |
|
131 | 131 | self.frequencyB_Mhz = parameters['frequencyB_Mhz'] |
|
132 | 132 | self.phaseA_degrees = parameters['phaseA_degrees'] |
|
133 | 133 | self.phaseB_degrees = parameters['phaseB_degrees'] |
|
134 | 134 | self.modulation = parameters['modulation'] |
|
135 | 135 | self.amplitude_enabled = parameters['amplitude_enabled'] |
|
136 | 136 | |
|
137 | 137 | def import_from_file(self, fp): |
|
138 | 138 | |
|
139 | 139 | import os, json |
|
140 | 140 | |
|
141 | 141 | parms = {} |
|
142 | 142 | |
|
143 | 143 | path, ext = os.path.splitext(fp.name) |
|
144 | 144 | |
|
145 | 145 | if ext == '.json': |
|
146 | 146 | parms = json.load(fp) |
|
147 | 147 | |
|
148 | 148 | if ext == '.dds': |
|
149 | 149 | lines = fp.readlines() |
|
150 | 150 | parms = data.text_to_dict(lines) |
|
151 | 151 | |
|
152 | 152 | return parms |
|
153 | 153 | |
|
154 | 154 | def status_device(self): |
|
155 | 155 | |
|
156 | 156 | answer = api.status(ip = self.device.ip_address, |
|
157 | 157 | port = self.device.port_address) |
|
158 | 158 | |
|
159 | 159 | self.device.status = int(answer[0]) |
|
160 | 160 | self.message = answer[2:] |
|
161 | 161 | |
|
162 | 162 | self.device.save() |
|
163 | 163 | |
|
164 | 164 | return self.device.status |
|
165 | 165 | |
|
166 | 166 | def reset_device(self): |
|
167 | 167 | |
|
168 | 168 | answer = api.reset(ip = self.device.ip_address, |
|
169 | 169 | port = self.device.port_address) |
|
170 | 170 | |
|
171 | 171 | if answer[0] != "1": |
|
172 | 172 | self.message = answer[0:] |
|
173 | 173 | return 0 |
|
174 | 174 | |
|
175 | 175 | self.message = answer[2:] |
|
176 | 176 | return 1 |
|
177 | 177 | |
|
178 | 178 | def stop_device(self): |
|
179 | 179 | |
|
180 | 180 | answer = api.disable_rf(ip = self.device.ip_address, |
|
181 | 181 | port = self.device.port_address) |
|
182 | 182 | |
|
183 | 183 | if answer[0] != "1": |
|
184 | 184 | self.message = answer[0:] |
|
185 | 185 | return 0 |
|
186 | 186 | |
|
187 | 187 | self.message = answer[2:] |
|
188 | 188 | return 1 |
|
189 | 189 | |
|
190 | 190 | def start_device(self): |
|
191 | 191 | |
|
192 | 192 | answer = api.enable_rf(ip = self.device.ip_address, |
|
193 | 193 | port = self.device.port_address) |
|
194 | 194 | |
|
195 | 195 | if answer[0] != "1": |
|
196 | 196 | self.message = answer[0:] |
|
197 | 197 | return 0 |
|
198 | 198 | |
|
199 | 199 | self.message = answer[2:] |
|
200 | 200 | return 1 |
|
201 | 201 | |
|
202 | 202 | def read_device(self): |
|
203 | 203 | |
|
204 | 204 | parms = api.read_config(ip = self.device.ip_address, |
|
205 | 205 | port = self.device.port_address) |
|
206 | 206 | |
|
207 | 207 | if not parms: |
|
208 | 208 | self.message = "Could not read DDS parameters from this device" |
|
209 | 209 | return parms |
|
210 | 210 | |
|
211 | 211 | self.message = "" |
|
212 | 212 | return parms |
|
213 | 213 | |
|
214 | 214 | |
|
215 | 215 | def write_device(self): |
|
216 | 216 | |
|
217 | 217 | answer = api.write_config(ip = self.device.ip_address, |
|
218 | 218 | port = self.device.port_address, |
|
219 | 219 | parms = self.parms_to_dict()) |
|
220 | 220 | |
|
221 | 221 | if answer[0] != "1": |
|
222 | 222 | self.message = answer[0:] |
|
223 | 223 | return 0 |
|
224 | 224 | |
|
225 | 225 | self.message = answer[2:] |
|
226 | 226 | return 1 |
|
227 | 227 | |
|
228 | 228 | class Meta: |
|
229 | 229 | db_table = 'dds_configurations' |
|
230 | 230 | No newline at end of file |
General Comments 0
You need to be logged in to leave comments.
Login now