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