-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogramtx_py.py
More file actions
654 lines (567 loc) · 24.8 KB
/
programtx_py.py
File metadata and controls
654 lines (567 loc) · 24.8 KB
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
# -*- coding: utf-8 -*-
"""Programtx.py
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1bHat9wPtppKbJA_ewoQIqskRVOx1kJxH
"""
import smbus
import time
import numpy as np
import skfuzzy as fuzz
from skfuzzy import control as ctrl
import binascii
import RPi.GPIO as GPIO
from time import sleep
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(12, GPIO.OUT, initial=GPIO.LOW)
class ADXL345:
# Global Variables
EARTH_GRAVITY_MS2 = 9.80665
GRAVITY_MS2 = None
# This is the typical scale factor in g/LSB as given in the datasheet (page 4)
SCALE_MULTIPLIER = 0.0039
# This is the bus that we use to send data over I2C
bus = smbus.SMBus(1)
address = None
DEBUG = False
# ADXL345 Registers
DATA_FORMAT = 0x31
BANDWIDTH_RATE_REG = 0x2C
POWER_CTL = 0x2D
measure = 0x08
BANDWIDTH_RATE_1600HZ = 0x0F
BANDWIDTH_RATE_800HZ = 0x0E
BANDWIDTH_RATE_400HZ = 0x0D
BANDWIDTH_RATE_200HZ = 0x0C
BANDWIDTH_RATE_100HZ = 0x0B
BANDWIDTH_RATE_50HZ = 0x0A
BANDWIDTH_RATE_25HZ = 0x09
RANGE_2G = 0x00
RANGE_4G = 0x01
RANGE_8G = 0x02
RANGE_16G = 0x03
DATAX0 = 0x32
DATAX1 = 0x33
DATAY0 = 0x34
DATAY1 = 0x35
DATAZ0 = 0x36
DATAZ1 = 0x37
hasil = None
def __init__(self, address = 0x53, base_range = RANGE_2G, base_bandwidth_rate = BANDWIDTH_RATE_100HZ):
self.GRAVITY_MS2 = self.EARTH_GRAVITY_MS2
self.address = address
self.set_bandwidth_rate(base_bandwidth_rate)
self.set_range(base_range)
self.enable_measurement()
self.fuzzy()
def enable_measurement(self):
"""Enables measurement by writing 0x08 to POWER_CTL."""
try:
self.bus.write_byte_data(self.address, self.POWER_CTL, self.measure)
except:
return -1
def disable_measurement(self):
"""Disables measurement by writing 0x00 to POWER_CTL."""
try:
self.bus.write_byte_data(self.address, self.POWER_CTL, 0x00)
except:
return -1
def read_measurement_mode(self):
"""Reads POWER_CTL.
Returns the read value.
"""
try:
return self.bus.read_byte_data(self.address, self.POWER_CTL)
except:
return -1
def set_bandwidth_rate(self, rate):
"""Changes the bandwidth rate by writing rate to BANDWIDTH_RATE_REG.
rate -- the bandwidth rate the ADXL345 will be set to. Using a
pre-defined rate is advised.
"""
try:
self.bus.write_byte_data(self.address, self.BANDWIDTH_RATE_REG, rate)
except:
return -1
def read_bandwidth_rate(self):
"""Reads BANDWIDTH_RATE_REG.
Returns the read value.
"""
try:
raw_bandwidth_rate = self.bus.read_byte_data(self.address, self.bandwidthRate)
return raw_bandwidth_rate & 0x0F
except:
return -1
# Changes the range of the ADXL345. Available ranges are 2G, 4G, 8G and 16G.
def set_range(self, range):
"""Changes the range of the ADXL345.
range -- the range to set the accelerometer to. Using a pre-defined
range is advised.
"""
value = None
try:
value = self.bus.read_byte_data(self.address, self.DATA_FORMAT)
except:
return -1
value &= ~0x0F;
value |= range;
value |= 0x08;
self.bus.write_byte_data(self.address, self.DATA_FORMAT, value)
def read_range(self, hex):
"""Reads the range the ADXL345 is currently set to.
hex -- If hex is true it wil return a hexadecimal value. If raw is false
it will return a string.
"""
raw_value = self.bus.read_byte_data(self.address, self.DATA_FORMAT)
if hex is True:
if raw_value == 8:
return self.RANGE_2G
elif raw_value == 9:
return self.RANGE_4G
elif raw_value == 10:
return self.RANGE_8G
elif raw_value == 11:
return self.RANGE_16G
elif hex is False:
if raw_value == 8:
return "2G"
elif raw_value == 9:
return "4G"
elif raw_value == 10:
return "8G"
elif raw_value == 11:
return "16G"
def fuzzy(self):
#The universe of variables and membership functions
Xaxis = ctrl.Antecedent (np.arange(-3.5, 7.5, 0.2), 'Xaxis')
Yaxis = ctrl.Antecedent(np.arange(-10.5, 15.5, 0.2), 'Yaxis')
Zaxis = ctrl.Antecedent(np.arange(-15.5, 20.5, 0.2), 'Zaxis')
BrakeLevel = ctrl.Consequent(np.arange(-0.05, 1.05, 0.2), 'BrakeLevel')
# Rules
Xaxis['HardBrake'] = fuzz.trapmf(Xaxis.universe, [-3, -3, -0.5, 1])
Xaxis['NoBrake'] = fuzz.trimf(Xaxis.universe, [-0.5, 1.5, 3.5])
Xaxis['Brake'] = fuzz.trapmf(Xaxis.universe, [2.5, 4, 11.5, 12])
Yaxis['HardBrake'] = fuzz.trapmf(Yaxis.universe, [-10, -10, 3.5, 6.5])
Yaxis['Brake'] = fuzz.trimf(Yaxis.universe, [5, 7.5, 10])
Yaxis['NoBrake'] = fuzz.trapmf(Yaxis.universe, [8.5, 11, 15, 25])
Zaxis['Brake'] = fuzz.trapmf(Zaxis.universe, [-15, -15, 4, 9])
Zaxis['NoBrake'] = fuzz.trimf(Zaxis.universe, [5, 11.5, 18])
Zaxis['HardBrake'] = fuzz.trapmf(Zaxis.universe, [12, 16, 20, 30])
BrakeLevel['NoBrake'] = fuzz.trapmf(BrakeLevel.universe, [0, 0, 0.2, 0.4])
BrakeLevel['Brake'] = fuzz.trimf(BrakeLevel.universe, [0.3, 0.5, 0.7])
BrakeLevel['HardBrake'] = fuzz.trapmf(BrakeLevel.universe, [0.6, 0.8, 1, 1])
#DECLARE THE RULES
rule1 = ctrl.Rule(Xaxis['HardBrake'] & Yaxis['HardBrake'] & Zaxis['Brake'], BrakeLevel['HardBrake'])
rule2 = ctrl.Rule(Xaxis['HardBrake'] & Yaxis['HardBrake'] & Zaxis['NoBrake'], BrakeLevel['HardBrake'])
rule3 = ctrl.Rule(Xaxis['HardBrake'] & Yaxis['HardBrake'] & Zaxis['HardBrake'], BrakeLevel['HardBrake'])
rule4 = ctrl.Rule(Xaxis['HardBrake'] & Yaxis['Brake'] & Zaxis['Brake'], BrakeLevel['Brake'])
rule5 = ctrl.Rule(Xaxis['HardBrake'] & Yaxis['Brake'] & Zaxis['NoBrake'], BrakeLevel['Brake'])
rule6 = ctrl.Rule(Xaxis['HardBrake'] & Yaxis['Brake'] & Zaxis['HardBrake'], BrakeLevel['Brake'])
rule7 = ctrl.Rule(Xaxis['HardBrake'] & Yaxis['NoBrake'] & Zaxis['Brake'], BrakeLevel['NoBrake'])
rule8 = ctrl.Rule(Xaxis['HardBrake'] & Yaxis['NoBrake'] & Zaxis['NoBrake'], BrakeLevel['NoBrake'])
rule9 = ctrl.Rule(Xaxis['HardBrake'] & Yaxis['NoBrake'] & Zaxis['HardBrake'], BrakeLevel['NoBrake'])
rule10 = ctrl.Rule(Xaxis['Brake'] & Yaxis['HardBrake'] & Zaxis['Brake'], BrakeLevel['HardBrake'])
rule11 = ctrl.Rule(Xaxis['Brake'] & Yaxis['HardBrake'] & Zaxis['NoBrake'], BrakeLevel['HardBrake'])
rule12 = ctrl.Rule(Xaxis['Brake'] & Yaxis['HardBrake'] & Zaxis['HardBrake'], BrakeLevel['HardBrake'])
rule13 = ctrl.Rule(Xaxis['Brake'] & Yaxis['Brake'] & Zaxis['Brake'], BrakeLevel['Brake'])
rule14 = ctrl.Rule(Xaxis['Brake'] & Yaxis['Brake'] & Zaxis['NoBrake'], BrakeLevel['Brake'])
rule15 = ctrl.Rule(Xaxis['Brake'] & Yaxis['Brake'] & Zaxis['HardBrake'], BrakeLevel['Brake'])
rule16 = ctrl.Rule(Xaxis['Brake'] & Yaxis['NoBrake'] & Zaxis['Brake'], BrakeLevel['NoBrake'])
rule17 = ctrl.Rule(Xaxis['Brake'] & Yaxis['NoBrake'] & Zaxis['NoBrake'], BrakeLevel['NoBrake'])
rule18 = ctrl.Rule(Xaxis['Brake'] & Yaxis['NoBrake'] & Zaxis['HardBrake'], BrakeLevel['NoBrake'])
rule19 = ctrl.Rule(Xaxis['NoBrake'] & Yaxis['HardBrake'] & Zaxis['Brake'], BrakeLevel['HardBrake'])
rule20 = ctrl.Rule(Xaxis['NoBrake'] & Yaxis['HardBrake'] & Zaxis['NoBrake'], BrakeLevel['HardBrake'])
rule21 = ctrl.Rule(Xaxis['NoBrake'] & Yaxis['HardBrake'] & Zaxis['HardBrake'], BrakeLevel['HardBrake'])
rule22 = ctrl.Rule(Xaxis['NoBrake'] & Yaxis['Brake'] & Zaxis['Brake'], BrakeLevel['Brake'])
rule23 = ctrl.Rule(Xaxis['NoBrake'] & Yaxis['Brake'] & Zaxis['NoBrake'], BrakeLevel['Brake'])
rule24 = ctrl.Rule(Xaxis['NoBrake'] & Yaxis['Brake'] & Zaxis['HardBrake'], BrakeLevel['Brake'])
rule25 = ctrl.Rule(Xaxis['NoBrake'] & Yaxis['NoBrake'] & Zaxis['Brake'], BrakeLevel['NoBrake'])
rule26 = ctrl.Rule(Xaxis['NoBrake'] & Yaxis['NoBrake'] & Zaxis['NoBrake'], BrakeLevel['NoBrake'])
rule27 = ctrl.Rule(Xaxis['NoBrake'] & Yaxis['NoBrake'] & Zaxis['HardBrake'], BrakeLevel['NoBrake'])
global brake_ctrl
brake_ctrl = ctrl.ControlSystem([rule1, rule2, rule3, rule4, rule5, rule6, rule7, rule8, rule9, rule10, rule11, rule12, rule13, rule14, rule15, rule16, rule17, rule18, rule19, rule20, rule21, rule22, rule23, rule24, rule25, rule26, rule27])
def get_all_axes(self, round = False):
"""Gets the measurement results from all the axes.
round -- if round is true it will round to 4 digits.
Returns a dictionary.
"""
# Read the raw bytes from the ADXL345
bytes = self.bus.read_i2c_block_data(self.address, self.DATAX0, 6)
#bib = self.fuzzy(brake_ctrl)
# bit shifting magic.
x = bytes[0] | (bytes[1] << 8)
if(x & (1 << 16 - 1)):
x = x - (1 << 16)
y = bytes[2] | (bytes[3] << 8)
if(y & (1 << 16 - 1)):
y = y - (1 << 16)
z = bytes[4] | (bytes[5] << 8)
if(z & (1 << 16 - 1)):
z = z - (1 << 16)
# Multiply the values by the scale multiplier to get the acceleration
# in g.
# The scale multiplier is given in the datasheet.
x = x * self.SCALE_MULTIPLIER
y = y * self.SCALE_MULTIPLIER
z = z * self.SCALE_MULTIPLIER
# Multiply the values in g by the gravity in m/s^2 to get the
# acceleration in m/s^2.
x = x * self.GRAVITY_MS2
y = y * self.GRAVITY_MS2
z = z * self.GRAVITY_MS2
# Round the values if the user wants to
# if round == True:
# x = round(x, 4)
# y = round(y, 4)
# z = round(z, 4)
braking = ctrl.ControlSystemSimulation(brake_ctrl)
braking.input['Xaxis'] = x
braking.input['Yaxis'] = y
braking.input['Zaxis'] = z
braking.compute()
#global hasil
hasil = braking.output['BrakeLevel']
# Return the correct values
if self.DEBUG == False:
return {"x": x, "y": y, "z": z, "hasil": hasil}
elif self.DEBUG == True:
return {"x": x, "y": y, "z": z, "hasil": hasil, "bytes": bytes}
else:
return {"x": x, "y": y, "z": z, "hasil" : hasil}
def get_one_value(self, value, round = False):
"""Reads one value and returns it.
value -- the value to be read. this can be 'x', 'y' or 'z'.
"""
read_register = 0x00
if value == 'x':
read_register = self.DATAX0
elif value == 'y':
read_register = self.DATAY0
elif value == 'z':
read_register = self.DATAZ0
# Read the raw bytes from the ADXL345
bytes = self.bus.read_i2c_block_data(self.address, read_register, 2)
# bit shifting magic.
val = bytes[0] | (bytes[1] << 8)
if(val & (1 << 16 - 1)):
val = val - (1 << 16)
# Multiply the value by the scale multiplier to get the acceleration
# in g.
val = val * self.SCALE_MULTIPLIER
# Multiply the value in g by the gravity in m/s^2 to get the
# acceleration in m/s^2.
val = val * self.GRAVITY_MS2
# Round the values if the user wants to
if round == True:
val = round(val, 4)
return val
# If a user runs this file just display the latest values
if __name__ == '__main__':
while True:
accelerometer = ADXL345(0x53)
axes = accelerometer.get_all_axes()
print("x: %.3f" % (axes['x']))
print("y: %.3f" % (axes['y']))
print("z: %.3f" % (axes['z']))
print("hasil: %.2f" % (axes['hasil']))
time.sleep(0.00000001)
pesan = round(axes['hasil'], 2)
pesan = pesan * 100
pesan = int(pesan)
biner = bin(int.from_bytes(pesan.encode(), 'big'))
bin2list = biner[2:]
listbin = []
for i in bin2list:
listbin.append(i)
listbin.insert(0,"0")
listbin.insert(0,"1")
for i in range(len(listbin)):
nilai = listbin[i]
if (nilai == "0"):
GPIO.output(12, GPIO.LOW)
if(nilai == "1"):
GPIO.output(12, GPIO.HIGH)
time.sleep(.001)
GPIO.output(12,GPIO.LOW)
!apt-get -qq install python-smbus python3-smbus
import smbus
import time
import numpy as np
import skfuzzy as fuzz
from skfuzzy import control as ctrl
import binascii
import RPi.GPIO as GPIO
from time import sleep
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(12, GPIO.OUT, initial=GPIO.LOW)
class ADXL345:
# Global Variables
EARTH_GRAVITY_MS2 = 9.80665
GRAVITY_MS2 = None
# This is the typical scale factor in g/LSB as given in the datasheet (page 4)
SCALE_MULTIPLIER = 0.0039
# This is the bus that we use to send data over I2C
bus = smbus.SMBus(1)
address = None
DEBUG = False
# ADXL345 Registers
DATA_FORMAT = 0x31
BANDWIDTH_RATE_REG = 0x2C
POWER_CTL = 0x2D
measure = 0x08
BANDWIDTH_RATE_1600HZ = 0x0F
BANDWIDTH_RATE_800HZ = 0x0E
BANDWIDTH_RATE_400HZ = 0x0D
BANDWIDTH_RATE_200HZ = 0x0C
BANDWIDTH_RATE_100HZ = 0x0B
BANDWIDTH_RATE_50HZ = 0x0A
BANDWIDTH_RATE_25HZ = 0x09
RANGE_2G = 0x00
RANGE_4G = 0x01
RANGE_8G = 0x02
RANGE_16G = 0x03
DATAX0 = 0x32
DATAX1 = 0x33
DATAY0 = 0x34
DATAY1 = 0x35
DATAZ0 = 0x36
DATAZ1 = 0x37
hasil = None
def __init__(self, address = 0x53, base_range = RANGE_2G, base_bandwidth_rate = BANDWIDTH_RATE_100HZ):
self.GRAVITY_MS2 = self.EARTH_GRAVITY_MS2
self.address = address
self.set_bandwidth_rate(base_bandwidth_rate)
self.set_range(base_range)
self.enable_measurement()
self.fuzzy()
def enable_measurement(self):
"""Enables measurement by writing 0x08 to POWER_CTL."""
try:
self.bus.write_byte_data(self.address, self.POWER_CTL, self.measure)
except:
return -1
def disable_measurement(self):
"""Disables measurement by writing 0x00 to POWER_CTL."""
try:
self.bus.write_byte_data(self.address, self.POWER_CTL, 0x00)
except:
return -1
def read_measurement_mode(self):
"""Reads POWER_CTL.
Returns the read value.
"""
try:
return self.bus.read_byte_data(self.address, self.POWER_CTL)
except:
return -1
def set_bandwidth_rate(self, rate):
"""Changes the bandwidth rate by writing rate to BANDWIDTH_RATE_REG.
rate -- the bandwidth rate the ADXL345 will be set to. Using a
pre-defined rate is advised.
"""
try:
self.bus.write_byte_data(self.address, self.BANDWIDTH_RATE_REG, rate)
except:
return -1
def read_bandwidth_rate(self):
"""Reads BANDWIDTH_RATE_REG.
Returns the read value.
"""
try:
raw_bandwidth_rate = self.bus.read_byte_data(self.address, self.bandwidthRate)
return raw_bandwidth_rate & 0x0F
except:
return -1
# Changes the range of the ADXL345. Available ranges are 2G, 4G, 8G and 16G.
def set_range(self, range):
"""Changes the range of the ADXL345.
range -- the range to set the accelerometer to. Using a pre-defined
range is advised.
"""
value = None
try:
value = self.bus.read_byte_data(self.address, self.DATA_FORMAT)
except:
return -1
value &= ~0x0F;
value |= range;
value |= 0x08;
self.bus.write_byte_data(self.address, self.DATA_FORMAT, value)
def read_range(self, hex):
"""Reads the range the ADXL345 is currently set to.
hex -- If hex is true it wil return a hexadecimal value. If raw is false
it will return a string.
"""
raw_value = self.bus.read_byte_data(self.address, self.DATA_FORMAT)
if hex is True:
if raw_value == 8:
return self.RANGE_2G
elif raw_value == 9:
return self.RANGE_4G
elif raw_value == 10:
return self.RANGE_8G
elif raw_value == 11:
return self.RANGE_16G
elif hex is False:
if raw_value == 8:
return "2G"
elif raw_value == 9:
return "4G"
elif raw_value == 10:
return "8G"
elif raw_value == 11:
return "16G"
def fuzzy(self):
#The universe of variables and membership functions
Xaxis = ctrl.Antecedent (np.arange(-3.5, 7.5, 0.2), 'Xaxis')
Yaxis = ctrl.Antecedent(np.arange(-10.5, 15.5, 0.2), 'Yaxis')
Zaxis = ctrl.Antecedent(np.arange(-15.5, 20.5, 0.2), 'Zaxis')
BrakeLevel = ctrl.Consequent(np.arange(-0.05, 1.05, 0.2), 'BrakeLevel')
# Rules
Xaxis['HardBrake'] = fuzz.trapmf(Xaxis.universe, [-3, -3, -0.5, 1])
Xaxis['NoBrake'] = fuzz.trimf(Xaxis.universe, [-0.5, 1.5, 3.5])
Xaxis['Brake'] = fuzz.trapmf(Xaxis.universe, [2.5, 4, 11.5, 12])
Yaxis['HardBrake'] = fuzz.trapmf(Yaxis.universe, [-10, -10, 3.5, 6.5])
Yaxis['Brake'] = fuzz.trimf(Yaxis.universe, [5, 7.5, 10])
Yaxis['NoBrake'] = fuzz.trapmf(Yaxis.universe, [8.5, 11, 15, 25])
Zaxis['Brake'] = fuzz.trapmf(Zaxis.universe, [-15, -15, 4, 9])
Zaxis['NoBrake'] = fuzz.trimf(Zaxis.universe, [5, 11.5, 18])
Zaxis['HardBrake'] = fuzz.trapmf(Zaxis.universe, [12, 16, 20, 30])
BrakeLevel['NoBrake'] = fuzz.trapmf(BrakeLevel.universe, [0, 0, 0.2, 0.4])
BrakeLevel['Brake'] = fuzz.trimf(BrakeLevel.universe, [0.3, 0.5, 0.7])
BrakeLevel['HardBrake'] = fuzz.trapmf(BrakeLevel.universe, [0.6, 0.8, 1, 1])
#DECLARE THE RULES
rule1 = ctrl.Rule(Xaxis['HardBrake'] & Yaxis['HardBrake'] & Zaxis['Brake'], BrakeLevel['HardBrake'])
rule2 = ctrl.Rule(Xaxis['HardBrake'] & Yaxis['HardBrake'] & Zaxis['NoBrake'], BrakeLevel['HardBrake'])
rule3 = ctrl.Rule(Xaxis['HardBrake'] & Yaxis['HardBrake'] & Zaxis['HardBrake'], BrakeLevel['HardBrake'])
rule4 = ctrl.Rule(Xaxis['HardBrake'] & Yaxis['Brake'] & Zaxis['Brake'], BrakeLevel['Brake'])
rule5 = ctrl.Rule(Xaxis['HardBrake'] & Yaxis['Brake'] & Zaxis['NoBrake'], BrakeLevel['Brake'])
rule6 = ctrl.Rule(Xaxis['HardBrake'] & Yaxis['Brake'] & Zaxis['HardBrake'], BrakeLevel['Brake'])
rule7 = ctrl.Rule(Xaxis['HardBrake'] & Yaxis['NoBrake'] & Zaxis['Brake'], BrakeLevel['NoBrake'])
rule8 = ctrl.Rule(Xaxis['HardBrake'] & Yaxis['NoBrake'] & Zaxis['NoBrake'], BrakeLevel['NoBrake'])
rule9 = ctrl.Rule(Xaxis['HardBrake'] & Yaxis['NoBrake'] & Zaxis['HardBrake'], BrakeLevel['NoBrake'])
rule10 = ctrl.Rule(Xaxis['Brake'] & Yaxis['HardBrake'] & Zaxis['Brake'], BrakeLevel['HardBrake'])
rule11 = ctrl.Rule(Xaxis['Brake'] & Yaxis['HardBrake'] & Zaxis['NoBrake'], BrakeLevel['HardBrake'])
rule12 = ctrl.Rule(Xaxis['Brake'] & Yaxis['HardBrake'] & Zaxis['HardBrake'], BrakeLevel['HardBrake'])
rule13 = ctrl.Rule(Xaxis['Brake'] & Yaxis['Brake'] & Zaxis['Brake'], BrakeLevel['Brake'])
rule14 = ctrl.Rule(Xaxis['Brake'] & Yaxis['Brake'] & Zaxis['NoBrake'], BrakeLevel['Brake'])
rule15 = ctrl.Rule(Xaxis['Brake'] & Yaxis['Brake'] & Zaxis['HardBrake'], BrakeLevel['Brake'])
rule16 = ctrl.Rule(Xaxis['Brake'] & Yaxis['NoBrake'] & Zaxis['Brake'], BrakeLevel['NoBrake'])
rule17 = ctrl.Rule(Xaxis['Brake'] & Yaxis['NoBrake'] & Zaxis['NoBrake'], BrakeLevel['NoBrake'])
rule18 = ctrl.Rule(Xaxis['Brake'] & Yaxis['NoBrake'] & Zaxis['HardBrake'], BrakeLevel['NoBrake'])
rule19 = ctrl.Rule(Xaxis['NoBrake'] & Yaxis['HardBrake'] & Zaxis['Brake'], BrakeLevel['HardBrake'])
rule20 = ctrl.Rule(Xaxis['NoBrake'] & Yaxis['HardBrake'] & Zaxis['NoBrake'], BrakeLevel['HardBrake'])
rule21 = ctrl.Rule(Xaxis['NoBrake'] & Yaxis['HardBrake'] & Zaxis['HardBrake'], BrakeLevel['HardBrake'])
rule22 = ctrl.Rule(Xaxis['NoBrake'] & Yaxis['Brake'] & Zaxis['Brake'], BrakeLevel['Brake'])
rule23 = ctrl.Rule(Xaxis['NoBrake'] & Yaxis['Brake'] & Zaxis['NoBrake'], BrakeLevel['Brake'])
rule24 = ctrl.Rule(Xaxis['NoBrake'] & Yaxis['Brake'] & Zaxis['HardBrake'], BrakeLevel['Brake'])
rule25 = ctrl.Rule(Xaxis['NoBrake'] & Yaxis['NoBrake'] & Zaxis['Brake'], BrakeLevel['NoBrake'])
rule26 = ctrl.Rule(Xaxis['NoBrake'] & Yaxis['NoBrake'] & Zaxis['NoBrake'], BrakeLevel['NoBrake'])
rule27 = ctrl.Rule(Xaxis['NoBrake'] & Yaxis['NoBrake'] & Zaxis['HardBrake'], BrakeLevel['NoBrake'])
global brake_ctrl
brake_ctrl = ctrl.ControlSystem([rule1, rule2, rule3, rule4, rule5, rule6, rule7, rule8, rule9, rule10, rule11, rule12, rule13, rule14, rule15, rule16, rule17, rule18, rule19, rule20, rule21, rule22, rule23, rule24, rule25, rule26, rule27])
def get_all_axes(self, round = False):
"""Gets the measurement results from all the axes.
round -- if round is true it will round to 4 digits.
Returns a dictionary.
"""
# Read the raw bytes from the ADXL345
bytes = self.bus.read_i2c_block_data(self.address, self.DATAX0, 6)
#bib = self.fuzzy(brake_ctrl)
# bit shifting magic.
x = bytes[0] | (bytes[1] << 8)
if(x & (1 << 16 - 1)):
x = x - (1 << 16)
y = bytes[2] | (bytes[3] << 8)
if(y & (1 << 16 - 1)):
y = y - (1 << 16)
z = bytes[4] | (bytes[5] << 8)
if(z & (1 << 16 - 1)):
z = z - (1 << 16)
# Multiply the values by the scale multiplier to get the acceleration
# in g.
# The scale multiplier is given in the datasheet.
x = x * self.SCALE_MULTIPLIER
y = y * self.SCALE_MULTIPLIER
z = z * self.SCALE_MULTIPLIER
# Multiply the values in g by the gravity in m/s^2 to get the
# acceleration in m/s^2.
x = x * self.GRAVITY_MS2
y = y * self.GRAVITY_MS2
z = z * self.GRAVITY_MS2
# Round the values if the user wants to
# if round == True:
# x = round(x, 4)
# y = round(y, 4)
# z = round(z, 4)
braking = ctrl.ControlSystemSimulation(brake_ctrl)
braking.input['Xaxis'] = x
braking.input['Yaxis'] = y
braking.input['Zaxis'] = z
braking.compute()
#global hasil
hasil = braking.output['BrakeLevel']
# Return the correct values
if self.DEBUG == False:
return {"x": x, "y": y, "z": z, "hasil": hasil}
elif self.DEBUG == True:
return {"x": x, "y": y, "z": z, "hasil": hasil, "bytes": bytes}
else:
return {"x": x, "y": y, "z": z, "hasil" : hasil}
def get_one_value(self, value, round = False):
"""Reads one value and returns it.
value -- the value to be read. this can be 'x', 'y' or 'z'.
"""
read_register = 0x00
if value == 'x':
read_register = self.DATAX0
elif value == 'y':
read_register = self.DATAY0
elif value == 'z':
read_register = self.DATAZ0
# Read the raw bytes from the ADXL345
bytes = self.bus.read_i2c_block_data(self.address, read_register, 2)
# bit shifting magic.
val = bytes[0] | (bytes[1] << 8)
if(val & (1 << 16 - 1)):
val = val - (1 << 16)
# Multiply the value by the scale multiplier to get the acceleration
# in g.
val = val * self.SCALE_MULTIPLIER
# Multiply the value in g by the gravity in m/s^2 to get the
# acceleration in m/s^2.
val = val * self.GRAVITY_MS2
# Round the values if the user wants to
if round == True:
val = round(val, 4)
return val
# If a user runs this file just display the latest values
if __name__ == '__main__':
while True:
accelerometer = ADXL345(0x53)
axes = accelerometer.get_all_axes()
print("x: %.3f" % (axes['x']))
print("y: %.3f" % (axes['y']))
print("z: %.3f" % (axes['z']))
print("hasil: %.2f" % (axes['hasil']))
time.sleep(0.00000001)
pesan = round(axes['hasil'], 2)
pesan = pesan * 100
pesan = int(pesan)
biner = bin(int.from_bytes(pesan.encode(), 'big'))
bin2list = biner[2:]
listbin = []
for i in bin2list:
listbin.append(i)
listbin.insert(0,"0")
listbin.insert(0,"1")
for i in range(len(listbin)):
nilai = listbin[i]
if (nilai == "0"):
GPIO.output(12, GPIO.LOW)
if(nilai == "1"):
GPIO.output(12, GPIO.HIGH)
time.sleep(.001)
GPIO.output(12,GPIO.LOW)