Check-in [bfc9ec1028]

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:changes for dIMU initial implementation of gryoscope.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256:bfc9ec1028c3494239d6c76d7460ab7cfd9eb2d0ab4694fbcdb92b2416bc5360
User & Date: gwlester 2018-01-29 06:15:40
Context
2018-01-30
02:55
Corrections. check-in: c34bc36854 user: gwlester tags: trunk
2018-01-29
06:15
changes for dIMU initial implementation of gryoscope. check-in: bfc9ec1028 user: gwlester tags: trunk
2018-01-25
00:26
Working bno055 module and Gryo Exmaple program using it. check-in: ba6362892d user: gwlester tags: trunk
Changes

Changes to bno055-1.0.0.tm.

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
..
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
...
154
155
156
157
158
159
160

161
162
163
164
165
166
167
168
169
...
324
325
326
327
328
329
330
331

332
333
334
335
336
337
338
...
355
356
357
358
359
360
361

362
363
364
365
366
367
368
...
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
        main,chipId     {0 0x00 b}
        acc,chipId      {0 0x01 b}
        mag,chipId      {0 0x02 b}
        gyro,chipId     {0 0x03 b}
        sw,rev          {0 0x04 w}
        bl,rev          {0 0x06 b}
        page            {0 0x07 b}
        acc,data,x      {0 0x08 w}
        acc,data,y      {0 0x0a w}
        acc,data,z      {0 0x0c w}
        mag,data,x      {0 0x0e w}
        mag,data,y      {0 0x10 w}
        mag,data,z      {0 0x12 w}
        gyr,data,x      {0 0x04 w}
        gyr,data,y      {0 0x16 w}
        gyr,data,z      {0 0x18 w}
        heading         {0 0x1a w}
        roll            {0 0x1c w}
        pitch           {0 0x1e w}
        qua,data,w      {0 0x20 w}
        qua,data,x      {0 0x22 w}
        qua,data,y      {0 0x24 w}
        qua,data,z      {0 0x26 w}
        lia,data,x      {0 0x28 w}
        lia,data,y      {0 0x2a w}
        lia,data,z      {0 0x2c w}
        grv,data,x      {0 0x2e w}
        grv,data,y      {0 0x30 w}
        grv,data,z      {0 0x32 w}
        temp,data       {0 0x34 b}
        mag,cab,status  {0 0x35 b 1:0}
        acc,cab,status  {0 0x35 b 3:2}
        gyr,cab,status  {0 0x35 b 5:4}
        sys,cab,status  {0 0x35 b 7:6}
        acc,selftest    {0 0x36 b 0:0}
        mag,selftest    {0 0x36 b 1:1}
        gyr,selftest    {0 0x36 b 2:2}
................................................................................
        tempSource      {0 0x40 b 1:0}
        remap,x,value   {0 0x41 b 1:0}
        remap,y,value   {0 0x41 b 3:2}
        remap,z,value   {0 0x41 b 5:4}
        remap,x,sign    {0 0x42 b 0:0}
        remap,y,sign    {0 0x43 b 1:1}
        remap,z,sign    {0 0x43 b 2:2}
        acc,x,offset    {0 0x55 w}
        acc,y,offset    {0 0x57 w}
        acc,z,offset    {0 0x59 w}
        mag,x,offset    {0 0x5b w}
        mag,y,offset    {0 0x5d w}
        mag,z,offset    {0 0x5f w}
        gyr,x,offset    {0 0x61 w}
        gyr,y,offset    {0 0x63 w}
        gyr,z,offset    {0 0x65 w}
        acc,radius      {0 0x67 w}
        mag,radius      {0 0x69 w}

        acc,range       {1 0x08 b 1:0}
        acc,bandwidth   {1 0x08 b 4:2}
        acc,pwrMode     {1 0x08 b 7:5}
        mag,rate        {1 0x09 b 2:0}
        mag,operMode    {1 0x09 b 4:3}
        mag,pwrMode     {1 0x09 b 6:5}
        gyr,range       {1 0x0a b 2:0}
        gyr,bandwidth   {1 0x0a b 5:3}
        gyr,pwrMode     {1 0x0b b 2:0}
        acc,sleepMode   {1 0x0c b 0:0}
        acc,sleepDur    {1 0x0c b 4:1}


        gyr,am,intMask  {1 0x0f b 2:2}
        gyr,hiR,intMask {1 0x0f b 3:3}
        acc,hiG,intMask {1 0x0f b 5:5}
        acc,am,intMask  {1 0x0f b 6:6}
        acc,nm,intMask  {1 0x0f b 7:7}
        gyr,am,intEnab  {1 0x10 b 2:2}
        gyr,hiR,intEnab {1 0x10 b 3:3}
