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