@@ -8,7 +8,7 from base64 import b64encode | |||||
8 | from struct import pack |
|
8 | from struct import pack | |
9 |
|
9 | |||
10 | from django.db import models |
|
10 | from django.db import models | |
11 |
from django. |
|
11 | from django.urls import reverse | |
12 | from django.core.validators import MinValueValidator, MaxValueValidator |
|
12 | from django.core.validators import MinValueValidator, MaxValueValidator | |
13 |
|
13 | |||
14 | from apps.main.models import Configuration |
|
14 | from apps.main.models import Configuration | |
@@ -109,7 +109,7 class RCConfiguration(Configuration): | |||||
109 |
|
109 | |||
110 | def clone(self, **kwargs): |
|
110 | def clone(self, **kwargs): | |
111 |
|
111 | |||
112 |
lines = self.get_lines() |
|
112 | lines = self.get_lines() | |
113 | self.pk = None |
|
113 | self.pk = None | |
114 | self.id = None |
|
114 | self.id = None | |
115 | for attr, value in kwargs.items(): |
|
115 | for attr, value in kwargs.items(): | |
@@ -158,6 +158,7 class RCConfiguration(Configuration): | |||||
158 | else: |
|
158 | else: | |
159 | data = Params(params).get_conf(dtype='rc') |
|
159 | data = Params(params).get_conf(dtype='rc') | |
160 |
|
160 | |||
|
161 | #print(data) | |||
161 | # self.name = data['name'] |
|
162 | # self.name = data['name'] | |
162 | self.ipp = data['ipp'] |
|
163 | self.ipp = data['ipp'] | |
163 | self.ntx = data['ntx'] |
|
164 | self.ntx = data['ntx'] | |
@@ -172,6 +173,8 class RCConfiguration(Configuration): | |||||
172 | self.save() |
|
173 | self.save() | |
173 | self.clean_lines() |
|
174 | self.clean_lines() | |
174 |
|
175 | |||
|
176 | #print(params) | |||
|
177 | ||||
175 | positions = {'tx':0, 'tr':0} |
|
178 | positions = {'tx':0, 'tr':0} | |
176 | for i, id in enumerate(data['lines']): |
|
179 | for i, id in enumerate(data['lines']): | |
177 | line_data = params['lines']['byId'][id] |
|
180 | line_data = params['lines']['byId'][id] | |
@@ -210,8 +213,9 class RCConfiguration(Configuration): | |||||
210 | params=json.dumps(ref_line['params']) |
|
213 | params=json.dumps(ref_line['params']) | |
211 | ).pk |
|
214 | ).pk | |
212 | line.params = json.dumps(line_params) |
|
215 | line.params = json.dumps(line_params) | |
|
216 | print(line.params) | |||
213 | line.save() |
|
217 | line.save() | |
214 |
|
218 | print("Fin de dict to param") | ||
215 |
|
219 | |||
216 | def get_delays(self): |
|
220 | def get_delays(self): | |
217 |
|
221 | |||
@@ -226,7 +230,6 class RCConfiguration(Configuration): | |||||
226 |
|
230 | |||
227 | return [points[i+1]-points[i] for i in range(len(points)-1)] |
|
231 | return [points[i+1]-points[i] for i in range(len(points)-1)] | |
228 |
|
232 | |||
229 |
|
||||
230 | def get_pulses(self, binary=True): |
|
233 | def get_pulses(self, binary=True): | |
231 |
|
234 | |||
232 | pulses = [line.pulses_as_points() for line in self.get_lines()] |
|
235 | pulses = [line.pulses_as_points() for line in self.get_lines()] | |
@@ -236,27 +239,42 class RCConfiguration(Configuration): | |||||
236 | points.sort() |
|
239 | points.sort() | |
237 | states = [] |
|
240 | states = [] | |
238 | last = [0 for x in pulses] |
|
241 | last = [0 for x in pulses] | |
|
242 | n_pulses = len(pulses) | |||
|
243 | print('len_pulses', n_pulses) | |||
|
244 | print('len_points', len(points)) | |||
|
245 | ups_arr = [[] for _ in range(n_pulses)] | |||
|
246 | dws_arr = [[] for _ in range(n_pulses)] | |||
|
247 | ||||
|
248 | for i, tups in enumerate(pulses): | |||
|
249 | ups_arr[i] = [tup[0] for tup in tups if tup!=(0,0)] | |||
|
250 | dws_arr[i] = [tup[1] for tup in tups if tup!=(0,0)] | |||
|
251 | print('len_points*n_pulses',len(points)*n_pulses) | |||
|
252 | #print('ups_arr', ups_arr) | |||
|
253 | #print('dws_arr', dws_arr) | |||
239 |
|
254 | |||
240 | for x in points: |
|
255 | for x in points: | |
241 | dum = [] |
|
256 | dum = [] | |
242 | for i, tups in enumerate(pulses): |
|
257 | print('loading', x*100/max(points)) | |
243 | ups = [tup[0] for tup in tups if tup!=(0,0)] |
|
258 | ||
244 | dws = [tup[1] for tup in tups if tup!=(0,0)] |
|
259 | for i in range(n_pulses): | |
245 | if x in ups: |
|
260 | if x in ups_arr[i]: | |
246 | dum.append(1) |
|
261 | dum.append(1) | |
247 | elif x in dws: |
|
262 | elif x in dws_arr[i]: | |
248 | dum.append(0) |
|
263 | dum.append(0) | |
249 | else: |
|
264 | else: | |
250 | dum.append(last[i]) |
|
265 | dum.append(last[i]) | |
251 | states.append(dum) |
|
266 | #print(dum) | |
|
267 | states.append(dum) | |||
252 | last = dum |
|
268 | last = dum | |
253 |
|
269 | |||
254 | if binary: |
|
270 | if binary: | |
255 | ret = [] |
|
271 | ret = [] | |
256 | for flips in states: |
|
272 | for flips in states: | |
257 | flips.reverse() |
|
273 | flips.reverse() | |
258 | ret.append(int(''.join([str(x) for x in flips]), 2)) |
|
274 | ret.append(int(''.join([str(x) for x in flips]), 2)) | |
259 | states = ret |
|
275 | states = ret | |
|
276 | #print(states[:-1]) | |||
|
277 | #print('len_states',len(states[:-1])) | |||
260 |
|
278 | |||
261 | return states[:-1] |
|
279 | return states[:-1] | |
262 |
|
280 | |||
@@ -343,8 +361,10 class RCConfiguration(Configuration): | |||||
343 | return '\n'.join(['{}'.format(x) for x in data]) |
|
361 | return '\n'.join(['{}'.format(x) for x in data]) | |
344 |
|
362 | |||
345 | def update_pulses(self): |
|
363 | def update_pulses(self): | |
346 |
|
364 | contador = 0 | ||
347 | for line in self.get_lines(): |
|
365 | for line in self.get_lines(): | |
|
366 | contador=contador+1 | |||
|
367 | print(contador) | |||
348 | line.update_pulses() |
|
368 | line.update_pulses() | |
349 |
|
369 | |||
350 | def plot_pulses2(self, km=False): |
|
370 | def plot_pulses2(self, km=False): | |
@@ -384,7 +404,7 class RCConfiguration(Configuration): | |||||
384 | ax.set_yticklabels(labels) |
|
404 | ax.set_yticklabels(labels) | |
385 | ax.set_xlabel = 'Units' |
|
405 | ax.set_xlabel = 'Units' | |
386 | plot = to_bokeh(fig, use_pandas=False) |
|
406 | plot = to_bokeh(fig, use_pandas=False) | |
387 |
plot.tools = [PanTool(dimensions= |
|
407 | plot.tools = [PanTool(dimensions="width"), WheelZoomTool(dimensions="width"), ResetTool(), SaveTool()] | |
388 | plot.toolbar_location="above" |
|
408 | plot.toolbar_location="above" | |
389 |
|
409 | |||
390 | return components(plot, CDN) |
|
410 | return components(plot, CDN) | |
@@ -394,7 +414,7 class RCConfiguration(Configuration): | |||||
394 | from bokeh.plotting import figure |
|
414 | from bokeh.plotting import figure | |
395 | from bokeh.resources import CDN |
|
415 | from bokeh.resources import CDN | |
396 | from bokeh.embed import components |
|
416 | from bokeh.embed import components | |
397 | from bokeh.models import FixedTicker, PrintfTickFormatter |
|
417 | from bokeh.models import FixedTicker, PrintfTickFormatter, Label | |
398 | from bokeh.models.tools import WheelZoomTool, ResetTool, PanTool, HoverTool, SaveTool |
|
418 | from bokeh.models.tools import WheelZoomTool, ResetTool, PanTool, HoverTool, SaveTool | |
399 | from bokeh.models.sources import ColumnDataSource |
|
419 | from bokeh.models.sources import ColumnDataSource | |
400 |
|
420 | |||
@@ -408,8 +428,8 class RCConfiguration(Configuration): | |||||
408 | ("IPP", "@ipp"), |
|
428 | ("IPP", "@ipp"), | |
409 | ("X", "@left")]) |
|
429 | ("X", "@left")]) | |
410 |
|
430 | |||
411 |
tools = [PanTool(dimensions= |
|
431 | tools = [PanTool(dimensions="width"), | |
412 |
WheelZoomTool(dimensions= |
|
432 | WheelZoomTool(dimensions="width"), | |
413 | hover, SaveTool()] |
|
433 | hover, SaveTool()] | |
414 |
|
434 | |||
415 | plot = figure(width=1000, |
|
435 | plot = figure(width=1000, | |
@@ -419,6 +439,15 class RCConfiguration(Configuration): | |||||
419 | toolbar_location='above', |
|
439 | toolbar_location='above', | |
420 | toolbar_sticky=False,) |
|
440 | toolbar_sticky=False,) | |
421 |
|
441 | |||
|
442 | pulses = [line.pulses_as_points() for line in self.get_lines()] | |||
|
443 | tuples = [tup for tups in pulses for tup in tups] | |||
|
444 | points = set([x for tup in tuples for x in tup]) | |||
|
445 | capacity_bytes = round((8*(len(points)-1)+12)/2) # se divide entre 2 porque la mitad era solo para direcciones | |||
|
446 | capacity_percent = (capacity_bytes/2097152)*100 | |||
|
447 | # Add the used memory message | |||
|
448 | x_label_memory = Label(x=900, y=-1.5, text='Used memory of '+str(capacity_bytes)+'/2097152 bytes ('+'%.3f'%capacity_percent+'%)', text_align="right", x_units='screen', text_font_size='14pt') | |||
|
449 | plot.add_layout(x_label_memory, 'below') | |||
|
450 | ||||
422 | plot.xaxis.axis_label = 'Km' if km else 'Units' |
|
451 | plot.xaxis.axis_label = 'Km' if km else 'Units' | |
423 | plot.xaxis[0].formatter = PrintfTickFormatter(format='%d') |
|
452 | plot.xaxis[0].formatter = PrintfTickFormatter(format='%d') | |
424 | plot.yaxis.axis_label = 'Pulses' |
|
453 | plot.yaxis.axis_label = 'Pulses' | |
@@ -545,7 +574,8 class RCConfiguration(Configuration): | |||||
545 |
|
574 | |||
546 | return True |
|
575 | return True | |
547 |
|
576 | |||
548 |
def write_device(self, raw=False): |
|
577 | def write_device(self, raw=False): | |
|
578 | print("write device") | |||
549 |
|
579 | |||
550 | if not raw: |
|
580 | if not raw: | |
551 | clock = RCClock.objects.get(rc_configuration=self) |
|
581 | clock = RCClock.objects.get(rc_configuration=self) | |
@@ -554,16 +584,22 class RCConfiguration(Configuration): | |||||
554 | else: |
|
584 | else: | |
555 | data = {'manual': [clock.multiplier, clock.divisor, clock.reference]} |
|
585 | data = {'manual': [clock.multiplier, clock.divisor, clock.reference]} | |
556 | payload = self.request('setfreq', 'post', data=json.dumps(data)) |
|
586 | payload = self.request('setfreq', 'post', data=json.dumps(data)) | |
557 |
if payload[' |
|
587 | #if payload['setfreq'] != 'ok': | |
|
588 | if payload['command'] != 'ok': | |||
|
589 | #self.message = 'RC write: {}'.format(payload['setfreq']) | |||
558 | self.message = 'RC write: {}'.format(payload['command']) |
|
590 | self.message = 'RC write: {}'.format(payload['command']) | |
559 | else: |
|
591 | else: | |
|
592 | #self.message = payload['setfreq'] | |||
560 | self.message = payload['programming'] |
|
593 | self.message = payload['programming'] | |
|
594 | #if payload['setfreq'] == 'fail': | |||
561 | if payload['programming'] == 'fail': |
|
595 | if payload['programming'] == 'fail': | |
562 | self.message = 'RC write: error programming CGS chip' |
|
596 | self.message = 'RC write: error programming CGS chip' | |
563 |
|
597 | |||
564 | values = [] |
|
598 | values = [] | |
|
599 | print('wait delay values...') | |||
565 | for pulse, delay in zip(self.get_pulses(), self.get_delays()): |
|
600 | for pulse, delay in zip(self.get_pulses(), self.get_delays()): | |
566 | while delay>65536: |
|
601 | #print('wait zip...') | |
|
602 | while delay > 65536: | |||
567 | values.append((pulse, 65535)) |
|
603 | values.append((pulse, 65535)) | |
568 | delay -= 65536 |
|
604 | delay -= 65536 | |
569 | values.append((pulse, delay-1)) |
|
605 | values.append((pulse, delay-1)) | |
@@ -583,6 +619,7 class RCConfiguration(Configuration): | |||||
583 | data.extend((139, 62)) |
|
619 | data.extend((139, 62)) | |
584 |
|
620 | |||
585 | last = 0 |
|
621 | last = 0 | |
|
622 | print('wait data...') | |||
586 | for tup in values: |
|
623 | for tup in values: | |
587 | vals = pack('<HH', last^tup[0], tup[1]) |
|
624 | vals = pack('<HH', last^tup[0], tup[1]) | |
588 | last = tup[0] |
|
625 | last = tup[0] | |
@@ -590,41 +627,48 class RCConfiguration(Configuration): | |||||
590 |
|
627 | |||
591 | #enable |
|
628 | #enable | |
592 | data.extend((129, 1)) |
|
629 | data.extend((129, 1)) | |
|
630 | print('len',len(data)) | |||
593 |
|
631 | |||
594 | if raw: |
|
632 | if raw: | |
595 | return b64encode(data) |
|
633 | return b64encode(data) | |
596 |
|
634 | |||
597 | try: |
|
635 | #try: | |
598 | payload = self.request('stop', 'post') |
|
636 | print('requests') | |
599 |
|
|
637 | payload = self.request('stop', 'post') | |
600 |
|
|
638 | payload = self.request('reset', 'post') | |
601 | #payload = self.request('write', 'post', data=b64encode(bytearray((139, 62))), timeout=20) |
|
639 | #payload = self.request('divider', 'post', data={'divider': self.clock_divider-1}) | |
602 | n = len(data) |
|
640 | #payload = self.request('write', 'post', data=b64encode(bytearray((139, 62))), timeout=20) | |
603 | x = 0 |
|
641 | n = len(data) | |
604 | #while x < n: |
|
642 | print('len: ',n) | |
605 | payload = self.request('write', 'post', data=b64encode(data)) |
|
643 | x = 0 | |
606 | # x += 1024 |
|
644 | cnt = 0 | |
607 |
|
|
645 | while x < n: | |
608 | if payload['write']=='ok': |
|
646 | print('writing...', cnt) | |
609 | self.device.status = 3 |
|
647 | payload = self.request('write', 'post', data=b64encode(data[x:x+3072]))#(data))# | |
610 | self.device.save() |
|
648 | x += 3072 | |
611 | self.message = 'RC configured and started' |
|
649 | cnt += 1#time.sleep(1) | |
612 | else: |
|
650 | print('writing...', x*100/n) | |
613 | self.device.status = 1 |
|
651 | ||
614 | self.device.save() |
|
652 | if payload['write']=='ok': | |
615 | self.message = 'RC write: {}'.format(payload['write']) |
|
653 | self.device.status = 3 | |
616 | return False |
|
654 | self.device.save() | |
|
655 | self.message = 'RC configured and started' | |||
|
656 | else: | |||
|
657 | self.device.status = 1 | |||
|
658 | self.device.save() | |||
|
659 | self.message = 'RC write: {}'.format(payload['write']) | |||
|
660 | return False | |||
617 |
|
661 | |||
618 | #payload = self.request('start', 'post') |
|
662 | #payload = self.request('start', 'post') | |
619 |
|
663 | |||
620 | except Exception as e: |
|
664 | #except Exception as e: | |
621 | if 'No route to host' not in str(e): |
|
665 | # if 'No route to host' not in str(e): | |
622 | self.device.status = 4 |
|
666 | # self.device.status = 4 | |
623 | else: |
|
667 | # else: | |
624 | self.device.status = 0 |
|
668 | # self.device.status = 0 | |
625 | self.message = 'RC write: {}'.format(str(e)) |
|
669 | # self.message = 'RC write: {}'.format(str(e)) | |
626 | self.device.save() |
|
670 | # self.device.save() | |
627 | return False |
|
671 | # return False | |
628 |
|
672 | |||
629 | return True |
|
673 | return True | |
630 |
|
674 | |||
@@ -663,8 +707,8 class RCLineType(models.Model): | |||||
663 |
|
707 | |||
664 | class RCLine(models.Model): |
|
708 | class RCLine(models.Model): | |
665 |
|
709 | |||
666 | rc_configuration = models.ForeignKey(RCConfiguration, on_delete=models.CASCADE) |
|
710 | rc_configuration = models.ForeignKey('RCConfiguration', on_delete=models.CASCADE) | |
667 | line_type = models.ForeignKey(RCLineType) |
|
711 | line_type = models.ForeignKey('RCLineType',on_delete=models.CASCADE) | |
668 | channel = models.PositiveIntegerField(default=0) |
|
712 | channel = models.PositiveIntegerField(default=0) | |
669 | position = models.PositiveIntegerField(default=0) |
|
713 | position = models.PositiveIntegerField(default=0) | |
670 | params = models.TextField(default='{}') |
|
714 | params = models.TextField(default='{}') | |
@@ -780,7 +824,8 class RCLine(models.Model): | |||||
780 |
|
824 | |||
781 | if self.line_type.name=='tr': |
|
825 | if self.line_type.name=='tr': | |
782 | tr_params = json.loads(self.params) |
|
826 | tr_params = json.loads(self.params) | |
783 |
|
827 | #print(tr_params) | ||
|
828 | #print(tr_params['TX_ref']) | |||
784 | if tr_params['TX_ref'] in ('0', 0): |
|
829 | if tr_params['TX_ref'] in ('0', 0): | |
785 | txs = self.get_lines(line_type__name='tx') |
|
830 | txs = self.get_lines(line_type__name='tx') | |
786 | else: |
|
831 | else: | |
@@ -913,8 +958,15 class RCLine(models.Model): | |||||
913 | ops = [value.split('|')[2] for value in values] |
|
958 | ops = [value.split('|')[2] for value in values] | |
914 | delays = [value.split('|')[3] for value in values] |
|
959 | delays = [value.split('|')[3] for value in values] | |
915 | masks = [value.split('|')[4] for value in values] |
|
960 | masks = [value.split('|')[4] for value in values] | |
|
961 | print("masks") | |||
|
962 | print(masks) | |||
|
963 | print('{:8b}'.format(int(masks[0]))) | |||
916 | mask = list('{:8b}'.format(int(masks[0]))) |
|
964 | mask = list('{:8b}'.format(int(masks[0]))) | |
|
965 | print("mask") | |||
|
966 | print(mask) | |||
917 | mask.reverse() |
|
967 | mask.reverse() | |
|
968 | print("mask reverse") | |||
|
969 | print(mask) | |||
918 | if mask[self.channel] in ('0', '', ' '): |
|
970 | if mask[self.channel] in ('0', '', ' '): | |
919 | y = np.zeros(confs[0].total_units, dtype=np.int8) |
|
971 | y = np.zeros(confs[0].total_units, dtype=np.int8) | |
920 | else: |
|
972 | else: | |
@@ -1019,9 +1071,9 class RCLine(models.Model): | |||||
1019 |
|
1071 | |||
1020 | class RCClock(models.Model): |
|
1072 | class RCClock(models.Model): | |
1021 |
|
1073 | |||
1022 | rc_configuration = models.ForeignKey(RCConfiguration, on_delete=models.CASCADE) |
|
1074 | rc_configuration = models.ForeignKey('RCConfiguration', on_delete=models.CASCADE) | |
1023 | mode = models.BooleanField(default=True, choices=((True, 'Auto'), (False, 'Manual'))) |
|
1075 | mode = models.BooleanField(default=True, choices=((True, 'Auto'), (False, 'Manual'))) | |
1024 | multiplier = models.PositiveIntegerField(default=60) |
|
1076 | multiplier = models.PositiveIntegerField(default=60) | |
1025 | divisor = models.PositiveIntegerField(default=10) |
|
1077 | divisor = models.PositiveIntegerField(default=10) | |
1026 | reference = models.PositiveSmallIntegerField(default=1, choices=((0, 'Internal (25MHz)'), (1, 'External (10MHz)'))) |
|
1078 | reference = models.PositiveSmallIntegerField(default=1, choices=((0, 'Internal (25MHz)'), (1, 'External (10MHz)'))) | |
1027 | frequency = models.FloatField(default=60.0) No newline at end of file |
|
1079 | frequency = models.FloatField(default=60.0) |
General Comments 0
You need to be logged in to leave comments.
Login now