................................................................................
        gyr,hr,x,durr   {1 0x19 b}
        gyr,hr,y,thres  {1 0x1a b 4:0}
        gyr,hr,y,hyst   {1 0x1a b 6:5}
        gyr,hr,y,durr   {1 0x1b b}
        gyr,hr,z,thres  {1 0x1c b 4:0}
        gyr,hr,z,hyst   {1 0x1c b 6:5}
        gyr,hr,z,durr   {1 0x1d b}

        gyr,am,slope    {1 0x1e b 1:0}
        gyr,am,durr     {1 0x1e b 3:2}
        
        uniqueId        {1 0x50 m 16}
    }

    array set Constants {
        CALIB_STAT,0    false
        CALIB_STAT,1    unknown
................................................................................
    }

    proc writeRegister {handle register value} {
        variable Registers

        lassign $Registers($register) page addr type mask

        ::twowire writeregbyte $handle [lindex $Registers(page) 0] $page


        if {$mask eq {}} {
            set writeValue $value
        } else {
            ##
            ## Masked values are always byte oriented
            ##
................................................................................
            m {
                ##
                ## Not supported in write mode
                ##
                throw {UNSUP WRTMODE} "Multibyte writes are not supported at this time"
            }
        }


        return;
    }

    proc writeRegisterBlock {handle args} {
        variable Registers

................................................................................
            }
            set size [expr {1 + ($highBit - $lowBit)}]
            set tmp [format "%${size}.${size}b" $value]
            set writeValue [string replace $writeValue end-$highBit end-$lowBit $tmp]
        }
        scan $writeValue {%b} writeValue

        ::twowire writeregbyte $handle [lindex $Registers(page) 0] $page


        switch -exact -- $size {
            1 {
                ::twowire writeregbyte $handle $addr $writeValue
            }
            2 {
                ::twowire writeregword $handle $addr $writeValue
            }
        }


        return;
    }




















































    proc readRegister {handle register} {
        variable Registers

        lassign $Registers($register) page addr type mask

        set result {}

        ::twowire writeregbyte $handle [lindex $Registers(page) 0] $page


        switch -exact -- $type {

            b {
                set result [::twowire readregbyte $handle $addr]

            }

            w {
                set result [::twowire readregword $handle $addr]

            }
            m {
              for {set cnt 1} {$cnt <= $mask} {incr cnt; incr addr} {
                append result [format {%2.2x} [::twowire readregbyte $handle $addr]]
              } 
            }
        }

        if {$mask ne {} && $type ne {m}} {
            lassign [split $mask {:}] highBit lowBit

            set result [string range [format {%16.16b} $result] end-$lowBit end-$highBit]
            scan $result {%b} result
        }







        return $result
    }










    namespace ensemble create -subcommands {
        getValue
        getValue2
        getSymbol

        readRegister

        writeRegister
        writeRegisterBlock
    }

}







|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|







 







|
|
|
|
|
|
|
|
|
|
|












>
>







 







>
|
|







 







|
>







 







>







 







|
>









>



>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>








|
>


>


>

>


>










>



>
>
>
>
>
>



>
>
>
>
>
>
>
>






>

