forked from DexterInd/GoPiGo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathline_sensor.py
More file actions
175 lines (154 loc) · 4.47 KB
/
Copy pathline_sensor.py
File metadata and controls
175 lines (154 loc) · 4.47 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
#!/usr/bin/env python
#
# GrovePi Python library
# v1.2.2
#
# This file provides the basic functions for using the GrovePi
#
# The GrovePi connects the Raspberry Pi and Grove sensors. You can learn more about GrovePi here: http://www.dexterindustries.com/GrovePi
#
# Have a question about this example? Ask on the forums here: http://www.dexterindustries.com/forum/?forum=grovepi
#
# LICENSE:
# These files have been made available online through a [Creative Commons Attribution-ShareAlike 3.0](http://creativecommons.org/licenses/by-sa/3.0/) license.
#
# Karan Nayan
# Initial Date: 13 Feb 2014
# Last Updated: 22 Jan 2015
# http://www.dexterindustries.com/
#
'''
## License
GoPiGo for the Raspberry Pi: an open source robotics platform for the Raspberry Pi.
Copyright (C) 2015 Dexter Industries
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
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.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/gpl-3.0.txt>.
'''
import smbus
import time
import math
import RPi.GPIO as GPIO
import struct
import operator
import pickle
debug =0
rev = GPIO.RPI_REVISION
if rev == 2 or rev == 3:
bus = smbus.SMBus(1)
else:
bus = smbus.SMBus(0)
# I2C Address of Arduino
address = 0x06
# Command Format
# analogRead() command format header
aRead_cmd = [3]
# This allows us to be more specific about which commands contain unused bytes
unused = 0
#Add a multipler to each sensor
multp=[-10,-5,0,5,10]
#TRAIN for the first time
#reading when all sensors are at white
# white=[767,815,859,710,700]
white_line=[0]*5
#reading when all sensors are black
# black=[1012,1013,1015,1003,1004]
black_line=[0]*5
#difference between black and white
# range_col=list(map(operator.sub, black, white))
range_col=[0]*5
file_b='black_line.txt'
file_w='white_line.txt'
file_r='range_line.txt'
# Function declarations of the various functions used for encoding and sending
# data from RPi to Arduino
# Write I2C block
def write_i2c_block(address, block):
try:
return bus.write_i2c_block_data(address, 1, block)
except IOError:
if debug:
print "IOError"
return -1
def read_sensor():
try:
#if sensor>=0 and sensor <=4:
bus.write_i2c_block_data(address, 1, aRead_cmd + [unused, unused, unused])
#time.sleep(.1)
#bus.read_byte(address)
number = bus.read_i2c_block_data(address, 1)
#time.sleep(.05)
return number[0]* 256 + number[1],number[2]* 256 + number[3],number[4]* 256 + number[5],number[6]* 256 + number[7],number[8]* 256 + number[9]
#return number[0]* 256 + number[1]
time.sleep(.05)
except IOError:
return -1,-1,-1,-1,-1
def get_sensorval():
while True:
val=read_sensor()
if val[0]<>-1:
return val
else:
#Read once more to clear buffer and remove junk values
val=read_sensor()
def set_black_line():
global black_line,white_line,range_col
for i in range(5):
val=read_sensor()
# print val
if val[0]!=-1:
black_line=val
else:
black_line=[-1]*5
range_col=list(map(operator.sub, black_line, white_line))
with open(file_b, 'wb') as f:
pickle.dump(black_line, f)
with open(file_r, 'wb') as f:
pickle.dump(range_col, f)
def set_white_line():
global white_line,black_line,range_col
for i in range(5):
val=read_sensor()
# print val
if val[0]!=-1:
white_line=val
else:
white_line=[-1]*5
range_col=list(map(operator.sub, black_line, white_line))
with open(file_w, 'wb') as f:
pickle.dump(white_line, f)
with open(file_r, 'wb') as f:
pickle.dump(range_col, f)
def line_position():
global black_line,white_line,range_col
#load default values from files
try:
with open(file_b, 'rb') as f:
black_line = pickle.load(f)
except:
black_line=[0]*5
try:
with open(file_w, 'rb') as f:
white_line = pickle.load(f)
except:
white_line=[0]*5
try:
with open(file_r, 'rb') as f:
range_col = pickle.load(f)
except:
range_col=[0]*5
curr=get_sensorval()
diff_val=list(map(operator.sub, curr, white_line))
curr_pos=0
percent_black_line=[0]*5
for i in range(5):
percent_black_line[i]=diff_val[i]*100/range_col[i]
curr_pos+=percent_black_line[i]*multp[i]
return curr_pos