diff --git a/Line_Follower.py b/Line_Follower.py index 75e68db..fa0f308 100644 --- a/Line_Follower.py +++ b/Line_Follower.py @@ -1,101 +1,103 @@ - -import smbus -import math -import time - -class Line_Follower(object): - def __init__(self, address=0x11, references=[300, 300, 300, 300, 300]): - self.bus = smbus.SMBus(1) - self.address = address - self._references = references - - def read_raw(self): - for i in range(0, 5): - try: - raw_result = self.bus.read_i2c_block_data(self.address, 0, 10) - Connection_OK = True - break - except: - Connection_OK = False - - if Connection_OK: - return raw_result - else: - return False - print "Error accessing %2X" % self.address - - def read_analog(self): - raw_result = self.read_raw() - if raw_result: - analog_result = [0, 0, 0, 0, 0] - for i in range(0, 5): - high_byte = raw_result[i*2] << 8 - low_byte = raw_result[i*2+1] - analog_result[i] = high_byte + low_byte - return analog_result - - def read_digital(self): - lt = self.read_analog() - digital_list = [] - for i in range(0, 5): - if lt[i] > self._references[i]: - digital_list.append(0) - elif lt[i] < self._references[i]: - digital_list.append(1) - else: - digital_list.append(-1) - return digital_list - - def get_average(self, mount): - if not isinstance(mount, int): - raise ValueError("Mount must be interger") - average = [0, 0, 0, 0, 0] - lt_list = [[], [], [], [], []] - for times in range(0, mount): - lt = self.read_analog() - for lt_id in range(0, 5): - lt_list[lt_id].append(lt[lt_id]) - for lt_id in range(0, 5): - average[lt_id] = int(math.fsum(lt_list[lt_id])/mount) - return average - - def found_line_in(self, timeout): - if isinstance(timeout, int) or isinstance(timeout, float): - pass - else: - raise ValueError("timeout must be interger or float") - time_start = time.time() - time_during = 0 - while time_during < timeout: - lt_status = self.read_digital() - result = 0 - if 1 in lt_status: - return lt_status - time_now = time.time() - time_during = time_now - time_start - return False - - def wait_tile_status(self, status): - while True: - lt_status = self.read_digital() - if lt_status in status: - break - - def wait_tile_center(self): - while True: - lt_status = self.read_digital() - if lt_status[2] == 1: - break - - @property - def references(self): - return self._references - - @references.setter - def references(self, value): - self._references = value - -if __name__ == '__main__': - lf = Line_Follower() - while True: - print lf.read_analog() + +from __future__ import print_function +import smbus +import math +import time +import logging + +class Line_Follower(object): + def __init__(self, address=0x11, references=[300, 300, 300, 300, 300]): + self.bus = smbus.SMBus(1) + self.address = address + self._references = references + + def read_raw(self): + for i in range(0, 5): + try: + raw_result = self.bus.read_i2c_block_data(self.address, 0, 10) + Connection_OK = True + break + except IOError: + logging.exception('Error reading') + Connection_OK = False + + if Connection_OK: + return raw_result + else: + raise RuntimeError("Error accessing %2X" % self.address) + + def read_analog(self): + raw_result = self.read_raw() + if raw_result: + analog_result = [0, 0, 0, 0, 0] + for i in range(0, 5): + high_byte = raw_result[i*2] << 8 + low_byte = raw_result[i*2+1] + analog_result[i] = high_byte + low_byte + return analog_result + + def read_digital(self): + lt = self.read_analog() + digital_list = [] + for i in range(0, 5): + if lt[i] > self._references[i]: + digital_list.append(0) + elif lt[i] < self._references[i]: + digital_list.append(1) + else: + digital_list.append(-1) + return digital_list + + def get_average(self, mount): + if not isinstance(mount, int): + raise ValueError("Mount must be interger") + average = [0, 0, 0, 0, 0] + lt_list = [[], [], [], [], []] + for times in range(0, mount): + lt = self.read_analog() + for lt_id in range(0, 5): + lt_list[lt_id].append(lt[lt_id]) + for lt_id in range(0, 5): + average[lt_id] = int(math.fsum(lt_list[lt_id])/mount) + return average + + def found_line_in(self, timeout): + if isinstance(timeout, int) or isinstance(timeout, float): + pass + else: + raise ValueError("timeout must be interger or float") + time_start = time.time() + time_during = 0 + while time_during < timeout: + lt_status = self.read_digital() + result = 0 + if 1 in lt_status: + return lt_status + time_now = time.time() + time_during = time_now - time_start + return False + + def wait_tile_status(self, status): + while True: + lt_status = self.read_digital() + if lt_status in status: + break + + def wait_tile_center(self): + while True: + lt_status = self.read_digital() + if lt_status[2] == 1: + break + + @property + def references(self): + return self._references + + @references.setter + def references(self, value): + self._references = value + +if __name__ == '__main__': + lf = Line_Follower() + while True: + print(lf.read_analog()) diff --git a/README.md b/README.md index a67b1da..6ab7193 100644 --- a/README.md +++ b/README.md @@ -1,17 +1,18 @@ -## SunFounder UltraSonic_Avoidance -SunFounder UltraSonic_Avoidance +## SunFounder Line Follower +SunFounder Line Follower Quick Links: - * [About SunFounder UltraSonic_Avoidance](#about_this_module) + * [About SunFounder Line Follower](#about_this_module) * [Update](#update) * [About SunFounder](#about_sunfounder) * [License](#license) * [Contact us](#contact_us) -### About SunFounder UltraSonic_Avoidance: -This module is for SunFounder UltraSonic_Avoidance the PCB board, 25KHz Ultra sonic avoidance module +### About SunFounder Line Follower: +This code is for SunFounder Line Follower module + ### Update: @@ -26,14 +27,14 @@ SunFounder is a technology company focused on Raspberry Pi and Arduino open sour ---------------------------------------------- ### License -This is the code for SunFounder UltraSonic_Avoidance. +This is the code for SunFounder Line Follower. 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 2 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 wa rranty 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, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. -SunFounder UltraSonic_Avoidance comes with ABSOLUTELY NO WARRANTY; for details run ./show w. This is free software, and you are welcome to redistribute it under certain conditions; run ./show c for details. +SunFounder Line Follower comes with ABSOLUTELY NO WARRANTY; for details run ./show w. This is free software, and you are welcome to redistribute it under certain conditions; run ./show c for details. SunFounder, Inc., hereby disclaims all copyright interest in the program 'SunFounder UltraSonic_Avoidance' (which makes passes at compilers). @@ -50,4 +51,4 @@ website: www.sunfounder.com E-mail: - service@sunfounder.com, support@sunfounder.com \ No newline at end of file + service@sunfounder.com, support@sunfounder.com