-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathEOappEncodersReader.c
2022 lines (1595 loc) · 77.7 KB
/
EOappEncodersReader.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2011 Department of Robotics Brain and Cognitive Sciences - Istituto Italiano di Tecnologia
* Author: Valentina Gaggero, Davide Pollarolo, marco accame
* website: www.robotcub.org
* Permission is granted to copy, distribute, and/or modify this program
* under the terms of the GNU General Public License, version 2 or any
* later version published by the Free Software Foundation.
*
* A copy of the license can be found at
* http://www.robotcub.org/icub/license/gpl.txt
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details
*/
// --------------------------------------------------------------------------------------------------------------------
// - doxy
// --------------------------------------------------------------------------------------------------------------------
/* @file eo_appl_encodersReaderModule.c
@brief This file implements emcodersReader Module.
@date 02/17/2012
**/
// --------------------------------------------------------------------------------------------------------------------
// - external dependencies
// --------------------------------------------------------------------------------------------------------------------
#include "stdlib.h"
#include "string.h"
#include "hal_common.h"
#include "hal_spiencoder.h"
#include "hal_quadencoder.h"
#include "hal_adc.h"
#include "EOtheEntities.h"
#include "EOconstarray.h"
#include "EoError.h"
#include "EOtheErrorManager.h"
#include "EOVtheSystem.h"
// --------------------------------------------------------------------------------------------------------------------
// - declaration of extern public interface
// --------------------------------------------------------------------------------------------------------------------
#include "EOappEncodersReader.h"
// --------------------------------------------------------------------------------------------------------------------
// - declaration of extern hidden interface
// --------------------------------------------------------------------------------------------------------------------
#include "EOappEncodersReader_hid.h"
// --------------------------------------------------------------------------------------------------------------------
// - #define with internal scope
// --------------------------------------------------------------------------------------------------------------------
#define CHECK_ENC_IS_ON_SPI(type) ((eomc_enc_aea3 == (type)) || (eomc_enc_aea == (type)) || (eomc_enc_aksim2 == (type)) || (eomc_enc_amo == (type)) || (eomc_enc_spichainof2 == (type)) || (eomc_enc_spichainof3 == (type)))
#define CHECK_ENC_IS_ON_STREAMED_SPI_WITHOTHERS(type) ((eomc_enc_aea3 == (type)) || (eomc_enc_aea == (type)) || (eomc_enc_aksim2 == (type)) || (eomc_enc_amo == (type)))
#define CHECK_ENC_IS_ON_STREAMED_SPI_ALONE(type) ((eomc_enc_spichainof2 == (type)) || (eomc_enc_spichainof3 == (type)))
#if defined(TESTethboardsfix)
#define FAKE_AEA
#endif
#if defined(TEST_diagn_mode_MC_AMOyarp)
#define FAKE_AMO
#endif
// --------------------------------------------------------------------------------------------------------------------
// - definition (and initialisation) of extern variables. deprecated: better using _get(), _set() on static variables
// --------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------
// - typedef with internal scope
// --------------------------------------------------------------------------------------------------------------------
typedef struct
{
eOmc_encoder_descriptor_t* descriptor;
eOencoderreader_valueInfo_t* valueinfo;
} eOencoderProperties_t;
// --------------------------------------------------------------------------------------------------------------------
// - declaration of static functions
// --------------------------------------------------------------------------------------------------------------------
static eObool_t s_eo_isconnected(eOmc_encoder_descriptor_t *des)
{
return ((eomc_enc_none != (des->type)) && (eobrd_port_nolocal != (des->port))) ? eobool_true : eobool_false;
}
static void s_eo_clear_SPI_streams(EOappEncReader *p);
static void s_eo_deconfigure_SPI_encoders(EOappEncReader *p);
static eObool_t s_eo_configure_SPI_encoders(EOappEncReader *p);
static eObool_t s_eo_appEncReader_IsValidValue_AEA(uint32_t *valueraw, eOencoderreader_errortype_t *error);
static eObool_t s_eo_appEncReader_IsValidValue_AEA3(uint32_t *valueraw, eOencoderreader_errortype_t *error);
static eObool_t s_eo_appEncReader_IsValidValue_AKSIM2(hal_spiencoder_diagnostic_t* diag, eOencoderreader_errortype_t *error);
static eObool_t s_eo_appEncReader_IsValidValue_SPICHAIN2(uint32_t *valueraw, eOencoderreader_errortype_t *error);
static eObool_t s_eo_appEncReader_IsValidValue_SPICHAIN3(uint32_t *valueraw, eOencoderreader_errortype_t *error);
static void s_eo_appEncReader_deconfigure_NONSPI_encoders(EOappEncReader *p);
static void s_eo_appEncReader_configure_NONSPI_encoders(EOappEncReader *p);
static uint32_t s_eo_appEncReader_rescale2icubdegrees(uint32_t val_raw, uint8_t jomo, eOmc_position_t pos);
static uint32_t s_eo_appEncReader_mais_rescale2icubdegrees(EOappEncReader* p, uint32_t val_raw, uint8_t jomo);
static uint32_t s_eo_appEncReader_psc_rescale2icubdegrees(EOappEncReader* p, int16_t val_raw);
static uint32_t s_eo_appEncReader_pos_rescale2icubdegrees(EOappEncReader* p, int16_t val_raw);
static uint32_t s_eo_appEncReader_hallAdc_rescale2icubdegrees(EOappEncReader* p, uint32_t val_raw, uint8_t jomo);
static hal_spiencoder_stream_t s_eo_appEncReader_get_spi_stream(EOappEncReader* p, uint8_t port);
static void s_eo_appEncReader_init_halSPIencoders(EOappEncReader *p);
static void s_eo_appEncReader_deinit_halSPIencoders(EOappEncReader *p);
static void s_eo_appEncReader_anotherSPIread(void* arg);
static void s_eo_appEncReader_stopSPIread(void* arg);
static uint32_t s_eo_read_mais_for_port(EOappEncReader *p, uint8_t port);
static eObool_t s_eo_read_psc_for_port(EOappEncReader *p, eObrd_portpsc_t port, eOencoderreader_valueInfo_t *valueInfo);
static eObool_t s_eo_read_pos_for_port(EOappEncReader *p, eObrd_portpos_t port, eOencoderreader_valueInfo_t *valueInfo);
static hal_spiencoder_type_t s_eo_appEncReader_map_encodertype_to_halspiencodertype(eOmc_encoder_t encodertype);
static eOresult_t s_eo_appEncReader_Diagnostics_Config(EOappEncReader *p, eo_appEncReader_diagnostics_cfg_t* cfg);
// interface of amo diagnostics.
// they operate w/ s_eo_theappencreader.amodiag
static void s_eo_appEncReader_amodiag_Init();
static void s_eo_appEncReader_amodiag_Config(eOmn_serv_diagn_cfg_t dc);
static void s_eo_appEncReader_amodiag_Update(uint8_t jomo, hal_spiencoder_position_t amorawvalue, eOencoderProperties_t *prop, hal_spiencoder_diagnostic_t *dia);
static void s_eo_appEncReader_amodiag_Tick();
// --------------------------------------------------------------------------------------------------------------------
// - definition (and initialisation) of static variables
// --------------------------------------------------------------------------------------------------------------------
static EOappEncReader s_eo_theappencreader =
{
EO_INIT(.initted ) eobool_false,
EO_INIT(.active ) eobool_false,
EO_INIT(.totalnumberofencoders ) 0,
EO_INIT(.stream_map ) NULL,
EO_INIT(.config ) {0},
EO_INIT(.SPI_streams ) {{EO_INIT(.type ) hal_spiencoder_typeNONE, EO_INIT(.numberof ) 0, EO_INIT(.maxsupported ) 0, EO_INIT(.isacquiring ) eobool_false, EO_INIT(.id ) {hal_spiencoderNONE}}},
EO_INIT(.diagnostics )
{
EO_INIT(.config )
{
EO_INIT(.jomomask ) 0,
EO_INIT(.periodOKreport ) 0,
EO_INIT(.periodKOreport ) 0,
EO_INIT(.errorled ) eo_ledpulser_led_none,
EO_INIT(.errorgpio ) { EO_INIT(.port ) hal_gpio_portNONE, EO_INIT(.pin ) hal_gpio_pinNONE }
},
EO_INIT(.par64 ) {0},
EO_INIT(.par16 ) {0}
},
EO_INIT(.maisConversionFactors ) {1.0, 1.0, 1.0, 1.0},
EO_INIT(.hallAdcConversionData )
{
EO_INIT(.factors ) {1.0f, 1.0f, 1.0f, 1.0f},
EO_INIT(.offsets ) {0, 0, 0, 0}
},
EO_INIT(.amodiag)
{
EO_INIT(.enabled) eobool_true,
EO_INIT(.minimuminterval) {(100*1000), (100*1000)},
EO_INIT(.vals) { 0, 0 },
EO_INIT(.regs) { 0, 0 },
EO_INIT(.cnts) { 0, 0},
EO_INIT(.one) { 0, 0, 0, 0, 0 },
EO_INIT(.two) { 0, 0, 0, 0, 0 }
}
};
// --------------------------------------------------------------------------------------------------------------------
// - definition of extern public functions
// --------------------------------------------------------------------------------------------------------------------
extern EOappEncReader* eo_appEncReader_Initialise(void)
{
if(eobool_true == s_eo_theappencreader.initted)
{
return(&s_eo_theappencreader);
}
// do what ever is needed apart from what depends on the configuration
s_eo_theappencreader.totalnumberofencoders = 0;
// step 1: retrieve the spi mapping from hal
s_eo_theappencreader.stream_map = hal_spiencoder_stream_map_get();
if(NULL == s_eo_theappencreader.stream_map)
{
// marco.accame: put a diagnostics message about the failure
return(NULL);
}
memset(&s_eo_theappencreader.config, 0, sizeof(s_eo_theappencreader.config));
s_eo_clear_SPI_streams(&s_eo_theappencreader);
// amodiag is an extra respect to the legacy diagnostics.
// in here i just init it. later i will set its enable state
s_eo_appEncReader_amodiag_Init();
s_eo_theappencreader.initted = eobool_true;
return(&s_eo_theappencreader);
}
extern EOappEncReader* eo_appEncReader_GetHandle(void)
{
if(eobool_true == s_eo_theappencreader.initted)
{
return(&s_eo_theappencreader);
}
return(NULL);
}
extern eOresult_t eo_appEncReader_Deactivate(EOappEncReader *p)
{
if(NULL == p)
{
return(eores_NOK_nullpointer);
}
if(eobool_false == s_eo_theappencreader.active)
{
return(eores_OK);
}
// clean the things prepared with _Activate() ... in reverse order
s_eo_appEncReader_deconfigure_NONSPI_encoders(p);
s_eo_deconfigure_SPI_encoders(p);
memset(&p->config, 0, sizeof(s_eo_theappencreader.config));
p->config.numofjomos = 0;
s_eo_theappencreader.totalnumberofencoders = 0;
s_eo_theappencreader.active = eobool_false;
return(eores_OK);
}
extern eOresult_t eo_appEncReader_Activate(EOappEncReader *p, EOconstarray *arrayofjomodes, eOmn_serv_diagn_cfg_t dc)
{
if((NULL == p) || (NULL == arrayofjomodes))
{
return(eores_NOK_nullpointer);
}
if(eo_constarray_Size(arrayofjomodes) > eOappEncReader_jomos_maxnumberof)
{
return eores_NOK_generic;
}
EOconstarray* carray = eo_constarray_Load((const EOarray*)arrayofjomodes);
if(eobool_true == s_eo_theappencreader.active)
{
eo_appEncReader_Deactivate(p);
}
// ok, now the rest of the things
// 1. prepare the config
p->config.numofjomos = eo_constarray_Size(carray);
for(uint8_t i=0; i<p->config.numofjomos; i++)
{
const eOmc_jomo_descriptor_t *jomodes = (eOmc_jomo_descriptor_t*) eo_constarray_At(carray, i);
if(NULL != jomodes)
{
p->config.jomoconfig[i].encoder1des = jomodes->encoder1;
p->config.jomoconfig[i].encoder2des = jomodes->encoder2;
}
}
// 2. configure spi encoders
s_eo_configure_SPI_encoders(p);
// 3. configure other encoders
s_eo_appEncReader_configure_NONSPI_encoders(p);
s_eo_theappencreader.active = eobool_true;
// to enable the diagnostics ... use on equal to eobool_true
s_eo_appEncReader_amodiag_Config(dc);
eo_appEncReader_Diagnostics_Enable(p, (eomn_serv_diagn_mode_MC_ENC == dc.mode) ? eobool_true: eobool_false);
return(eores_OK);
}
extern eOresult_t eo_appEncReader_UpdatedMaisConversionFactors(EOappEncReader *p, uint8_t jomo, float convFactor)
{
eOappEncReader_jomoconfig_t this_jomoconfig = p->config.jomoconfig[jomo];
if(NULL == p)
{
return(eores_NOK_nullpointer);
}
// check existence for primary encoder
if((eomc_enc_mais != this_jomoconfig.encoder1des.type) && (eomc_enc_mais != this_jomoconfig.encoder2des.type))
{
return(eores_NOK_unsupported);
}
p->maisConversionFactors[jomo] = convFactor;
return(eores_OK);
}
extern eOresult_t eo_appEncReader_UpdatedHallAdcConversionFactors(EOappEncReader *p, uint8_t jomo, float convFactor)
{
eOappEncReader_jomoconfig_t this_jomoconfig = p->config.jomoconfig[jomo];
if(NULL == p)
{
return(eores_NOK_nullpointer);
}
// check existence for primary encoder
if (eomc_enc_absanalog != this_jomoconfig.encoder1des.type)
{
return(eores_NOK_unsupported);
}
p->hallAdcConversionData.factors[jomo] = convFactor;
return(eores_OK);
}
extern eOresult_t eo_appEncReader_UpdatedHallAdcOffset(EOappEncReader *p, uint8_t jomo, int32_t offset)
{
eOappEncReader_jomoconfig_t this_jomoconfig = p->config.jomoconfig[jomo];
if(NULL == p)
{
return(eores_NOK_nullpointer);
}
// check existence for primary encoder
if (eomc_enc_absanalog != this_jomoconfig.encoder1des.type)
{
return(eores_NOK_unsupported);
}
p->hallAdcConversionData.offsets[jomo] = offset;
return(eores_OK);
}
extern eOresult_t eo_appEncReader_StartRead(EOappEncReader *p)
{
// i can start reading at most two spi encoders which are mapped into different streams
// in case we did not have any chained spi encoders, it woul be enough to set two bits now (one for stream0 and one for stream1),
// to make the last ISR of each stream reset it at end of the chain of encoder acquistion.
// in this mode, the check of eo_appEncReader_isReady() is easy: we just compare a mask to zero.
// with the use of chained spi encoders, everything goes to which encoders we use. to make it quick, i use a dedicated mask of 8 bits.
if(NULL == p)
{
return(eores_NOK_nullpointer);
}
// reset isacquiring status of the two spi streams, then start acquisition
for(uint8_t i=0; i<hal_spiencoder_streams_number; i++)
{
eOappEncReader_stream_t* thestream = &p->SPI_streams[i];
hal_spiencoder_t firstencoder = thestream->id[0];
thestream->isacquiring = eobool_false;
if(hal_spiencoderNONE != firstencoder)
{
thestream->isacquiring = eobool_true;
hal_spiencoder_read_start(firstencoder);
}
}
return(eores_OK);
}
extern eOresult_t eo_appEncReader_Diagnostics_Enable(EOappEncReader *p, eObool_t on)
{
if(NULL == p)
{
return(eores_NOK_nullpointer);
}
eo_appEncReader_diagnostics_cfg_t config;
config.jomomask = 0x0f;
config.periodOKreport = 0;
config.periodKOreport = 1;
config.errorled = eo_ledpulser_led_none;
config.errorgpio.port = hal_gpio_portNONE;
config.errorgpio.pin = hal_gpio_pinNONE;
if(eobool_false == on)
{
config.jomomask = 0;
config.periodKOreport = 0;
config.periodOKreport = 0;
}
return(s_eo_appEncReader_Diagnostics_Config(p, &config));
}
extern eOresult_t eo_appEncReader_Diagnostics_Tick(EOappEncReader *p)
{
if(NULL == p)
{
return(eores_NOK_nullpointer);
}
// this is amodiag, the diagnostics dedicated to amo encoder
s_eo_appEncReader_amodiag_Tick();
// this is the legacy diagnostics for all types of encoders
if(0 != p->diagnostics.config.jomomask)
{
eOerrmanDescriptor_t errdes = {0};
if((0 != p->diagnostics.par16[0]) || (0 != p->diagnostics.par16[1]))
{ // we have something wrong to signal
// the gpio is put on and it stays on forever, no matter what.
hal_gpio_setval(p->diagnostics.config.errorgpio, hal_gpio_valHIGH);
// the led is made pulsing once for 100 ms. we pulse again only if the situation stays wrong
if(eobool_false == eo_ledpulser_IsOn(eo_ledpulser_GetHandle(), p->diagnostics.config.errorled))
{
eo_ledpulser_Start(eo_ledpulser_GetHandle(), p->diagnostics.config.errorled, eok_reltime100ms, 1);
}
// we transmit a diagnostics message for encoder1
if((0 != p->diagnostics.par16[0]) || (0 != p->diagnostics.par64[0]))
{
errdes.code = eoerror_code_get(eoerror_category_Debug, eoerror_value_DEB_tag07);
errdes.sourcedevice = eo_errman_sourcedevice_localboard;
errdes.sourceaddress = 0;
errdes.par16 = p->diagnostics.par16[0];
errdes.par64 = p->diagnostics.par64[0];
eo_errman_Error(eo_errman_GetHandle(), eo_errortype_error, NULL, NULL, &errdes);
}
// we transmit a diagnostics message for encoder2
if((0 != p->diagnostics.par16[1]) || (0 != p->diagnostics.par64[1]))
{
errdes.code = eoerror_code_get(eoerror_category_Debug, eoerror_value_DEB_tag07);
errdes.sourcedevice = eo_errman_sourcedevice_localboard;
errdes.sourceaddress = 1;
errdes.par16 = p->diagnostics.par16[1];
errdes.par64 = p->diagnostics.par64[1];
eo_errman_Error(eo_errman_GetHandle(), eo_errortype_error, NULL, NULL, &errdes);
}
}
// reset par16[] / par64[]
p->diagnostics.par16[0] = p->diagnostics.par16[1] = 0;
p->diagnostics.par64[0] = p->diagnostics.par64[1] = 0;
}
return(eores_OK);
}
extern eOresult_t eo_appEncReader_GetValue(EOappEncReader *p, uint8_t jomo, eOencoderreader_valueInfo_t *valueInfo1, eOencoderreader_valueInfo_t *valueInfo2)
{
// reviewed by marco.accame on 07jun17
// changes:
// - return value is always OK apart form NULL params
// - error in reading is now contained inside eOencoderreader_valueInfo_t::errortype. in case of error eOencoderreader_valueInfo_t::value is .... zero.
// - eOencoderreader_errortype_t has gained many more values to better describe what is happening
// - unified equal code for encoder1 and encoder2
if((NULL == p) || (NULL == valueInfo1) || (NULL == valueInfo2))
{
return(eores_NOK_nullpointer);
}
eOencoderProperties_t encProp[2] = {NULL};
encProp[0].descriptor = &p->config.jomoconfig[jomo].encoder1des;
encProp[0].valueinfo = valueInfo1;
encProp[1].descriptor = &p->config.jomoconfig[jomo].encoder2des;
encProp[1].valueinfo = valueInfo2;
for(uint8_t i=0; i<2; i++)
{ // for each of the two encoders ....
eOencoderProperties_t prop = encProp[i];
uint16_t errorparam = 0;
// assign composedof and position
prop.valueinfo->composedof = eomc_encoder_get_numberofcomponents((eOmc_encoder_t)prop.descriptor->type);
prop.valueinfo->position = (eOmc_position_t)prop.descriptor->pos;
// so far we assume no errors and we assign 0 to all values
prop.valueinfo->errortype = encreader_err_NONE;
memset(prop.valueinfo->value, 0, sizeof(prop.valueinfo->value)); // was not 0 but: hal_NA32;
// now we compute the value(s) and adjust if we detect any errors
if(eobool_false == s_eo_isconnected(prop.descriptor))
{ // the encoder is not connected: adjust its error type and continue with other encoders in the loop
prop.valueinfo->errortype = encreader_err_NOTCONNECTED;
continue; // KEEP this continue! IT'S IMPORTANT!
}
// ok, we have a connected encoder. we see what type it is and we perform the proper acquisition
// we also retrieve its diagnostic
hal_spiencoder_diagnostic_t diagn = {0};
diagn.type = hal_spiencoder_diagnostic_type_none;
switch(prop.descriptor->type)
{
// they are eomc_enc_aea3, eomc_enc_aea, eomc_enc_amo, eomc_enc_spichainof2, eomc_enc_spichainof3, eomc_enc_qenc, eomc_enc_absanalog, eomc_enc_mais
case eomc_enc_aea:
{
hal_spiencoder_position_t spiRawValue = 0;
// hal_spiencoder_errors_flags flags = {0};
#if defined(FAKE_AEA)
spiRawValue = 0;
uint32_t ticks = (spiRawValue >> 6) & 0x0FFF;
prop.valueinfo->value[0] = s_eo_appEncReader_rescale2icubdegrees(ticks, jomo, (eOmc_position_t)prop.descriptor->pos);
#else
// if(hal_res_OK == hal_spiencoder_get_value((hal_spiencoder_t)prop.descriptor->port, &spiRawValue, &flags))
if(hal_res_OK == hal_spiencoder_get_value2((hal_spiencoder_t)prop.descriptor->port, &spiRawValue, &diagn))
{ // ok, the hal reads correctly
if(eobool_true == s_eo_appEncReader_IsValidValue_AEA(&spiRawValue, &prop.valueinfo->errortype))
{ // the spi raw reading from hal is valid. i just need to rescale it.
// marco.accame: hal_spiencoder_get_value2() gives back a value in uint32_t with only 18 bits of information (internally masked with 0x03FFFF).
// only the 12 most significant bits contain a position reading. to obtain the ticks we should do:
// ticks = (spiRawValue >> 6) & 0x0FFF;
// the resolution is now 4096 ticks per revolution.
// GOOD VALUE:
uint32_t ticks = (spiRawValue >> 6) & 0x0FFF;
prop.valueinfo->value[0] = s_eo_appEncReader_rescale2icubdegrees(ticks, jomo, (eOmc_position_t)prop.descriptor->pos);
}
else
{ // we have a valid raw value from hal but ... it is not valid after a check
errorparam = (spiRawValue >> 6) & 0x0FFF;
}
}
else
{ // we dont even have a valid reading from hal
prop.valueinfo->errortype = encreader_err_AEA_READING;
errorparam = 0xffff;
}
#endif
} break;
case eomc_enc_aea3:
{
hal_spiencoder_position_t spiRawValue = 0;
if(hal_res_OK == hal_spiencoder_get_value2((hal_spiencoder_t)prop.descriptor->port, &spiRawValue, &diagn))
{
// ok, the hal reads correctly
// check validy for aea3
if(eobool_true == s_eo_appEncReader_IsValidValue_AEA3(&spiRawValue, &prop.valueinfo->errortype))
{ // the spi raw reading from hal is valid. i just need to rescale it.
// the resolution is 16384 ticks per revolution.
// GOOD VALUE:
uint32_t ticks = (spiRawValue >> 1) & 0x3FFF;
prop.valueinfo->value[0] = s_eo_appEncReader_rescale2icubdegrees(ticks, jomo, (eOmc_position_t)prop.descriptor->pos);
}
else
{ // we have a valid raw value from hal but ... it is not valid after a check
errorparam = (spiRawValue >> 1) & 0x3FFF;
}
}
else
{ // we dont even have a valid reading from hal
prop.valueinfo->errortype = encreader_err_AEA_READING;
errorparam = 0xffff;
}
} break;
case eomc_enc_aksim2:
{
hal_spiencoder_position_t position = 0;
if(hal_res_OK == hal_spiencoder_get_value2((hal_spiencoder_t)prop.descriptor->port, &position, &diagn))
{
// check validity for aksim2
if(eobool_true == s_eo_appEncReader_IsValidValue_AKSIM2(&diagn, &prop.valueinfo->errortype))
{
// rescale the position value of the position to icubdegrees
prop.valueinfo->value[0] = s_eo_appEncReader_rescale2icubdegrees(position, jomo, (eOmc_position_t)prop.descriptor->pos);
}
else
{
// we have a valid reading from hal but ... it is not valid after a check
errorparam = diagn.info.aksim2_status_crc;
}
}
else
{ // we dont even have a valid reading from hal
prop.valueinfo->errortype = encreader_err_AEA_READING;
errorparam = 0xffff;
}
} break;
case eomc_enc_amo:
{
hal_spiencoder_position_t spiRawValue = 0;
// hal_spiencoder_errors_flags flags = {0};
#if defined(FAKE_AMO)
static int32_t cnt = 0;
spiRawValue = cnt++;
prop.valueinfo->value[0] = s_eo_appEncReader_rescale2icubdegrees(spiRawValue, jomo, (eOmc_position_t)prop.descriptor->pos);
#else
// if(hal_res_OK == hal_spiencoder_get_value((hal_spiencoder_t)prop.descriptor->port, &spiRawValue, &flags))
if(hal_res_OK == hal_spiencoder_get_value2((hal_spiencoder_t)prop.descriptor->port, &spiRawValue, &diagn))
{ // the spi raw reading is ok. i just need to rescale it.
//spiRawValue = (spiRawValue>>4) & 0xFFFF; marco.accame on 07jun17: why is there this comment? shall we remove it?
// GOOD VALUE
eOmc_joint_t *joint = (eOmc_joint_t*) eoprot_entity_ramof_get(eoprot_board_localboard, eoprot_endpoint_motioncontrol, eoprot_entity_mc_joint, jomo);
int16_t sectors = (int16_t)(joint->config.gearbox_E2J + 0.5f);
if (sectors == 32)
{
spiRawValue = (spiRawValue >> 1) & 0x3FFF;
}
else if (sectors == 64)
{
spiRawValue = spiRawValue & 0x3FFF;
}
prop.valueinfo->value[0] = s_eo_appEncReader_rescale2icubdegrees(spiRawValue, jomo, (eOmc_position_t)prop.descriptor->pos);
}
else
{ // we dont even have a valid reading from hal .....
// marco.accame: verify if the hal for amo encoder returns error also for parity or else ... so far, i just return a generic amo error
prop.valueinfo->errortype = encreader_err_AMO_GENERIC;
errorparam = 0; // we shall expand it later on...
}
#endif
// and now ... use diagn
// for legacy diagnostics
// and calls the following for amodiag
s_eo_appEncReader_amodiag_Update(jomo, spiRawValue, &prop, &diagn);
} break;
case eomc_enc_spichainof2:
{
hal_spiencoder_position_t spiRawValue = 0;
// hal_spiencoder_errors_flags flags = {0};
// if(hal_res_OK == hal_spiencoder_get_value((hal_spiencoder_t)prop.descriptor->port, &spiRawValue, &flags))
if(hal_res_OK == hal_spiencoder_get_value2((hal_spiencoder_t)prop.descriptor->port, &spiRawValue, &diagn))
{ // ok, the hal reads correctly
if(eobool_true == s_eo_appEncReader_IsValidValue_SPICHAIN2(&spiRawValue, &prop.valueinfo->errortype))
{ // the hal value is valid. i just need to rescale it.
// GOOD VALUE
// marco.accame on 07jun17: why is it not rescaled to icub-degrees?
prop.valueinfo->value[0] = (spiRawValue << 2) & 0xfff0; // it is the first encoder in the chain
prop.valueinfo->value[1] = (spiRawValue >> 14) & 0xfff0; // it is the second encoder in the chain
}
else
{ // we have a valid value from hal but ... it is not valid after a check
prop.valueinfo->errortype = prop.valueinfo->errortype;
errorparam = spiRawValue & 0xffff;
}
}
else
{ // we dont even have a valid reading from hal .....
prop.valueinfo->errortype = encreader_err_SPICHAINOF2_GENERIC;
errorparam = 0xffff;
}
} break;
case eomc_enc_spichainof3:
{
hal_spiencoder_position_t arrayof3[3] = {0};
// hal_spiencoder_errors_flags flags = {0};
// if(hal_res_OK == hal_spiencoder_get_value((hal_spiencoder_t)prop.descriptor->port, arrayof3, &flags))
if(hal_res_OK == hal_spiencoder_get_value2((hal_spiencoder_t)prop.descriptor->port, arrayof3, &diagn))
{ // ok, the hal reads correctly
if(eobool_true == s_eo_appEncReader_IsValidValue_SPICHAIN3(arrayof3, &prop.valueinfo->errortype))
{ // the hal value is valid. i just need to rescale it.
// GOOD VALUE
// marco.accame on 07jun17: why is it not rescaled to icub-degrees?
prop.valueinfo->value[0] = (arrayof3[0] << 2) & 0xfff0; // it is the first encoder in the chain
prop.valueinfo->value[1] = (arrayof3[1] << 2) & 0xfff0; // it is the second encoder in the chain
prop.valueinfo->value[2] = (arrayof3[2] << 2) & 0xfff0; // it is the third encoder in the chain
}
else
{ // we have a valid value from hal but ... it is not valid after a check
prop.valueinfo->errortype = prop.valueinfo->errortype;
errorparam = arrayof3[0] & 0xffff;
}
}
else
{ // we dont even have a valid reading from hal .....
prop.valueinfo->errortype = encreader_err_SPICHAINOF3_GENERIC;
errorparam = 0xffff;
}
} break;
case eomc_enc_qenc:
{
uint32_t val = hal_quadencoder_get_counter((hal_quadencoder_t)prop.descriptor->port);
if(hal_NA32 != val)
{ // the hal reading is ok. i just need to rescale it.
// GOOD VALUE
prop.valueinfo->value[0] = s_eo_appEncReader_rescale2icubdegrees(val, jomo, (eOmc_position_t)prop.descriptor->pos);
}
else
{ // the hal has detected a problem
prop.valueinfo->errortype = encreader_err_QENC_GENERIC;
errorparam = val & 0xffff;
}
} break;
case eomc_enc_absanalog:
{
uint32_t val = hal_adc_get_hall_sensor_analog_input_mV((uint8_t)prop.descriptor->port);
if(hal_NA32 != val)
{ // the hal reading is ok. it is the voltage from the motor port (0 - 3300mV). i just need to rescale it
// GOOD VALUE
prop.valueinfo->value[0] = s_eo_appEncReader_hallAdc_rescale2icubdegrees(p, val, jomo);
}
else
{ // the hal has detected a problem
prop.valueinfo->errortype = encreader_err_ABSANALOG_GENERIC;
errorparam = val & 0xffff;
}
} break;
case eomc_enc_mais:
{
uint32_t val = s_eo_read_mais_for_port(p, prop.descriptor->port);
//#warning marco.accame on 07jun17: when it fails s_eo_read_mais_for_port() return either hal_NA32 or 0. what is to be evaluated?
if(hal_NA32 != val)
{ // the hal reading is ok. it may be also 0. i just need to rescale it
// GOOD VALUE
prop.valueinfo->value[0] = s_eo_appEncReader_mais_rescale2icubdegrees(p, val, jomo);
}
else
{ // the port is not correct for a mais.
prop.valueinfo->errortype = encreader_err_MAIS_GENERIC;
errorparam = val & 0xffff;
}
} break;
case eomc_enc_psc:
{
eObool_t ret = s_eo_read_psc_for_port(p, (eObrd_portpsc_t)prop.descriptor->port, prop.valueinfo);
if(eobool_false == ret)
{ // the port is not correct for a PSC.
prop.valueinfo->errortype = encreader_err_PSC_GENERIC;
}
} break;
case eomc_enc_pos:
{
eObool_t ret = s_eo_read_pos_for_port(p, (eObrd_portpos_t)prop.descriptor->port, prop.valueinfo);
if(eobool_false == ret)
{ // the port is not correct for a POS.
prop.valueinfo->errortype = encreader_err_POS_GENERIC;
}
} break;
default:
{ // we have not recognised any valid encoder type
prop.valueinfo->errortype = encreader_err_GENERIC;
errorparam = 0;
} break;
}
// now we see if there is any diagnostics to send up. we eval the errortype
// we have only one diagnostics for the 4 joints.
// in par16[0] and par64[0] we put info of the 4 primary encoders.
// in par16[1] and par64[1] we put info of the 4 secondary encoders.
// so far we use prop.valueinfo->errortype but we may use also diagn for the amo
eObool_t filldiagnostics = eo_common_byte_bitcheck(p->diagnostics.config.jomomask, jomo);
if(eobool_true == filldiagnostics)
{
if(eomc_enc_amo == prop.descriptor->type)
{
// in here we send up a message only on the basis of diagn
if(hal_spiencoder_diagnostic_type_none != diagn.type)
{
// select only a nibble from diagn.type and shift the nibble up so that we can fit 4 nibbles, one for each joint
p->diagnostics.par16[i] |= ((0x0f & diagn.type)<<(4*jomo));
// select only 2 bytes: 1 from from diagn.type and one from diagn.info.value
uint64_t word = (0x0ff & diagn.type) | ((0xff & diagn.info.value) << 8);
// copy the word in correct position so that we have [word-enc3 | word-enc2 | word-enc1 | word-enc0]
p->diagnostics.par64[i] |= (word << (16*jomo));
}
}
else
{
switch(prop.valueinfo->errortype)
{
case encreader_err_NONE:
case encreader_err_NOTCONNECTED:
{ // we do nothing because reading is ok or is not done
} break;
default:
case encreader_err_GENERIC:
{ // we dont know what is happening ... we just set the flag in par16[i].
p->diagnostics.par16[i] |= (encreader_err_GENERIC<<(4*jomo)); // shift by nibbles ..
} break;
case encreader_err_AEA_READING:
case encreader_err_AEA_PARITY:
case encreader_err_AEA_CHIP:
case encreader_err_QENC_GENERIC:
case encreader_err_ABSANALOG_GENERIC:
case encreader_err_MAIS_GENERIC:
case encreader_err_PSC_GENERIC:
case encreader_err_POS_GENERIC:
case encreader_err_AMO_GENERIC:
case encreader_err_AKSIM2_CLOSE_TO_LIMITS:
case encreader_err_AKSIM2_CRC_ERROR:
case encreader_err_AKSIM2_INVALID_DATA:
case encreader_err_SPICHAINOF2_GENERIC:
case encreader_err_SPICHAINOF3_GENERIC:
{ // in such cases, we report the errortype and the errorparam that someone has prepared
p->diagnostics.par16[i] |= (prop.valueinfo->errortype<<(4*jomo)); // shift by nibbles ..
p->diagnostics.par64[i] &= (errorparam<<(16*jomo)); // shift by two bytes
} break;
}
}
}
// ok, we now go to next encoder or ... we terminate the for() loop
} // for()
// now the return value. we return always OK
return eores_OK;
}
extern eObool_t eo_appEncReader_isReady(EOappEncReader *p)
{
if(NULL == p)
{
return(eobool_true);
}
eObool_t ready = eobool_true;
if((eobool_true == p->SPI_streams[hal_spiencoder_stream0].isacquiring) || (eobool_true == p->SPI_streams[hal_spiencoder_stream1].isacquiring))
{
ready = eobool_false;
}
return(ready);
}
// --------------------------------------------------------------------------------------------------------------------
// - definition of extern hidden functions
// --------------------------------------------------------------------------------------------------------------------
// empty-section
// --------------------------------------------------------------------------------------------------------------------
// - definition of static functions
// --------------------------------------------------------------------------------------------------------------------
static void s_eo_clear_SPI_streams(EOappEncReader *p)
{
for(uint8_t i=0; i<hal_spiencoder_streams_number; i++)
{
p->SPI_streams[i].type = hal_spiencoder_typeNONE;
p->SPI_streams[i].numberof = 0;
p->SPI_streams[i].maxsupported = 0;
p->SPI_streams[i].isacquiring = eobool_false;
for(uint8_t j=0; j<(hal_spiencoder_maxnumber_in_stream+1); j++)
{
p->SPI_streams[i].id[j] = hal_spiencoderNONE;
}
}
}
static eObool_t s_eo_prepare_SPI_streams(EOappEncReader *p)
{
uint8_t i = 0;
uint8_t spiencoderportisused[hal_spiencoders_number] = {0};
s_eo_clear_SPI_streams(p);
uint8_t numberofSPIbased = 0;
for(i=0; i<p->config.numofjomos; i++)
{
const eOappEncReader_jomoconfig_t *jdes = &p->config.jomoconfig[i];
// if in the joint i-th we have any spi-based encoder which is primary or value2, then we put it into the proper spi-stream
if(CHECK_ENC_IS_ON_STREAMED_SPI_WITHOTHERS(jdes->encoder1des.type))
{
uint8_t port = jdes->encoder1des.port;
hal_spiencoder_stream_t streamnumber = s_eo_appEncReader_get_spi_stream(p, port);