>





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
..
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
...
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
...
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
...
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
...
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
        main,chipId     {0 0x00 b}
        acc,chipId      {0 0x01 b}
        mag,chipId      {0 0x02 b}
        gyro,chipId     {0 0x03 b}
        sw,rev          {0 0x04 w}
        bl,rev          {0 0x06 b}
        page            {0 0x07 b}
        acc,data,x      {0 0x08 W}
        acc,data,y      {0 0x0a W}
        acc,data,z      {0 0x0c W}
        mag,data,x      {0 0x0e W}
        mag,data,y      {0 0x10 W}
        mag,data,z      {0 0x12 W}
        gyr,data,x      {0 0x04 W}
        gyr,data,y      {0 0x16 W}
        gyr,data,z      {0 0x18 W}
        heading         {0 0x1a W}
        roll            {0 0x1c W}
        pitch           {0 0x1e W}
        qua,data,w      {0 0x20 W}
        qua,data,x      {0 0x22 W}
        qua,data,y      {0 0x24 W}
        qua,data,z      {0 0x26 W}
        lia,data,x      {0 0x28 W}
        lia,data,y      {0 0x2a W}
        lia,data,z      {0 0x2c W}
        grv,data,x      {0 0x2e W}
        grv,data,y      {0 0x30 W}
        grv,data,z      {0 0x32 W}
        temp,data       {0 0x34 B}
        mag,cab,status  {0 0x35 b 1:0}
        acc,cab,status  {0 0x35 b 3:2}
        gyr,cab,status  {0 0x35 b 5:4}
        sys,cab,status  {0 0x35 b 7:6}
        acc,selftest    {0 0x36 b 0:0}
        mag,selftest    {0 0x36 b 1:1}
        gyr,selftest    {0 0x36 b 2:2}
................................................................................
        tempSource      {0 0x40 b 1:0}
        remap,x,value   {0 0x41 b 1:0}
        remap,y,value   {0 0x41 b 3:2}
        remap,z,value   {0 0x41 b 5:4}
        remap,x,sign    {0 0x42 b 0:0}
        remap,y,sign    {0 0x43 b 1:1}
        remap,z,sign    {0 0x43 b 2:2}
        acc,x,offset    {0 0x55 W}
        acc,y,offset    {0 0x57 W}
        acc,z,offset    {0 0x59 W}
        mag,x,offset    {0 0x5b W}
        mag,y,offset    {0 0x5d W}
        mag,z,offset    {0 0x5f W}
        gyr,x,offset    {0 0x61 W}
        gyr,y,offset    {0 0x63 W}
        gyr,z,offset    {0 0x65 W}
        acc,radius      {0 0x67 W}
        mag,radius      {0 0x69 W}

        acc,range       {1 0x08 b 1:0}
        acc,bandwidth   {1 0x08 b 4:2}
        acc,pwrMode     {1 0x08 b 7:5}
        mag,rate        {1 0x09 b 2:0}
        mag,operMode    {1 0x09 b 4:3}
        mag,pwrMode     {1 0x09 b 6:5}
        gyr,range       {1 0x0a b 2:0}
        gyr,bandwidth   {1 0x0a b 5:3}
        gyr,pwrMode     {1 0x0b b 2:0}
        acc,sleepMode   {1 0x0c b 0:0}
        acc,sleepDur    {1 0x0c b 4:1}
        gyr,sleepDur    {1 0x0d b 2:1}
        gyr,autoSlpDur  {1 0x0d b 5:3}
        gyr,am,intMask  {1 0x0f b 2:2}
        gyr,hiR,intMask {1 0x0f b 3:3}
        acc,hiG,intMask {1 0x0f b 5:5}
        acc,am,intMask  {1 0x0f b 6:6}
        acc,nm,intMask  {1 0x0f b 7:7}
        gyr,am,intEnab  {1 0x10 b 2:2}
        gyr,hiR,intEnab {1 0x10 b 3:3}
