Check-in [8fb9d3b213]

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

Overview
Comment:Gyro example coded.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256:8fb9d3b213f926aafb133db0920df7a299fc2bfbe66b117e7b24d5a97dc14a06
User & Date: gwlester 2018-01-23 05:31:08
Context
2018-01-24
03:56
Added low and higher level interfaces. check-in: c6c68c2381 user: gwlester tags: trunk
2018-01-23
05:31
Gyro example coded. check-in: 8fb9d3b213 user: gwlester tags: trunk
03:04
Initial check in. check-in: bcbd8c3555 user: gwlester tags: trunk
Changes

Accelerometer_Calibrate.tcl became executable.

Accelerometer_Example.tcl became executable.

Changes to Gryo_Example.tcl.

11
12
13
14
15
16
17


























































































































































































































































































## same directory.
##
## This program is based on a C program by the same name from Dexter Industries.
## See (https://github.com/DexterInd/DI_LEGO_NXT/blob/master/dIMU/RobotC/Gryo_Example.c)
##

package require piio

































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
## same directory.
##
## This program is based on a C program by the same name from Dexter Industries.
## See (https://github.com/DexterInd/DI_LEGO_NXT/blob/master/dIMU/RobotC/Gryo_Example.c)
##

package require piio

##
## These bytes set the full scale range of the gyroscope.
## it is important to define full_scale_range.  Values are:
##      0x00 - 250 dps.  Full scale range.
##      0x10 - 500 dps.  Full scale range.
##      0x30 - 2000 dps.  Full scale range.
##
array set GyroValues {
    250dps  0x00
    500dps  0x10
    2000dps 0x30
    250dps,divisor 128.0
    500dps,divisor 64.0
    2000dps,divisor 16.0
    divisor 1.0
    full_scale_range 0x30
    gryo,offset 0xD2
    gryo,x,axis 0x28
    gryo,y,axis 0x2a
    gryo,z,axis 0x2c
    rate 50.0
    freq 10.0
}

set i2cbus 1
set dIMUaddress {}
set range 2000dps

proc StartGyro {handle range} {
    global GyroValues

    set base $GyroValues(gryo,offset)

    ##
    ## Write CTRL_REG1 - Enable all axes. Disable power down.
    ##
    twowire writeregbyte $handle [expr {$base + 0x20}] 0x0F
    piio usleep 10
    set status [twowire readbyte $handle]

    ##
    ## Write CTRL_REG2 - No High Pass Filter.
    ##
    twowire writeregbyte $handle [expr {$base + 0x21}] 0x00
    piio usleep 10
    set status [twowire readbyte $handle]

    ##
    ## Write CTRL_REG3 - No interrupts.  Date ready.
    ##
    twowire writeregbyte $handle [expr {$base + 0x22}] 0x08
    piio usleep 10
    set status [twowire readbyte $handle]

    ##
    ## Write CTRL_REG4 - Specified scale range.
    ##
    twowire writeregbyte $handle [expr {$base + 0x23}] $GyroValues($range)
    piio usleep 10
    set status [twowire readbyte $handle]

    ##
    ## Write CTRL_REG5 - Enable all axes. Disable power down.
    ##
    twowire writeregbyte $handle [expr {$base + 0x24}] 0x00
    piio usleep 10
    set status [twowire readbyte $handle]

    ##
    ## Set divisor so that the output of our gyro axis readings can be turned
    ## into scaled values.
    ##
    set GyroValues(divisor) GyroValues($range,divisor)

    return;
}

##
## Gyro: gets a full axis reading, scaled to the full scale reading.  Returns
## in degrees per second.
##
proc GryoAxisGeading {axis} {
    global GyroValues

    set intValue [twowire readregword $GyroValues(handle) [expr {$base + $GyroValues(gryo,$axis,axis)}]]
    return [format {%8.3f} [expr {$intValue / $GyroValues(divisor)}]]
}

proc ClearHistory {} {
    global CurrentValue

    set CurrentValue(history) {}
}

proc ReadGyro {} {
    global GyroValues CurrentValue

    if {[info exists GyroValues(afterId)]} {
        after cancel $GyroValues(afterId)
    }
    set GyroValues(afterId) [after [expr {$GyroValues(rate) * 10}] ReadGyro]

    ##
    ## Read the GYROSCOPE
    ##
    set CurrentValue(x) [GryoAxisGeading x]
    set CurrentValue(y) [GryoAxisGeading y]
    set CurrentValue(z) [GryoAxisGeading z]

    ##
    ## See if we need to log it to the listbox widget
    ##
    if {[incr GyroValues(count)] >= $CurrentValue(freq)} {
        set GyroValues(count)
        set timeStamp [clock format [clock seconds]]
        .main.history.data insert {} end -values [list $timeStamp $CurrentValue(x) $CurrentValue(y) $CurrentValue(z)]
    }
}

proc StartReading {} {
    global i2cbus dIMUaddress range GyroValues

    set GyroValues(handle) [twowire twowire $i2cbus $dIMUaddress]
    StartGyro $GyroValues(handle) $range
    set GyroValues(count) 0
    ReadGyro

}

proc StopReading {} {
    global GyroValues

    if {[info exists GyroValues(afterId)]} {
        after cancel $GyroValues(afterId)
    }
}

