@@ -4,11 +4,6 from .models import DDSConfiguration | |||||
4 |
|
4 | |||
5 | # from django.core.validators import MinValueValidator, MaxValueValidator |
|
5 | # from django.core.validators import MinValueValidator, MaxValueValidator | |
6 |
|
6 | |||
7 | EXT_TYPES = ( |
|
|||
8 | ('dds', '.dds'), |
|
|||
9 | ('json', '.json'), |
|
|||
10 | ) |
|
|||
11 |
|
||||
12 | class DDSConfigurationForm(forms.ModelForm): |
|
7 | class DDSConfigurationForm(forms.ModelForm): | |
13 |
|
8 | |||
14 | def __init__(self, *args, **kwargs): |
|
9 | def __init__(self, *args, **kwargs): | |
@@ -26,10 +21,10 class DDSConfigurationForm(forms.ModelForm): | |||||
26 |
|
21 | |||
27 | self.fields['device'].widget.choices = [(device.id, device) for device in devices] |
|
22 | self.fields['device'].widget.choices = [(device.id, device) for device in devices] | |
28 |
|
23 | |||
29 |
|
24 | # | ||
30 | def clean(self): |
|
25 | # def clean(self): | |
31 | # Custom validation to force an integer when type of unit = "Unit" |
|
26 | # # Custom validation to force an integer when type of unit = "Unit" | |
32 | return |
|
27 | # return | |
33 |
|
28 | |||
34 | class Meta: |
|
29 | class Meta: | |
35 | model = DDSConfiguration |
|
30 | model = DDSConfiguration |
@@ -5,7 +5,7 from apps.main.models import Configuration | |||||
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'), | |
@@ -32,10 +32,10 class DDSConfiguration(Configuration): | |||||
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 | phaseB_degrees = models.FloatField(verbose_name='Phase B (Degrees)', validators=[MinValueValidator(0), MaxValueValidator(360)], blank=True, null=True) |
|
|||
36 |
|
||||
37 | 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) | |
38 |
|
36 | |||
|
37 | phaseB_degrees = models.FloatField(verbose_name='Phase B (Degrees)', validators=[MinValueValidator(0), MaxValueValidator(360)], blank=True, null=True) | |||
|
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) | |
@@ -61,7 +61,10 class DDSConfiguration(Configuration): | |||||
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) | |||
|
66 | self.frequencyB_Mhz = data.binary_to_freq(self.frequencyB, self.clock*self.multiplier) | |||
|
67 | ||||
65 | def verify_frequencies(self): |
|
68 | def verify_frequencies(self): | |
66 |
|
69 | |||
67 | return True |
|
70 | return True | |
@@ -72,17 +75,30 class DDSConfiguration(Configuration): | |||||
72 |
|
75 | |||
73 | parameters['clock'] = float(self.clock) |
|
76 | parameters['clock'] = float(self.clock) | |
74 | parameters['multiplier'] = int(self.multiplier) |
|
77 | parameters['multiplier'] = int(self.multiplier) | |
|
78 | ||||
75 | parameters['frequencyA'] = int(self.frequencyA) |
|
79 | parameters['frequencyA'] = int(self.frequencyA) | |
76 |
parameters['frequency |
|
80 | parameters['frequencyA_Mhz'] = float(self.frequencyA_Mhz) | |
|
81 | ||||
77 | parameters['phaseA'] = data.phase_to_binary(self.phaseA_degrees) |
|
82 | parameters['phaseA'] = data.phase_to_binary(self.phaseA_degrees) | |
78 | parameters['phaseB'] = data.phase_to_binary(self.phaseB_degrees) |
|
|||
79 | parameters['frequencyA_Mhz'] = int(self.frequencyA_Mhz) |
|
|||
80 | parameters['frequencyB_Mhz'] = int(self.frequencyB_Mhz) |
|
|||
81 | parameters['phaseA_degrees'] = float(self.phaseA_degrees) |
|
83 | parameters['phaseA_degrees'] = float(self.phaseA_degrees) | |
82 | parameters['phaseB_degrees'] = float(self.phaseB_degrees) |
|
84 | ||
83 | parameters['modulation'] = int(self.modulation) |
|
85 | parameters['modulation'] = int(self.modulation) | |
84 |
parameters['amplitude_enabled'] = |
|
86 | parameters['amplitude_enabled'] = bool(self.amplitude_enabled) | |
|
87 | ||||
|
88 | if self.frequencyB: | |||
|
89 | parameters['frequencyB'] = int(self.frequencyB) | |||
|
90 | parameters['frequencyB_Mhz'] = float(self.frequencyB_Mhz) | |||
|
91 | else: | |||
|
92 | parameters['frequencyB'] = 0 | |||
|
93 | parameters['frequencyB_Mhz'] = 0 | |||
85 |
|
94 | |||
|
95 | if self.phaseB_degrees: | |||
|
96 | parameters['phaseB_degrees'] = float(self.phaseB_degrees) | |||
|
97 | parameters['phaseB'] = data.phase_to_binary(self.phaseB_degrees) | |||
|
98 | else: | |||
|
99 | parameters['phaseB_degrees'] = 0 | |||
|
100 | parameters['phaseB'] = 0 | |||
|
101 | ||||
86 | if self.amplitudeI: |
|
102 | if self.amplitudeI: | |
87 | parameters['amplitudeI'] = int(self.amplitudeI) |
|
103 | parameters['amplitudeI'] = int(self.amplitudeI) | |
88 | else: |
|
104 | else: | |
@@ -95,6 +111,14 class DDSConfiguration(Configuration): | |||||
95 |
|
111 | |||
96 | return parameters |
|
112 | return parameters | |
97 |
|
113 | |||
|
114 | def parms_to_text(self): | |||
|
115 | ||||
|
116 | my_dict = self.parms_to_dict() | |||
|
117 | ||||
|
118 | text = data.dict_to_text(my_dict) | |||
|
119 | ||||
|
120 | return text | |||
|
121 | ||||
98 | def dict_to_parms(self, parameters): |
|
122 | def dict_to_parms(self, parameters): | |
99 |
|
123 | |||
100 | self.clock = parameters['clock'] |
|
124 | self.clock = parameters['clock'] | |
@@ -109,18 +133,19 class DDSConfiguration(Configuration): | |||||
109 | self.amplitude_enabled = parameters['amplitude_enabled'] |
|
133 | self.amplitude_enabled = parameters['amplitude_enabled'] | |
110 |
|
134 | |||
111 | def import_from_file(self, fp): |
|
135 | def import_from_file(self, fp): | |
112 |
|
136 | |||
113 | import os |
|
137 | import os, json | |
114 |
|
138 | |||
115 | parms = {} |
|
139 | parms = {} | |
116 |
|
140 | |||
117 | path, ext = os.path.splitext(fp) |
|
141 | path, ext = os.path.splitext(fp.name) | |
118 |
|
142 | |||
119 | if ext == '.json': |
|
143 | if ext == '.json': | |
120 |
parms = |
|
144 | parms = json.load(fp) | |
121 |
|
145 | |||
122 | if ext == '.dds': |
|
146 | if ext == '.dds': | |
123 |
|
|
147 | lines = fp.readlines() | |
|
148 | parms = data.text_to_dict(lines) | |||
124 |
|
149 | |||
125 | return parms |
|
150 | return parms | |
126 |
|
151 |
@@ -14,7 +14,7 def dds_conf(request, id_conf): | |||||
14 |
|
14 | |||
15 | kwargs = {} |
|
15 | kwargs = {} | |
16 |
|
16 | |||
17 | kwargs['status'] = conf.device.status |
|
17 | kwargs['status'] = conf.device.get_status_display() | |
18 |
|
18 | |||
19 | # if not kwargs['connected']: |
|
19 | # if not kwargs['connected']: | |
20 | # messages.error(request, message=answer) |
|
20 | # messages.error(request, message=answer) | |
@@ -65,7 +65,7 def dds_conf_edit(request, id_conf): | |||||
65 | return redirect('url_dds_conf', id_conf=conf.id) |
|
65 | return redirect('url_dds_conf', id_conf=conf.id) | |
66 |
|
66 | |||
67 | ##ERRORS |
|
67 | ##ERRORS | |
68 |
|
|
68 | ||
69 | kwargs = {} |
|
69 | kwargs = {} | |
70 | kwargs['id_dev'] = conf.id |
|
70 | kwargs['id_dev'] = conf.id | |
71 | kwargs['form'] = form |
|
71 | kwargs['form'] = form |
@@ -3,6 +3,20 from django.utils.safestring import mark_safe | |||||
3 |
|
3 | |||
4 | from .models import DeviceType, Device, Experiment, Campaign, Configuration, Location |
|
4 | from .models import DeviceType, Device, Experiment, Campaign, Configuration, Location | |
5 |
|
5 | |||
|
6 | FILE_FORMAT = ( | |||
|
7 | ('json', 'json'), | |||
|
8 | ) | |||
|
9 | ||||
|
10 | DDS_FILE_FORMAT = ( | |||
|
11 | ('json', 'json'), | |||
|
12 | ('text', 'dds') | |||
|
13 | ) | |||
|
14 | ||||
|
15 | RC_FILE_FORMAT = ( | |||
|
16 | ('json', 'json'), | |||
|
17 | ('text', 'rc') | |||
|
18 | ) | |||
|
19 | ||||
6 | def add_empty_choice(choices, pos=0, label='-----'): |
|
20 | def add_empty_choice(choices, pos=0, label='-----'): | |
7 | if len(choices)>0: |
|
21 | if len(choices)>0: | |
8 | choices = list(choices) |
|
22 | choices = list(choices) | |
@@ -68,10 +82,22 class DeviceTypeForm(forms.Form): | |||||
68 | class UploadFileForm(forms.Form): |
|
82 | class UploadFileForm(forms.Form): | |
69 |
|
83 | |||
70 | file = forms.FileField() |
|
84 | file = forms.FileField() | |
71 |
|
85 | |||
72 | class DownloadFileForm(forms.Form): |
|
86 | class DownloadFileForm(forms.Form): | |
73 |
|
87 | |||
74 |
format = forms.C |
|
88 | format = forms.ChoiceField(choices= ((0, 'json'),) ) | |
|
89 | ||||
|
90 | def __init__(self, device_type, *args, **kwargs): | |||
|
91 | ||||
|
92 | super(DownloadFileForm, self).__init__(*args, **kwargs) | |||
|
93 | ||||
|
94 | self.fields['format'].choices = FILE_FORMAT | |||
|
95 | ||||
|
96 | if device_type == 'dds': | |||
|
97 | self.fields['format'].choices = DDS_FILE_FORMAT | |||
|
98 | ||||
|
99 | if device_type == 'rc': | |||
|
100 | self.fields['format'].choices = RC_FILE_FORMAT | |||
75 |
|
101 | |||
76 | class OperationForm(forms.Form): |
|
102 | class OperationForm(forms.Form): | |
77 | # today = datetime.today() |
|
103 | # today = datetime.today() |
@@ -163,9 +163,9 class Configuration(PolymorphicModel): | |||||
163 | db_table = 'db_configurations' |
|
163 | db_table = 'db_configurations' | |
164 |
|
164 | |||
165 | def __unicode__(self): |
|
165 | def __unicode__(self): | |
166 |
return u'[%s |
|
166 | return u'[%s, %s]: %s' % (self.experiment.name, | |
167 |
self. |
|
167 | self.device.name, | |
168 |
self. |
|
168 | self.name) | |
169 |
|
169 | |||
170 | def parms_to_dict(self): |
|
170 | def parms_to_dict(self): | |
171 |
|
171 | |||
@@ -176,6 +176,18 class Configuration(PolymorphicModel): | |||||
176 |
|
176 | |||
177 | return parameters |
|
177 | return parameters | |
178 |
|
178 | |||
|
179 | def parms_to_text(self): | |||
|
180 | ||||
|
181 | raise NotImplementedError, "This method should be implemented in %s Configuration model" %str(self.device.device_type.name).upper() | |||
|
182 | ||||
|
183 | return '' | |||
|
184 | ||||
|
185 | def parms_to_binary(self): | |||
|
186 | ||||
|
187 | raise NotImplementedError, "This method should be implemented in %s Configuration model" %str(self.device.device_type.name).upper() | |||
|
188 | ||||
|
189 | return '' | |||
|
190 | ||||
179 | def dict_to_parms(self, parameters): |
|
191 | def dict_to_parms(self, parameters): | |
180 |
|
192 | |||
181 | if type(parameters) != type({}): |
|
193 | if type(parameters) != type({}): | |
@@ -188,19 +200,22 class Configuration(PolymorphicModel): | |||||
188 |
|
200 | |||
189 | import json |
|
201 | import json | |
190 |
|
202 | |||
191 |
content_type = ' |
|
203 | content_type = '' | |
192 | filename = '%s.json' %self.name |
|
|||
193 | content = json.dumps(self.params_to_dict()) |
|
|||
194 |
|
204 | |||
195 | if format == 'text': |
|
205 | if format == 'text': | |
196 | content_type = 'text/plain' |
|
206 | content_type = 'text/plain' | |
197 | filename = '%s.%s' %(self.name, self.device.device_type.name) |
|
207 | filename = '%s_%s.%s' %(self.device.device_type.name, self.name, self.device.device_type.name) | |
198 |
content = self.par |
|
208 | content = self.parms_to_text() | |
199 |
|
209 | |||
200 | if format == 'binary': |
|
210 | if format == 'binary': | |
201 | content_type = 'application/octet-stream' |
|
211 | content_type = 'application/octet-stream' | |
202 | filename = '%s.bin' %self.name |
|
212 | filename = '%s_%s.bin' %(self.device.device_type.name, self.name) | |
203 |
content = self.par |
|
213 | content = self.parms_to_binary() | |
|
214 | ||||
|
215 | if not content_type: | |||
|
216 | content_type = 'application/json' | |||
|
217 | filename = '%s_%s.json' %(self.device.device_type.name, self.name) | |||
|
218 | content = json.dumps(self.parms_to_dict()) | |||
204 |
|
219 | |||
205 | fields = {'content_type':content_type, |
|
220 | fields = {'content_type':content_type, | |
206 | 'filename':filename, |
|
221 | 'filename':filename, | |
@@ -209,39 +224,46 class Configuration(PolymorphicModel): | |||||
209 |
|
224 | |||
210 | return fields |
|
225 | return fields | |
211 |
|
226 | |||
212 |
def import_from_file(self, f |
|
227 | def import_from_file(self, fp): | |
|
228 | ||||
|
229 | import os, json | |||
|
230 | ||||
|
231 | parms = {} | |||
|
232 | ||||
|
233 | path, ext = os.path.splitext(fp.name) | |||
213 |
|
234 | |||
214 | raise NotImplementedError, "This method should be implemented in each Configuration model" |
|
235 | if ext == '.json': | |
|
236 | parms = json.load(fp) | |||
215 |
|
237 | |||
216 |
return |
|
238 | return parms | |
217 |
|
239 | |||
218 | def status_device(self): |
|
240 | def status_device(self): | |
219 |
|
241 | |||
220 |
raise NotImplementedError, "This method should be implemented in |
|
242 | raise NotImplementedError, "This method should be implemented in %s Configuration model" %str(self.device.device_type.name).upper() | |
221 |
|
243 | |||
222 | return None |
|
244 | return None | |
223 |
|
245 | |||
224 | def stop_device(self): |
|
246 | def stop_device(self): | |
225 |
|
247 | |||
226 |
raise NotImplementedError, "This method should be implemented in |
|
248 | raise NotImplementedError, "This method should be implemented in %s Configuration model" %str(self.device.device_type.name).upper() | |
227 |
|
249 | |||
228 | return None |
|
250 | return None | |
229 |
|
251 | |||
230 | def start_device(self): |
|
252 | def start_device(self): | |
231 |
|
253 | |||
232 |
raise NotImplementedError, "This method should be implemented in |
|
254 | raise NotImplementedError, "This method should be implemented in %s Configuration model" %str(self.device.device_type.name).upper() | |
233 |
|
255 | |||
234 | return None |
|
256 | return None | |
235 |
|
257 | |||
236 | def write_device(self, parms): |
|
258 | def write_device(self, parms): | |
237 |
|
259 | |||
238 |
raise NotImplementedError, "This method should be implemented in |
|
260 | raise NotImplementedError, "This method should be implemented in %s Configuration model" %str(self.device.device_type.name).upper() | |
239 |
|
261 | |||
240 | return None |
|
262 | return None | |
241 |
|
263 | |||
242 | def read_device(self): |
|
264 | def read_device(self): | |
243 |
|
265 | |||
244 |
raise NotImplementedError, "This method should be implemented in |
|
266 | raise NotImplementedError, "This method should be implemented in %s Configuration model" %str(self.device.device_type.name).upper() | |
245 |
|
267 | |||
246 | return None |
|
268 | return None | |
247 |
|
269 |
@@ -41,11 +41,13 | |||||
41 | <li class=" dropdown {% block operation-active %}{% endblock %}"><a href="{% url 'url_operation'%}">Operation</a> |
|
41 | <li class=" dropdown {% block operation-active %}{% endblock %}"><a href="{% url 'url_operation'%}">Operation</a> | |
42 | </li> |
|
42 | </li> | |
43 | <li class=" dropdown {% block conf-active %}{% endblock %}"> |
|
43 | <li class=" dropdown {% block conf-active %}{% endblock %}"> | |
44 |
<a href="#" class="dropdown-toggle" data-toggle="dropdown"> |
|
44 | <a href="#" class="dropdown-toggle" data-toggle="dropdown">New<span class="caret"></span></a> | |
45 | <ul class="dropdown-menu" role="menu"> |
|
45 | <ul class="dropdown-menu" role="menu"> | |
46 |
<li><a href=" |
|
46 | <li><a href="{% url 'url_add_campaign' %}">Campaign</a></li> | |
47 |
<li><a href=" |
|
47 | <li><a href="{% url 'url_add_experiment' 0%}">Experiment</a></li> | |
48 |
<li><a href=" |
|
48 | <li><a href="{% url 'url_add_dev_conf' 0%}">Device Configuration</a></li> | |
|
49 | <li><a href="{% url 'url_add_device'%}">Radar/Device</a></li> | |||
|
50 | ||||
49 | </ul> |
|
51 | </ul> | |
50 | </li> |
|
52 | </li> | |
51 | <li class=" dropdown {% block test-active %}{% endblock %}"> |
|
53 | <li class=" dropdown {% block test-active %}{% endblock %}"> |
@@ -28,7 +28,7 | |||||
28 | <table class="table table-bordered"> |
|
28 | <table class="table table-bordered"> | |
29 | <tr> |
|
29 | <tr> | |
30 | <th>Status</th> |
|
30 | <th>Status</th> | |
31 |
<td>{%if status %} <span class="glyphicon glyphicon-ok-circle text-success" aria-hidden="true"></span> {{status}} {% else %} <span class="glyphicon glyphicon-remove-circle text-danger" aria-hidden="true"></span> |
|
31 | <td>{%if status != "No connected" %} <span class="glyphicon glyphicon-ok-circle text-success" aria-hidden="true"></span> {{status}} {% else %} <span class="glyphicon glyphicon-remove-circle text-danger" aria-hidden="true"></span> {{status}} {% endif %}</td> | |
32 | </tr> |
|
32 | </tr> | |
33 |
|
33 | |||
34 | {% for key in dev_conf_keys %} |
|
34 | {% for key in dev_conf_keys %} |
@@ -514,7 +514,7 def dev_conf_edit(request, id_conf): | |||||
514 | ###### SIDEBAR ###### |
|
514 | ###### SIDEBAR ###### | |
515 | kwargs.update(sidebar(conf)) |
|
515 | kwargs.update(sidebar(conf)) | |
516 |
|
516 | |||
517 |
return render(request, ' |
|
517 | return render(request, '%s_conf_edit.html' %conf.device.device_type.name, kwargs) | |
518 |
|
518 | |||
519 | def dev_conf_start(request, id_conf): |
|
519 | def dev_conf_start(request, id_conf): | |
520 |
|
520 | |||
@@ -528,7 +528,9 def dev_conf_start(request, id_conf): | |||||
528 | messages.success(request, conf.message) |
|
528 | messages.success(request, conf.message) | |
529 | else: |
|
529 | else: | |
530 | messages.error(request, conf.message) |
|
530 | messages.error(request, conf.message) | |
531 |
|
|
531 | ||
|
532 | conf.status_device() | |||
|
533 | ||||
532 | return redirect(conf.get_absolute_url()) |
|
534 | return redirect(conf.get_absolute_url()) | |
533 |
|
535 | |||
534 | def dev_conf_stop(request, id_conf): |
|
536 | def dev_conf_stop(request, id_conf): | |
@@ -544,6 +546,8 def dev_conf_stop(request, id_conf): | |||||
544 | else: |
|
546 | else: | |
545 | messages.error(request, conf.message) |
|
547 | messages.error(request, conf.message) | |
546 |
|
548 | |||
|
549 | conf.status_device() | |||
|
550 | ||||
547 | return redirect(conf.get_absolute_url()) |
|
551 | return redirect(conf.get_absolute_url()) | |
548 |
|
552 | |||
549 | def dev_conf_status(request, id_conf): |
|
553 | def dev_conf_status(request, id_conf): | |
@@ -571,16 +575,20 def dev_conf_write(request, id_conf): | |||||
571 | conf = DevConfModel.objects.get(pk=id_conf) |
|
575 | conf = DevConfModel.objects.get(pk=id_conf) | |
572 |
|
576 | |||
573 | answer = conf.write_device() |
|
577 | answer = conf.write_device() | |
|
578 | conf.status_device() | |||
574 |
|
579 | |||
575 | if answer: |
|
580 | if answer: | |
576 | messages.success(request, conf.message) |
|
581 | messages.success(request, conf.message) | |
577 |
|
582 | |||
|
583 | #Creating a historical configuration | |||
578 | conf.pk = None |
|
584 | conf.pk = None | |
579 | conf.id = None |
|
585 | conf.id = None | |
580 | conf.type = 1 |
|
586 | conf.type = 1 | |
581 | conf.template = 0 |
|
587 | conf.template = 0 | |
582 | conf.save() |
|
588 | conf.save() | |
583 |
|
589 | |||
|
590 | #Original configuration | |||
|
591 | conf = DevConfModel.objects.get(pk=id_conf) | |||
584 | else: |
|
592 | else: | |
585 | messages.error(request, conf.message) |
|
593 | messages.error(request, conf.message) | |
586 |
|
594 | |||
@@ -598,6 +606,7 def dev_conf_read(request, id_conf): | |||||
598 | if request.method=='GET': |
|
606 | if request.method=='GET': | |
599 |
|
607 | |||
600 | parms = conf.read_device() |
|
608 | parms = conf.read_device() | |
|
609 | conf.status_device() | |||
601 |
|
610 | |||
602 | if not parms: |
|
611 | if not parms: | |
603 | messages.error(request, conf.message) |
|
612 | messages.error(request, conf.message) | |
@@ -609,10 +618,8 def dev_conf_read(request, id_conf): | |||||
609 | form = DevConfForm(request.POST, instance=conf) |
|
618 | form = DevConfForm(request.POST, instance=conf) | |
610 |
|
619 | |||
611 | if form.is_valid(): |
|
620 | if form.is_valid(): | |
612 |
|
|
621 | form.save() | |
613 |
|
622 | return redirect(conf.get_absolute_url()) | ||
614 | if dev_model.save(): |
|
|||
615 | return redirect(conf.get_absolute_url()) |
|
|||
616 |
|
623 | |||
617 | messages.error(request, "Parameters could not be saved") |
|
624 | messages.error(request, "Parameters could not be saved") | |
618 |
|
625 | |||
@@ -626,7 +633,7 def dev_conf_read(request, id_conf): | |||||
626 | ###### SIDEBAR ###### |
|
633 | ###### SIDEBAR ###### | |
627 | kwargs.update(sidebar(conf)) |
|
634 | kwargs.update(sidebar(conf)) | |
628 |
|
635 | |||
629 | return render(conf.get_absolute_url_edit()) |
|
636 | return render(request, '%s_conf_edit.html' %conf.device.device_type.name, kwargs) | |
630 |
|
637 | |||
631 | def dev_conf_import(request, id_conf): |
|
638 | def dev_conf_import(request, id_conf): | |
632 |
|
639 | |||
@@ -648,9 +655,8 def dev_conf_import(request, id_conf): | |||||
648 | parms = conf.import_from_file(request.FILES['file']) |
|
655 | parms = conf.import_from_file(request.FILES['file']) | |
649 |
|
656 | |||
650 | if parms: |
|
657 | if parms: | |
651 |
|
||||
652 | messages.success(request, "Parameters imported from: '%s'." %request.FILES['file'].name) |
|
658 | messages.success(request, "Parameters imported from: '%s'." %request.FILES['file'].name) | |
653 |
|
659 | print parms | ||
654 | form = DevConfForm(initial=parms, instance=conf) |
|
660 | form = DevConfForm(initial=parms, instance=conf) | |
655 |
|
661 | |||
656 | kwargs = {} |
|
662 | kwargs = {} | |
@@ -689,13 +695,13 def dev_conf_export(request, id_conf): | |||||
689 | conf = DevConfModel.objects.get(pk=id_conf) |
|
695 | conf = DevConfModel.objects.get(pk=id_conf) | |
690 |
|
696 | |||
691 | if request.method == 'GET': |
|
697 | if request.method == 'GET': | |
692 | file_form = DownloadFileForm() |
|
698 | file_form = DownloadFileForm(conf.device.device_type.name) | |
693 |
|
699 | |||
694 | if request.method == 'POST': |
|
700 | if request.method == 'POST': | |
695 | file_form = DownloadFileForm(request.POST) |
|
701 | file_form = DownloadFileForm(conf.device.device_type.name, request.POST) | |
696 |
|
702 | |||
697 | if file_form.is_valid(): |
|
703 | if file_form.is_valid(): | |
698 | fields = conf.export_to_file(format = file_form.format) |
|
704 | fields = conf.export_to_file(format = file_form.cleaned_data['format']) | |
699 |
|
705 | |||
700 | response = HttpResponse(content_type=fields['content_type']) |
|
706 | response = HttpResponse(content_type=fields['content_type']) | |
701 | response['Content-Disposition'] = 'attachment; filename="%s"' %fields['filename'] |
|
707 | response['Content-Disposition'] = 'attachment; filename="%s"' %fields['filename'] |
@@ -1,8 +1,8 | |||||
1 | import ast |
|
1 | import ast | |
2 | import json |
|
2 | import json | |
3 | import numpy as np |
|
3 | # import numpy as np | |
4 | import matplotlib.pyplot as plt |
|
4 | # import matplotlib.pyplot as plt | |
5 | from matplotlib import cm |
|
5 | # from matplotlib import cm | |
6 |
|
6 | |||
7 | class Pulse_Design_Racp: |
|
7 | class Pulse_Design_Racp: | |
8 | """A class to define the .racp output from Pulse Design """ |
|
8 | """A class to define the .racp output from Pulse Design """ |
@@ -4,34 +4,179 Created on Feb 15, 2016 | |||||
4 | @author: Miguel Urco |
|
4 | @author: Miguel Urco | |
5 | ''' |
|
5 | ''' | |
6 | import struct |
|
6 | import struct | |
|
7 | import string | |||
7 |
|
8 | |||
8 | DDS_NBITS = 48 |
|
9 | DDS_NBITS = 48 | |
9 |
|
10 | |||
|
11 | FILE_STRUCTURE = """Phase Adjust Register 1 | |||
|
12 | ----------------------- | |||
|
13 | 00000000 | |||
|
14 | 00000000 | |||
|
15 | ----------------------- | |||
|
16 | Phase Adjust Register 2 | |||
|
17 | ----------------------- | |||
|
18 | 00000000 | |||
|
19 | 00000000 | |||
|
20 | ----------------------- | |||
|
21 | Frequency Tuning Word 1 | |||
|
22 | ----------------------- | |||
|
23 | 00000000 | |||
|
24 | 00000000 | |||
|
25 | 00000000 | |||
|
26 | 00000000 | |||
|
27 | 00000000 | |||
|
28 | 00000000 | |||
|
29 | ----------------------- | |||
|
30 | Frequency Tuning Word 2 | |||
|
31 | ----------------------- | |||
|
32 | 00000000 | |||
|
33 | 00000000 | |||
|
34 | 00000000 | |||
|
35 | 00000000 | |||
|
36 | 00000000 | |||
|
37 | 00000000 | |||
|
38 | ----------------------- | |||
|
39 | Delta Frequency Word | |||
|
40 | ----------------------- | |||
|
41 | 00000000 | |||
|
42 | 00000000 | |||
|
43 | 00000000 | |||
|
44 | 00000000 | |||
|
45 | 00000000 | |||
|
46 | 00000000 | |||
|
47 | ----------------------- | |||
|
48 | Update Clock | |||
|
49 | ----------------------- | |||
|
50 | 00000000 | |||
|
51 | 00000000 | |||
|
52 | 00000000 | |||
|
53 | 00000000 | |||
|
54 | ----------------------- | |||
|
55 | Ramp Rate Clock | |||
|
56 | ----------------------- | |||
|
57 | 00000000 | |||
|
58 | 00000000 | |||
|
59 | 00000000 | |||
|
60 | ----------------------- | |||
|
61 | Control Register | |||
|
62 | ----------------------- | |||
|
63 | 00000000 | |||
|
64 | 00000000 | |||
|
65 | 00000000 | |||
|
66 | 00000000 | |||
|
67 | ----------------------- | |||
|
68 | Output Shaped Keying I | |||
|
69 | Multiplier | |||
|
70 | ----------------------- | |||
|
71 | 00000000 | |||
|
72 | 00000000 | |||
|
73 | ----------------------- | |||
|
74 | Output Shaped Keying Q | |||
|
75 | Multiplier | |||
|
76 | ----------------------- | |||
|
77 | 00000000 | |||
|
78 | 00000000 | |||
|
79 | ----------------------- | |||
|
80 | Output Shaped Keying | |||
|
81 | Ramp Rate | |||
|
82 | ----------------------- | |||
|
83 | 00000000 | |||
|
84 | ----------------------- | |||
|
85 | QDAC | |||
|
86 | ----------------------- | |||
|
87 | 00000000 | |||
|
88 | 00000000 | |||
|
89 | ----------------------- | |||
|
90 | CLOCK INPUT | |||
|
91 | ----------------------- | |||
|
92 | 10.00000000""" | |||
|
93 | ||||
10 | def freq_to_binary(freq, mclock): |
|
94 | def freq_to_binary(freq, mclock): | |
11 |
|
95 | |||
12 | binary = (float(freq)/mclock)*(2**DDS_NBITS) |
|
96 | if not mclock: | |
|
97 | return None | |||
|
98 | ||||
|
99 | try: | |||
|
100 | binary = int((float(freq)/mclock)*(2**DDS_NBITS)) | |||
|
101 | except: | |||
|
102 | return 0 | |||
13 |
|
103 | |||
14 | return binary |
|
104 | return binary | |
15 |
|
105 | |||
16 | def binary_to_freq(binary, mclock): |
|
106 | def binary_to_freq(binary, mclock): | |
17 |
|
107 | |||
18 | freq = (float(binary)/(2**DDS_NBITS))*mclock |
|
108 | if not mclock: | |
|
109 | return None | |||
|
110 | ||||
|
111 | try: | |||
|
112 | freq = (float(binary)/(2**DDS_NBITS))*mclock | |||
|
113 | except: | |||
|
114 | return 0 | |||
19 |
|
115 | |||
20 | return freq |
|
116 | return freq | |
21 |
|
117 | |||
22 | def phase_to_binary(phase): |
|
118 | def phase_to_binary(phase): | |
23 |
|
119 | |||
24 | binary = float(phase)*8192/180.0 |
|
120 | try: | |
|
121 | binary = int(float(phase)*8192/180.0) | |||
|
122 | except: | |||
|
123 | return 0 | |||
25 |
|
124 | |||
26 | return binary |
|
125 | return binary | |
27 |
|
126 | |||
28 | def binary_to_phase(binary): |
|
127 | def binary_to_phase(binary): | |
29 |
|
128 | |||
30 | phase = float(binary)*180.0/8192 |
|
129 | try: | |
|
130 | phase = float(binary)*180.0/8192 | |||
|
131 | except: | |||
|
132 | return 0 | |||
31 |
|
133 | |||
32 | return phase |
|
134 | return phase | |
33 |
|
135 | |||
34 | def dds_str_to_dict(registers): |
|
136 | def __fill_dds_dict(parms): | |
|
137 | ||||
|
138 | my_dict = {'clock' : None, | |||
|
139 | 'multiplier' : 1, | |||
|
140 | 'frequencyA' : 0, | |||
|
141 | 'frequencyB' : 0, | |||
|
142 | 'frequencyA_Mhz' : 0, | |||
|
143 | 'frequencyB_Mhz' : 0, | |||
|
144 | 'phaseA_degress' : 0, | |||
|
145 | 'phaseB_degress' : 0, | |||
|
146 | 'modulation' : 0, | |||
|
147 | 'amplitudeI' : 0, | |||
|
148 | 'amplitudeQ' : 0, | |||
|
149 | 'amplitude_enabled' : 0, | |||
|
150 | 'delta_frequency' : 0, | |||
|
151 | 'update_clock' : 0, | |||
|
152 | 'ramp_rate_clock' : 0, | |||
|
153 | 'amplitude_ramp_rate' : 0, | |||
|
154 | 'qdac' : 0 | |||
|
155 | } | |||
|
156 | ||||
|
157 | my_dict.update(parms) | |||
|
158 | my_dict['phaseA'] = phase_to_binary(my_dict['phaseA_degrees']) | |||
|
159 | my_dict['phaseB'] = phase_to_binary(my_dict['phaseB_degrees']) | |||
|
160 | ||||
|
161 | pll_range = 0 | |||
|
162 | if my_dict['clock'] >= 200: | |||
|
163 | pll_range = 1 | |||
|
164 | ||||
|
165 | pll_bypass = 0 | |||
|
166 | if my_dict['multiplier'] < 4: | |||
|
167 | pll_bypass = 1 | |||
|
168 | ||||
|
169 | control_register = (1 << 28) + \ | |||
|
170 | (pll_range << 22) + (pll_bypass << 21) + \ | |||
|
171 | (my_dict['multiplier'] << 16) + \ | |||
|
172 | (my_dict['modulation'] << 9) + \ | |||
|
173 | (my_dict['amplitude_enabled'] << 5) | |||
|
174 | ||||
|
175 | my_dict['control_register'] = control_register | |||
|
176 | ||||
|
177 | return my_dict | |||
|
178 | ||||
|
179 | def dds_str_to_dict(registers, clock=None): | |||
35 |
|
180 | |||
36 | """ |
|
181 | """ | |
37 | Output: |
|
182 | Output: | |
@@ -80,12 +225,20 def dds_str_to_dict(registers): | |||||
80 | modulation = (control_register & 0x00000E00) >> 9 |
|
225 | modulation = (control_register & 0x00000E00) >> 9 | |
81 | amplitude_enabled = (control_register & 0x00000020) >> 5 |
|
226 | amplitude_enabled = (control_register & 0x00000020) >> 5 | |
82 |
|
227 | |||
83 | parms = {'clock' : None, |
|
228 | frequencyA_Mhz = None | |
|
229 | frequencyB_Mhz = None | |||
|
230 | ||||
|
231 | if clock: | |||
|
232 | mclock = clock*multiplier | |||
|
233 | frequencyA_Mhz = binary_to_freq(frequencyA, mclock) | |||
|
234 | frequencyB_Mhz = binary_to_freq(frequencyB, mclock) | |||
|
235 | ||||
|
236 | parms = {'clock' : clock, | |||
84 | 'multiplier' : multiplier, |
|
237 | 'multiplier' : multiplier, | |
85 | 'frequencyA' : frequencyA, |
|
238 | 'frequencyA' : frequencyA, | |
86 | 'frequencyB' : frequencyB, |
|
239 | 'frequencyB' : frequencyB, | |
87 |
'frequencyA_Mhz' : |
|
240 | 'frequencyA_Mhz' : frequencyA_Mhz, | |
88 |
'frequencyB_Mhz' : |
|
241 | 'frequencyB_Mhz' : frequencyB_Mhz, | |
89 | 'phaseA' : phaseA, |
|
242 | 'phaseA' : phaseA, | |
90 | 'phaseB' : phaseB, |
|
243 | 'phaseB' : phaseB, | |
91 | 'phaseA_degrees' : binary_to_phase(phaseA), |
|
244 | 'phaseA_degrees' : binary_to_phase(phaseA), | |
@@ -117,35 +270,10 def dict_to_dds_str(parms): | |||||
117 | amplitudeQ : 0 to (2**12-1) equivalent to: 0 - 100% |
|
270 | amplitudeQ : 0 to (2**12-1) equivalent to: 0 - 100% | |
118 | """ |
|
271 | """ | |
119 |
|
272 | |||
120 | my_dict = {'clock' : None, |
|
273 | my_dict = __fill_dds_dict(parms) | |
121 | 'multiplier' : 1, |
|
|||
122 | 'frequencyA' : 0, |
|
|||
123 | 'frequencyB' : 0, |
|
|||
124 | 'frequencyA_Mhz' : 0, |
|
|||
125 | 'frequencyB_Mhz' : 0, |
|
|||
126 | 'phaseA_degress' : 0, |
|
|||
127 | 'phaseB_degress' : 0, |
|
|||
128 | 'modulation' : 0, |
|
|||
129 | 'amplitudeI' : 0, |
|
|||
130 | 'amplitudeQ' : 0, |
|
|||
131 | 'amplitude_enabled' : 0, |
|
|||
132 | 'delta_frequency' : 0, |
|
|||
133 | 'update_clock' : 0, |
|
|||
134 | 'ramp_rate_clock' : 0, |
|
|||
135 | 'amplitude_ramp_rate' : 0, |
|
|||
136 | 'qdac' : 0 |
|
|||
137 | } |
|
|||
138 |
|
||||
139 | print "PArms", parms |
|
|||
140 |
|
||||
141 | my_dict.update(parms) |
|
|||
142 | my_dict['phaseA'] = phase_to_binary(my_dict['phaseA_degrees']) |
|
|||
143 | my_dict['phaseB'] = phase_to_binary(my_dict['phaseB_degrees']) |
|
|||
144 |
|
274 | |||
145 | registers = "" |
|
275 | registers = "" | |
146 |
|
276 | |||
147 | control_register = (my_dict['multiplier'] << 16) + (my_dict['modulation'] << 9) + (my_dict['amplitude_enabled'] << 5) |
|
|||
148 |
|
||||
149 | registers += struct.pack(">H", my_dict['phaseA']) |
|
277 | registers += struct.pack(">H", my_dict['phaseA']) | |
150 | registers += struct.pack(">H", my_dict['phaseB']) |
|
278 | registers += struct.pack(">H", my_dict['phaseB']) | |
151 |
|
279 | |||
@@ -158,7 +286,7 def dict_to_dds_str(parms): | |||||
158 |
|
286 | |||
159 | registers += struct.pack(">I", my_dict['ramp_rate_clock'])[1:] |
|
287 | registers += struct.pack(">I", my_dict['ramp_rate_clock'])[1:] | |
160 |
|
288 | |||
161 | registers += struct.pack(">I", control_register) |
|
289 | registers += struct.pack(">I", my_dict['control_register']) | |
162 |
|
290 | |||
163 | registers += struct.pack(">H", my_dict['amplitudeI']) |
|
291 | registers += struct.pack(">H", my_dict['amplitudeI']) | |
164 |
|
292 | |||
@@ -168,4 +296,115 def dict_to_dds_str(parms): | |||||
168 |
|
296 | |||
169 | registers += struct.pack(">H", my_dict['qdac']) |
|
297 | registers += struct.pack(">H", my_dict['qdac']) | |
170 |
|
298 | |||
171 | return registers No newline at end of file |
|
299 | return registers | |
|
300 | ||||
|
301 | def text_to_dict(lines): | |||
|
302 | ||||
|
303 | registers = "" | |||
|
304 | registers_v2 = [] | |||
|
305 | ||||
|
306 | for this_line in lines: | |||
|
307 | this_line = str.strip(this_line) | |||
|
308 | ||||
|
309 | if str.isalpha(this_line): | |||
|
310 | continue | |||
|
311 | ||||
|
312 | if not str.isdigit(this_line): | |||
|
313 | try: | |||
|
314 | value = float(this_line) | |||
|
315 | except: | |||
|
316 | continue | |||
|
317 | ||||
|
318 | registers_v2.append(value) | |||
|
319 | continue | |||
|
320 | ||||
|
321 | if len(this_line) != 8: | |||
|
322 | continue | |||
|
323 | ||||
|
324 | registers += chr(string.atoi(this_line,2)) | |||
|
325 | ||||
|
326 | mclock = None | |||
|
327 | if len(registers_v2) > 0: | |||
|
328 | mclock = registers_v2[0] | |||
|
329 | ||||
|
330 | my_dict = dds_str_to_dict(registers, mclock) | |||
|
331 | ||||
|
332 | return my_dict | |||
|
333 | ||||
|
334 | def dict_to_text(parms): | |||
|
335 | """ | |||
|
336 | It creates formatted DDS text using dictionary values. | |||
|
337 | """ | |||
|
338 | my_dict = __fill_dds_dict(parms) | |||
|
339 | ||||
|
340 | lines = FILE_STRUCTURE.split('\n') | |||
|
341 | ||||
|
342 | cad = '{0:016b}'.format(my_dict['phaseA']) | |||
|
343 | lines[2] = cad[0:8] | |||
|
344 | lines[3] = cad[8:16] | |||
|
345 | ||||
|
346 | cad = '{0:016b}'.format(my_dict['phaseB']) | |||
|
347 | lines[7] = cad[0:8] | |||
|
348 | lines[8] = cad[8:16] | |||
|
349 | ||||
|
350 | cad = '{0:048b}'.format(my_dict['frequencyA']) | |||
|
351 | lines[12] = cad[0:8] | |||
|
352 | lines[13] = cad[8:16] | |||
|
353 | lines[14] = cad[16:24] | |||
|
354 | lines[15] = cad[24:32] | |||
|
355 | lines[16] = cad[32:40] | |||
|
356 | lines[17] = cad[40:48] | |||
|
357 | ||||
|
358 | cad = '{0:048b}'.format(my_dict['frequencyB']) | |||
|
359 | lines[21] = cad[0:8] | |||
|
360 | lines[22] = cad[8:16] | |||
|
361 | lines[23] = cad[16:24] | |||
|
362 | lines[24] = cad[24:32] | |||
|
363 | lines[25] = cad[32:40] | |||
|
364 | lines[26] = cad[40:48] | |||
|
365 | ||||
|
366 | cad = '{0:048b}'.format(my_dict['delta_frequency']) | |||
|
367 | lines[30] = cad[0:8] | |||
|
368 | lines[31] = cad[8:16] | |||
|
369 | lines[32] = cad[16:24] | |||
|
370 | lines[33] = cad[24:32] | |||
|
371 | lines[34] = cad[32:40] | |||
|
372 | lines[35] = cad[40:48] | |||
|
373 | ||||
|
374 | cad = '{0:032b}'.format(my_dict['update_clock']) | |||
|
375 | lines[39] = cad[0:8] | |||
|
376 | lines[40] = cad[8:16] | |||
|
377 | lines[41] = cad[16:24] | |||
|
378 | lines[42] = cad[24:32] | |||
|
379 | ||||
|
380 | cad = '{0:024b}'.format(my_dict['ramp_rate_clock']) | |||
|
381 | lines[46] = cad[0:8] | |||
|
382 | lines[47] = cad[8:16] | |||
|
383 | lines[48] = cad[16:24] | |||
|
384 | ||||
|
385 | cad = '{0:032b}'.format(my_dict['control_register']) | |||
|
386 | lines[52] = cad[0:8] | |||
|
387 | lines[53] = cad[8:16] | |||
|
388 | lines[54] = cad[16:24] | |||
|
389 | lines[55] = cad[24:32] | |||
|
390 | ||||
|
391 | cad = '{0:016b}'.format(my_dict['amplitudeI']) | |||
|
392 | lines[60] = cad[0:8] | |||
|
393 | lines[61] = cad[8:16] | |||
|
394 | ||||
|
395 | cad = '{0:016b}'.format(my_dict['amplitudeQ']) | |||
|
396 | lines[66] = cad[0:8] | |||
|
397 | lines[67] = cad[8:16] | |||
|
398 | ||||
|
399 | cad = '{0:08b}'.format(my_dict['amplitude_ramp_rate']) | |||
|
400 | lines[72] = cad[0:8] | |||
|
401 | ||||
|
402 | cad = '{0:016b}'.format(my_dict['qdac']) | |||
|
403 | lines[76] = cad[0:8] | |||
|
404 | lines[77] = cad[8:16] | |||
|
405 | ||||
|
406 | lines[81] = '%10.8f' %my_dict['clock'] | |||
|
407 | ||||
|
408 | text = '\n'.join(lines) | |||
|
409 | ||||
|
410 | return text No newline at end of file |
1 | NO CONTENT: file was removed |
|
NO CONTENT: file was removed |
1 | NO CONTENT: file was removed |
|
NO CONTENT: file was removed |
General Comments 0
You need to be logged in to leave comments.
Login now