................................................................................
        gyr,hr,x,durr   {1 0x19 b}
        gyr,hr,y,thres  {1 0x1a b 4:0}
        gyr,hr,y,hyst   {1 0x1a b 6:5}
        gyr,hr,y,durr   {1 0x1b b}
        gyr,hr,z,thres  {1 0x1c b 4:0}
        gyr,hr,z,hyst   {1 0x1c b 6:5}
        gyr,hr,z,durr   {1 0x1d b}
        gyr,am,thresh   {1 0x1f b 6:0}
        gyr,am,slope    {1 0x1f b 1:0}
        gyr,am,durr     {1 0x1f b 3:2}
        
        uniqueId        {1 0x50 m 16}
    }

    array set Constants {
        CALIB_STAT,0    false
        CALIB_STAT,1    unknown
................................................................................
    }

    proc writeRegister {handle register value} {
        variable Registers

        lassign $Registers($register) page addr type mask

        ::twowire writeregbyte $handle [lindex $Registers(page) 1] $page
        after 20

        if {$mask eq {}} {
            set writeValue $value
        } else {
            ##
            ## Masked values are always byte oriented
            ##
................................................................................
            m {
                ##
                ## Not supported in write mode
                ##
                throw {UNSUP WRTMODE} "Multibyte writes are not supported at this time"
            }
        }
        after 20

        return;
    }

    proc writeRegisterBlock {handle args} {
        variable Registers

................................................................................
            }
            set size [expr {1 + ($highBit - $lowBit)}]
            set tmp [format "%${size}.${size}b" $value]
            set writeValue [string replace $writeValue end-$highBit end-$lowBit $tmp]
        }
        scan $writeValue {%b} writeValue

        ::twowire writeregbyte $handle [lindex $Registers(page) 1] $page
        after 10

        switch -exact -- $size {
            1 {
                ::twowire writeregbyte $handle $addr $writeValue
            }
            2 {
                ::twowire writeregword $handle $addr $writeValue
            }
        }
        after 20

        return;
    }

    proc readMultipleRegisters {handle registerList} {
        variable Registers

        set registerValues {}
        set returnValues {}

        foreach register $registerList {
            lassign $Registers($register) page addr type mask

            if {![dict exists $registerValues page $page addr $addr]} {
                ::twowire writeregbyte $handle [lindex $Registers(page) 1] $page
                after 10
                switch -exact -- $type {
                    B -
                    b {
                        set result [::twowire readregbyte $handle $addr]
                        set size 8
                    }
                    W -
                    w {
                        set size 16
                        set result [::twowire readregword $handle $addr]
                    }
                    m {
                        dict set returnValues $register [readRegister $handle $register]
                        continue
                    }
                }
                dict set registerValues page $page addr $addr [format {%16.16b} $result]
            }
            if {$mask ne {} && $type ne {m}} {
                lassign [split $mask {:}] highBit lowBit
                set result [dict get $registerValues page $page addr $addr]
                set result [string range $result end-$lowBit end-$highBit]
                set size [expr {1 + $highBit - $lowBit}]
                scan $result {%b} result
            } else {
                set result [dict get $registerValues page $page addr $addr]
            }
            if {$type in {B W}} {
                if {[string index [format "%${size}.${size}b" $result] 0]} {
                    scan 1[string repeat 0 $size] %b max
                    incr result -$max
                }
            }
            dict set returnValues $register $result
        }

        return $returnValues
    }

    proc readRegister {handle register} {
        variable Registers

        lassign $Registers($register) page addr type mask

        set result {}

        ::twowire writeregbyte $handle [lindex $Registers(page) 1] $page
        after 20

        switch -exact -- $type {
            B -
            b {
                set result [::twowire readregbyte $handle $addr]
                set size 8
            }
            W -
            w {
                set result [::twowire readregword $handle $addr]
                set size 16
            }
            m {
              for {set cnt 1} {$cnt <= $mask} {incr cnt; incr addr} {
                append result [format {%2.2x} [::twowire readregbyte $handle $addr]]
              } 
            }
        }

        if {$mask ne {} && $type ne {m}} {
            lassign [split $mask {:}] highBit lowBit
            set size [expr {1 + $highBit - $lowBit}]
            set result [string range [format {%16.16b} $result] end-$lowBit end-$highBit]
            scan $result {%b} result
        }
        if {$type in {B W}} {
            if {[string index [format "%${size}.${size}b" $result] 0]} {
                scan 1[string repeat 0 $size] %b max
                incr result -$max
            }
        }

        return $result
    }

    proc validateHandle {handle} {
        foreach register {main,chipId acc,chipId mag,chipId gyro,chipId} expectedValue {0xa0 0xfb 0x32 0x0f} {
            if {[readRegister $handle $register] != $expectedValue} {
                throw [list NOTBNO055 $handle] "The handle, '$handle', does not refrence a bno055"
            }
        }
    }


    namespace ensemble create -subcommands {
        getValue
        getValue2
        getSymbol
        readMultipleRegisters
        readRegister
        validateHandle
        writeRegister
        writeRegisterBlock
    }

}

Changes to dimu-1.0.0.tm.

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
..
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
..
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
##
## Provide a singlton class for the Dexter Industries Inertial Motion Unit (IMU) sensor.
##
package require piio
package require bno055
package require oo::util