proc CreateGui {} {
    wm title . "dIMU Gyroscope Example"

    ttk::frame .main
    grid configure .main -sticky nsew
    grid columnconfigure . .main -weight 1
    grid rowconfigure . .main -weight 1
    
    ttk::labelframe .main.controls -text {Controls}
    ttk::labelframe .main.current -text {Current Readings}
    ttk::labelframe .main.history -text {History}
    grid configure .main.controls -sticky nsew
    grid configure .main.current -sticky ew
    grid configure .main.history -sticky nsew
    grid rowconfigure .main .main.history -weight 1
    grid columnconfigure .main .main.history -weight 1

    CreateControlFrame .main.controls
    CreateCurrentReadingsFrame .main.current
    CreateHistoryFrame .main.history
}

proc CreateControlFrame {w} {
    ttk::label $w.busLbl \
        -text {I2C Bus:}
    ttk::combobox $w.busCMB \
        -textvariable ::i2cbus \
        -values [list 1] \
        -state readonly
    ttk::label $w.addrLbl \
        -text {iDMU Address:}
    ttk::entry $w.addrEnt \
        -width 5 \
        -textvariable ::dIMUaddress
    ttk::label $w.rangeLbl \
        -text {Range:}
    ttk::combobox $w.rangeCMB \
        -textvariable ::range \
        -values [list 250dps 500dps 2000dps] \
        -state readonly
    ttk::label $w.rateLbl1 \
        -text {Refresh Rate:}
    ttk::spinbox $w.rateEnt \
        -from 50.0 \
        -to 500.0 \
        -increment 10.0 \
        -format {%3.0f} \
        -wrap no \
        -command [format {set ::GyroValues(rate) [expr {entier([%1$s get])}]}  $w.rateEnt]
    $w.rateEnt set 50.0
    ttk::label $w.rateLbl2 \
        -text { in microseconds}
    ttk::label $w.frequencyLbl1 \
        -text {Logging Rate:}
    ttk::spinbox $w.frequencyEnt \
        -from 1.0 \
        -to 1000.0 \
        -increment 10.0 \
        -format {%4.0f} \
        -wrap no \
        -command [format {set ::GyroValues(freq) [expr {entier([%1$s get])}]}  $w.frequencyEnt]
    $w.frequencyEnt set 1.0
    ttk::label $w.frequencyLbl2 \
        -text { in seconds}
    ttk::frame $w.buttons
    ttk::button $w.buttons.start \
        -text {Start} \
        -width 6 \
        -command StartReading
    ttk::button $w.buttons.stop \
        -text {Stop} \
        -width 6 \
        -command StopReading
    ttk::button $w.buttons.reset \
        -text {Clear Log} \
        -width 10 \
        -command {.main.history.data delete [.main.history.data children {}]}
        
    grid configure $w.busLbl $w.busCMB $w.addrLbl $w.addrEnt $w.rangeLbl $w.rangeCMB x -padx 2 -sticky ew
    grid configure $w.rateLbl1 $w.rateEnt $w.rateLbl2 x x x x -padx 2 -sticky ew
    grid configure $w.frequencyLbl1 $w.frequencyEnt $w.frequencyLbl2 x x x -padx 2 -sticky ew
    grid configure $w.buttons -columnspan 7 -sticky ew -pady 3
    grid configure x $w.buttons.start x  $w.buttons.stop x  $w.buttons.reset x -sticky ew
    grid columnconfigure $w.buttons {0 2 4 6} -weight 1
    grid columnconfigure $w 6 -weight 1
        
}

proc CreateCurrentReadingsFrame {w} {
    ttk::label $w.xLabel \
        -text {X:}
    ttk::entry $w.xValue \
        -textvariable ::CurrentValue(x) \
        -state readonly
    ttk::label $w.yLabel \
        -text {Y:}
    ttk::entry $w.yValue \
        -textvariable ::CurrentValue(y) \
        -state readonly
    ttk::label $w.zLabel \
        -text {Z:}
    ttk::entry $w.zValue \
        -textvariable ::CurrentValue(z) \
        -state readonly
    grid configure $w.xLabel $w.xValue $w.yLabel $w.yValue $w.zLabel $w.zValue x -sticky ew -padx 2 -pady 1
    grid columnconfigure $w [list $w.xLabel $w.yLabel $w.zLabel] -uniform labels -weight 0
    grid columnconfigure $w [list $w.xValue $w.yValue $w.zValue] -uniform values -weight 0
    grid columnconfigure $w 6 -weight 1
}

proc CreateHistoryFrame {w} {
    ttk::treeview $w.data \
        -xscrollcommand [list $w.hsb set] \
        -yscrollcommand [list $w.vsb set] \
        -columns {timestamp xaxis yaxis zaxis} \
        -displaycolumns  {timestamp xaxis yaxis zaxis} \
        -show headings \
        -selectmode none
    ttk::scrollbar $w.vsb \
        -orient vertical \
        -command [list $w.data yview]
    ttk::scrollbar $w.hsb \
        -orient horizontal \
        -command [list $w.data xview]
    
    foreach column {timestamp xaxis yaxis zaxis} heading {"Time Stamp" "X Axis" "Y Axis" "Z Axis"} {
        $w.data column $column \
            -anchor w \
            -stretch yes
        $w.data heading $column \
            -anchor center \
            -text $heading
    }
    $w.data column timestamp \
            -anchor e

    grid configure $w.data $w.vsb -sticky nsew
    grid configure $w.hsb -sticky ew
    grid columnconfigure $w $w.data -weight 1
    grid rowconfigure $w $w.data -weight 1
}

CreateGui