oo::class create dimu {
    self mixin singleton

	self method init {} {
		variable HardwiredSettings

		variable OptionDef

		set OptionDef {
            -bus        {-default 1}
			-mode		{-default {}}












		}

		set HardwiredSettings {
			address 0x28
		}





































































































		::oo::objdefine [self] deletemethod init


	}

    method ProcessArgs {inputArgs classArgsDict} {
        variable instanceInfo

        set unprocessedArgs {}
        array set missingRequiredArr {}
................................................................................
            set instanceInfo {}
        }

        ##
        ## Set the class defaults
        ##
        foreach {arg argDefList} $classArgsDict {
            if {[dict exists $instanceInfo options $arg]} {
                ##
                ## Has already been processed by a subclass
                ##
                continue
            }
            if {"-boolean" in $argDefList} {
                dict set instanceInfo options $arg false
            }
            if {"-default" in $argDefList} {
                set i [lsearch -exact $argDefList "-default"]
                dict set instanceInfo options $arg [lindex $argDefList [incr i]]
                dict set instanceInfo defaults $arg [lindex $argDefList $i]
            } else {
                dict set instanceInfo defaults $arg {}
            }
            if {"-required" in $argDefList} {
                set missingRequiredArr($arg) 1
            }

        }

        ##
        ## Process each argument
        ##
        set argCount [llength $inputArgs]

        for {set i 0} {$i < $argCount} {incr i} {
            ##
            ## If argument is a class argument, overide the default
            ##
            set arg [lindex $inputArgs $i]
            if {[dict exists $classArgsDict $arg]} {
                set argDef [dict get $classArgsDict $arg]
                if {"-boolean" in $argDef} {
                    dict set instanceInfo options $arg true
                } else {
                    dict set instanceInfo options $arg [lindex $inputArgs [incr i]]
                }
                unset -nocomplain missingRequiredArr($arg)
            } else {
                ##
                ## If argument is not a class argument, append it to the unprocessed argument
                ##
                lappend unprocessedArgs $arg
            }
................................................................................
        set missingRequired [array names missingRequiredArr]
        if {[llength $missingRequired]} {
            throw [list PTK MISSING OPTION $missingRequired] "missing required options: \"$missingRequired\""
        }

        return $unprocessedArgs
    }










    constructor {args} {
		variable instanceInfo
		variable OptionDef
        variable HardwiredSettings


        set remainingArgs [ProcessArgs $args $OptionDef]
        if {[llength $remainingArgs]} {
			set arg1 [lindex $remainingArgs 0]
			throw [list LOOKUP OPTION $arg1] "unknown option \"$arg1\""
		}

        set bus [my cget -bus]

		dict set instanceInfo handle [twowire twowire $bus $HardwiredSettings(address)]




		return
    }

    destructor {
		variable instanceInfo

		##
		## Close the handle to the device
		##
        close [dict get $instanceInfo handle]
    }
    
    
    method cget {option} {
        variable instanceInfo



        if {[dict exists $instanceInfo options $option]} {
            return [dict get $instanceInfo options $option]









        } else {
            set legalOptions [dict keys [dict get $instanceInfo options]]
            throw [list TK LOOKUP OPTION $option] "unknown option \"$option\" -- legal options are: [join $legalOptions {,}]"
        }
    }

    method configure {args} {
        variable instanceInfo



        set argLen [llength $args]
        if {$argLen == 0} {
            set returnList {}
            if {[dict exists $instanceInfo options]} {
                foreach option [lsort -dictionary [dict keys [dict get $instanceInfo options]]] {
                    ##
                    ## Order is optionName defaultValue currentValue
                    ##

                    set itemList [list $option [dict get $instanceInfo defaults $option] [dict get $instanceInfo options $option]]
                    lappend returnList $itemList







                }

            }



            return $returnList
        } elseif {$argLen == 1} {
            set option [lindex $args 0]
            if {[dict exists $instanceInfo options $option]} {
                return [dict get $instanceInfo options $option]
            } else {
                set legalOptions [dict keys [dict get $instanceInfo options]]
                throw [list LOOKUP OPTION $option] "unknown option \"$option\" -- legal options are: [join $legalOptions {,}]"
            }

        } elseif {($argLen % 2) == 0} {


            foreach {option value} $args {
                if {[dict exists $instanceInfo options $option]} {
                    dict set instanceInfo options $option $value
                    ## put in code to change the configureation











                } else {
                    set legalOptions [dict keys [dict get $instanceInfo options]]
                    throw [list LOOKUP OPTION $option] "unknown option \"$option\" -- legal options are: [join $legalOptions {,}]"
                }
            }





        }

            







        return;

    }




}







































dimu init







|
|



>



|
|
>
>
>
>
>
>
>
>
>
>
>
>






>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>

>
>







 







<
<
<
<
<
<







<
<




>






>






<
<
<
<
<
<







 







>
>
>
>
>
>
>
>




|
|
>

|






>
|
>
>
>












|
|


>
>



>
>
>
>
>
>
>
>
>

|






>
>



|
<
|
|
|
|
>
|
<
>
>
>
>
>
>
>
|
>
|
>
>
>



<
<
<
<
<
<
>

>
>
|


<
>
>
>
>
>
>
>
>
>
>
>





>
>
>
>
>
|
>
|
>
>
>
>
>
>
>



|
>
>
>
|
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>

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
...
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
...
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
##
## Provide a singlton class for the Dexter Industries Inertial Motion Unit (IMU) sensor.
##
package require piio
package require bno055
package require oo::util

::ooutil::singleton create dimu {
    #self mixin singleton

	self method init {} {
		variable HardwiredSettings
		variable ConfigurationRegisters
		variable OptionDef

		set OptionDef {
            -bus        	{-default 1}
			-power          {-register pwrMode -allowed {normal low suspend} -type PWR_MODE}
			-operation		{-register operMode -immediate yes -allowed {configmode acconly magonly gyroonly accmag accgyro maggyro amg imu compass m4g ndof_fmc_off ndof} -type OPER_MODE}
            -gryorange		{-register gyr,range -allowed {2000dps 1000dps 500dps 250dps 125dps} -type GRY_RANGE}
            -gryobandwidth	{-register gyr,bandwidth -allowed {523hz 230hz 116hz 64hz 47hz 32hz 23hz 12hz} -type GRY_BANDWIDTH}
            -gyropowermode	{-register gyr,pwrMode -allowed {normal fast_up deep_suspend suspend advanced_powersave} -type GRY_POWER_MODE}
			-gyrounits		{-register gry,units -allowed {dps rps} -type GRY_UNIT}
			-gryoautosleep	{-register gyr,autoSlpDur -allowed {4MS 5MS 8MS 10MS 15MS 20MS 40MS} -type GRY_AUTOSLP_DURR}
			-gryosleep		{-register gyr,sleepDur -allowed {4MS 5MS 8MS 10MS 15MS 18MS 20MS} -type GRY_SLEEP_DURR}
			-gryoawake		{-register gyr,am,durr -allowed {8samples 16samples 32samples 64samples} -type GYR_AWAKE_DURR}
			-gryoslope		{-register gyr,am,slope}
			-gryoxoffset	{-register gyr,x,offset}
			-gryoyoffset	{-register gyr,y,offset}
			-gryozoffset	{-register gyr,z,offset}
		}

		set HardwiredSettings {
			address 0x28
		}

        set ConfigurationRegisters {
            main,chipId
            acc,chipId
            mag,chipId
            gyro,chipId
            sw,rev
            bl,rev
            mag,cab,status
            acc,cab,status
            gyr,cab,status
            sys,cab,status
            acc,selftest
            mag,selftest
            gyr,selftest
            mcu,selftest
            sys,clk,status
            sys,status
            sys,error
            acc,units
            gyr,units
            eul,units
            temp,units
            format,opt
            operMode
            powerMode
            clockSource
            tempSource
            remap,x,value
            remap,y,value
            remap,z,value
            remap,x,sign
            remap,y,sign
            remap,z,sign
            acc,x,offset
            acc,y,offset
            acc,z,offset
            mag,x,offset
            mag,y,offset
            mag,z,offset
            gyr,x,offset
            gyr,y,offset
            gyr,z,offset
            acc,radius
            mag,radius
            acc,range
            acc,bandwidth
            acc,pwrMode
            mag,rate
            mag,operMode
            mag,pwrMode
            gyr,range
            gyr,bandwidth
            gyr,pwrMode
            acc,sleepMode
            acc,sleepDur
            gyr,am,intMask
            gyr,hiR,intMask
            acc,hiG,intMask
            acc,am,intMask
            acc,nm,intMask
            gyr,am,intEnab
            gyr,hiR,intEnab
            acc,hiG,intEnab
            acc,am,intEnab
            acc,nm,intEnab
            acc,am,thresh
            acc,am,dur
            acc,an,x
            acc,an,y
            acc,an,z
            acc,hi,x
            acc,hi,y
            acc,hi,z
            acc,hi,thresh
            acc,hi,durr
            acc,nm,thresh
            acc,smnm,enab
            acc,smnm,durr
            gyr,am,x,enab
            gyr,am,y,enab
            gyr,am,z,enab
            gyr,hr,x,enab
            gyr,hr,y,enab
            gyr,hr,z,enab
            gyr,am,filter
            gyr,hr,filter
            gyr,hr,x,thres
            gyr,hr,x,hyst
            gyr,hr,x,durr
            gyr,hr,y,thres
            gyr,hr,y,hyst
            gyr,hr,y,durr
            gyr,hr,z,thres
            gyr,hr,z,hyst
            gyr,hr,z,durr
            gyr,am,slope
            gyr,am,durr
            uniqueId
        }

		::oo::objdefine [self] deletemethod init
		

	}

    method ProcessArgs {inputArgs classArgsDict} {
        variable instanceInfo

        set unprocessedArgs {}
        array set missingRequiredArr {}
................................................................................
            set instanceInfo {}
        }

        ##
        ## Set the class defaults
        ##
        foreach {arg argDefList} $classArgsDict {






            if {"-boolean" in $argDefList} {
                dict set instanceInfo options $arg false
            }
            if {"-default" in $argDefList} {
                set i [lsearch -exact $argDefList "-default"]
                dict set instanceInfo options $arg [lindex $argDefList [incr i]]
                dict set instanceInfo defaults $arg [lindex $argDefList $i]


            }
            if {"-required" in $argDefList} {
                set missingRequiredArr($arg) 1
            }
			dict set instanceInfo legalOption $arg yes
        }

        ##
        ## Process each argument
        ##
        set argCount [llength $inputArgs]
		set blockWrite {}
        for {set i 0} {$i < $argCount} {incr i} {
            ##
            ## If argument is a class argument, overide the default
            ##
            set arg [lindex $inputArgs $i]
            if {[dict exists $classArgsDict $arg]} {






                unset -nocomplain missingRequiredArr($arg)
            } else {
                ##
                ## If argument is not a class argument, append it to the unprocessed argument
                ##
                lappend unprocessedArgs $arg
            }
................................................................................
        set missingRequired [array names missingRequiredArr]
        if {[llength $missingRequired]} {
            throw [list PTK MISSING OPTION $missingRequired] "missing required options: \"$missingRequired\""
        }

        return $unprocessedArgs
    }
    
    method ReadCurrentConfiguration {} {
		variable instanceInfo
        variable currentConfiguration
		classvariable ConfigurationRegisters
        
        set currentConfiguration [bno055 readMultipleRegisters [dict get $instanceInfo handle] $ConfigurationRegisters]
    }


    constructor {args} {
		variable instanceInfo
		classvariable OptionDef
        classvariable HardwiredSettings
        variable currentConfiguration

        set remainingArgs [my ProcessArgs $args $OptionDef]
        if {[llength $remainingArgs]} {
			set arg1 [lindex $remainingArgs 0]
			throw [list LOOKUP OPTION $arg1] "unknown option \"$arg1\""
		}

        set bus [my cget -bus]
		set handle [twowire twowire $bus [dict get $HardwiredSettings address]]
		dict set instanceInfo handle $handle
		bno055 validateHandle $handle
        my ReadCurrentConfiguration
        my configure {*}$args

		return
    }

    destructor {
		variable instanceInfo

		##
		## Close the handle to the device
		##
        close [dict get $instanceInfo handle]
    }


    method cget {option} {
        variable instanceInfo
		classvariable OptionDef
        variable currentConfiguration

        if {[dict exists $instanceInfo options $option]} {
            return [dict get $instanceInfo options $option]
        } elseif {[dict exists $instanceInfo legalOption $option]} {
			set optionDict [dict get $OptionDef $option]
			set register [dict get $optionDict -register]
			set value [dict get $currentConfiguration $register]
			if {[dict exists $OptionDef -type]} {
				set type [dict get $OptionDef -type]
				set value [bno055 getSymbol $type $value]
			}
			return $value
        } else {
            set legalOptions [dict keys [dict get $instanceInfo legalOption]]
            throw [list TK LOOKUP OPTION $option] "unknown option \"$option\" -- legal options are: [join $legalOptions {,}]"
        }
    }

    method configure {args} {
        variable instanceInfo
		classvariable OptionDef
        variable currentConfiguration

        set argLen [llength $args]
        if {$argLen == 0} {
			set returnList {}

			foreach option [lsort -dictionary [dict keys [dict get $instanceInfo legalOption]]] {
				##
				## Order is optionName defaultValue currentValue
				##
				if {[dict exists $instanceInfo options]} {
					set itemList [list $option [dict get $instanceInfo defaults $option] [dict get $instanceInfo options $option]]

				} else {
					set optionDict [dict get $OptionDef $option]
					set register [dict get $optionDict -register]
					set value [dict get $currentConfiguration $register]
					if {[dict exists $OptionDef -type]} {
						set type [dict get $OptionDef -type]
						set value [bno055 getSymbol $type $value]
					}
					set itemList [list $option {} $value]
				}

				lappend returnList $itemList
			}
            return $returnList
        } elseif {$argLen == 1} {
            set option [lindex $args 0]






            return [my cget $option]
        } elseif {($argLen % 2) == 0} {
			set configureList {}
			set immediateList {}
			foreach {option value} $args {
                if {[dict exists $instanceInfo options $option]} {
                    dict set instanceInfo options $option $value

				} elseif {[dict exists $OptionDef $option -register]} {
					if {![string is entier $value]} {
						set value [bno055 getValue2 [dict get $OptionDef $option -type] $value]
					}
					set register [dict get $OptionDef $option -register]
					dict set currentConfiguration $register $value
					if {[dict exists $OptionDef $option -immediate] && [dict get $OptionDef $option -immediate]} {
						lappend immediateList [list $register $value]
					} else {
						lappend configureList $register $value
					}
                } else {
                    set legalOptions [dict keys [dict get $instanceInfo options]]
                    throw [list LOOKUP OPTION $option] "unknown option \"$option\" -- legal options are: [join $legalOptions {,}]"
                }
            }
			set handle [dict get $instanceInfo handle]
			if {[llength $configureList]} {
				bno055 writeRegister $handle operMode [bno055 getValue2 OPER_MODE CONFIGMODE]
				foreach item $configureList {
					bno055 writeRegister $handle {*}$item
				}
				bno055 writeRegister $handle operMode [dict get $currentConfiguration operMode]
			}
			if {[llength $immediateList]} {
				foreach item $immediateList {
					bno055 writeRegister $handle {*}$item
				}
			}
        }
            
        return;

    }
	
	method readGyro {} {
        variable instanceInfo
        variable currentConfiguration
		
		set operMode [string range [bno055 getSymbol OPER_MODE [dict get $currentConfiguration operMode]] 9 end]
		if {$operMode in {CONFIGMODE ACCONLY MAGONLY COMPASS M4G}} {
			throw [list DIMU SENDIS $operMode] "Gryoscope reading are not availble when dIMU is in '$operMode' mode."
		}
		
		set results {}
		set readings [bno055 readMultipleRegisters [dict get $instanceInfo handle] {gyr,data,x gyr,data,y gyr,data,z}]
		set range [bno055 getSymbol GRY_RANGE [dict get $currentConfiguration gyr,range]]
		switch -exact -- $range {
			GRY_RANGE_125DPS {
				set divisor 256.0
			}
			GRY_RANGE_250DPS {
				set divisor 128.0
			}
			GRY_RANGE_500DPS {
				set divisor 64.0
			}
			GRY_RANGE_1000DPS {
				set divisor 32.0
			}
			GRY_RANGE_2000DPS {
				set divisor 16.0
			}
		}
		set units bno055 getSymbol GRY_UNIT [dict get $currentConfiguration gyr,units]]
		if {units eq "GRY_UNIT_DPS" } {
			dict set results units dps
		} else {
			dict set results units rps
			set divisor [expr {$divsor * 900 / 16}]
		}
		dict set results x [expr {[dict get $readings gyr,data,x] / $divisor}]
		dict set results y [expr {[dict get $readings gyr,data,y] / $divisor}]
		dict set results z [expr {[dict get $readings gyr,data,z] / $divisor}]
	}

}

dimu init