Skip to content

Commit

Permalink
flip develop to master branch for DexterOS (#304)
Browse files Browse the repository at this point in the history
* Remove i2c_mutex, it is now residing in script_tools (#287)

* Feature/easygopigo refactoring (#288)

* read_position_str() is also needed here to keep in sync with easygopigo3

* Add init_<sensor> methods to the easygopigo class so the two robots are kept in sync

* add vscode to  .gitignore

* Feature/systemwide mutex (#290)

* read_position_str() is also needed here to keep in sync with easygopigo3

* support systemwide mutex

* Bug/line follower + overall mutex (#291)

* read_position_str() is also needed here to keep in sync with easygopigo3

* support systemwide mutex

* Fix import line to its more generic form

* Fix/overallmutexcheck (#292)

* read_position_str() is also needed here to keep in sync with easygopigo3

* Query overallmutex flag every single time

* fix - remove arduino support (#293)

* fix - IR receiver on Stretch (#295)

fix - IR receiver on Stretch

* make IR Receiver in easygopigo Python3 ready (#298)
  • Loading branch information
RobertLucian committed Apr 26, 2018
1 parent 707a8a1 commit 594a2ba
Show file tree
Hide file tree
Showing 11 changed files with 151 additions and 60 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -216,3 +216,5 @@ pip-log.txt

#ATOM
.ftpconfig
.vscode/sftp.json

5 changes: 3 additions & 2 deletions Setup/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ install_dependencies() {
echo " "
feedback "Installing Dependencies"
feedback "======================="
sudo apt-get install python-pip git libi2c-dev python-serial python-rpi.gpio i2c-tools python-smbus arduino minicom libnss-mdns python-dev build-essential libffi-dev -y
sudo apt-get install python-pip git libi2c-dev python-serial python-rpi.gpio i2c-tools python-smbus minicom libnss-mdns python-dev build-essential libffi-dev -y
pip install -U RPi.GPIO
pip install pyusb
pip install numpy
Expand Down Expand Up @@ -286,7 +286,8 @@ python3 setup.py install --force
install_DHT
install_wiringpi
install_spi_i2c
install_avr
# no longer installing avr for arduino
# install_avr
install_line_follower
install_control_panel

Expand Down
62 changes: 60 additions & 2 deletions Software/Python/easygopigo.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,17 @@
def _ifMutexAcquire(mutex_enabled = False):
"""
Acquires the I2C if the ``use_mutex`` parameter of the constructor was set to ``True``.
Always acquires if system-wide mutex has been set.
"""
if mutex_enabled:
if mutex_enabled or mutex.overall_mutex()==True:
mutex.acquire()

def _ifMutexRelease(mutex_enabled = False):
"""
Releases the I2C if the ``use_mutex`` parameter of the constructor was set to ``True``.
"""
if mutex_enabled:
if mutex_enabled or mutex.overall_mutex()==True:
mutex.release()

try:
Expand Down Expand Up @@ -194,6 +196,50 @@ def trim_write(self,set_trim_to):
pass
_ifMutexRelease(self.use_mutex)

def init_light_sensor(self, port="A1"):
return LightSensor(port, gpg=self, use_mutex=self.use_mutex)

def init_sound_sensor(self, port="A1"):
return SoundSensor(port, gpg=self, use_mutex=self.use_mutex)

def init_loudness_sensor(self, port="AD1"):
return LoudnessSensor(port, gpg=self, use_mutex=self.use_mutex)

def init_ultrasonic_sensor(self, port="A1"):
return UltraSonicSensor(port, gpg=self, use_mutex=self.use_mutex)

def init_buzzer(self, port="D11"):
return Buzzer(port, gpg=self, use_mutex=self.use_mutex)

def init_led(self, port="D11"):
return Led(port, gpg=self, use_mutex=self.use_mutex)

def init_button_sensor(self, port="D11"):
return ButtonSensor(port, gpg=self, use_mutex=self.use_mutex)

def init_line_follower(self, port="I2C"):
return LineFollower(port, gpg=self, use_mutex=self.use_mutex )

def init_servo(self, port="SERVO"):
return Servo(port, gpg=self, use_mutex=self.use_mutex)

def init_distance_sensor(self, port="I2C"):
try:
from di_sensors import easy_distance_sensor
return DistanceSensor(port, gpg=self, use_mutex=self.use_mutex)
except:
print("DI Sensor library not found")
return None

def init_dht_sensor(self, port="SERIAL", sensor_type = 0):
return DHTSensor(port, self, sensor_type, use_mutex=self.use_mutex)

def init_remote(self, port="SERIAL"):
return Remote(port="SERIAL", gpg=self, use_mutex=self.use_mutex)

def init_motion_sensor(self, port="D11"):
return MotionSensor(port, gpg=self, use_mutex=self.use_mutex)



#############################################################
Expand Down Expand Up @@ -273,6 +319,12 @@ def isDigital(self):

def set_descriptor(self, descriptor):
self.descriptor = descriptor

def reconfig_bus(self):
'''
Does nothing. Placeholder for compatibility with GoPiGo3
'''
pass
##########################


Expand Down Expand Up @@ -596,6 +648,12 @@ def get_remote_code(self):
key = ir_receiver.nextcode(consume=False)
else:
key = ""
try:
# in python3, key will come in as a byte
key = key.decode("utf-8")
except AttributeError:
# in python2, key will be a string, so no need to do anything
pass

return key

Expand Down
7 changes: 4 additions & 3 deletions Software/Python/ir_remote_control/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ Connect the `IR receiver` to the Serial port on the GoPiGo. This will act as a p
## Setup the Software

In your `Raspberry Pi`, open up a terminal and enter the following commands:
1. `sudo bash lirc/install.sh`.
2. `sudo bash lirc/setup_older_version.sh` - run this script too only if your version of IR receiver `<= v1.0`.
3. `sudo bash server/install.sh`.
1. `sudo apt-get update && sudo apt-get install lirc=0.9.0~pre1-1.2 -y` - to install `lirc` package. Don't use more recent versions as the newer ones are buggy.
2. `sudo bash lirc/install.sh`.
3. `sudo bash lirc/setup_older_version.sh` - run this script too only if your version of IR receiver `<= v1.0`.
4. `sudo bash server/install.sh`.

## Enabling / Disabling Service

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
while True:
#Wait for the next IR code to arrive. The codes are queued in a buffer before printing
a= ir_receiver.nextcode()
if not isinstance(a, str):
a = a.decode('utf-8')
if len(a) !=0:
print a
print (a)
time.sleep(.1)
2 changes: 1 addition & 1 deletion Software/Python/ir_remote_control/examples/lirc_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@
#Wait for the next IR code to arrive. The codes are queued in a buffer before printing
a= lirc.nextcode()
if len(a) !=0:
print a[0]
print (a[0])
74 changes: 47 additions & 27 deletions Software/Python/ir_remote_control/server/di_ir_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import os

call("sudo /etc/init.d/lirc stop", shell=True)
debug = 1
debug = True

# class for exiting gracefully
# use as "with GracefullExiter as exiter:" and then keep on checking [exit_now]
Expand Down Expand Up @@ -322,15 +322,23 @@ def compare_with_button(inp):

# print 'sending "%s"' % str(i)
if found_flag:
print keys[key]
sock.sendall(keys[key])
print (keys[key])
try:
sock.sendall(keys[key]) # works with Python 2
except:
sock.sendall(keys[key].encode()) # works with Python 3
global saved_previous_key
saved_previous_key = keys[key]
else:
print "NO_MATCH"
sock.sendall("NO_MATCH")
print ("NO_MATCH")
try:
sock.sendall("NO_MATCH")
except TypeError:
sock.sendall("NO_MATCH".encode())
except socket.error:
print "Unable to connect to the server"
print ("Unable to connect to the server")
except:
print("something happened")
finally:
# print 'Closing socket'
sock.close()
Expand All @@ -339,7 +347,7 @@ def match_with_button(inp):
list_to_parse=[]
large_val_found=0
if debug:
print inp,len(inp)
print (inp,len(inp))

#The ir signals are 65 bytes long with either a pulse length of ~500 us or ~1600us.
#Sometime because of noise which cannot be filtered out, we have smaller chunks of signal also present in the bytearray
Expand Down Expand Up @@ -404,10 +412,18 @@ def main(process_ir):

# Read the raw value from the IR receiver
line = process_ir.stdout.readline()
if not isinstance(line, str):
line = line.decode('utf-8')

# we also remove the trailing whitespace and newlines
pulse_us_string = line[6:len(line)].rstrip()

# detect if pulse_us_string is a byte array - which implies python3
# transfer to str type if needed
if not isinstance(pulse_us_string, str):
pulse_us_string = pulse_us_string.decode('utf-8')
# print(type(pulse_us_string))

# check if we got a positive integer number
if str.isdigit(pulse_us_string):
pulse_us= int(pulse_us_string) # signal length
Expand All @@ -424,7 +440,7 @@ def main(process_ir):
# If noise was there in current pulse, just skip it
if pulse_us < noise_thresh:
if debug:
print "noise:",pulse_us
print ("noise:", pulse_us)
return

# There are 3 checks to detect the keypresses
Expand All @@ -442,7 +458,7 @@ def main(process_ir):
header_detected_flag=0
before_header_flag=0
if debug:
print "de:",pulse_us
print ("de:", pulse_us)
header_1_detected_flag=0
#*********************************************
match_with_button(detected_sig_buf)
Expand All @@ -452,21 +468,21 @@ def main(process_ir):
add_next_flag = 0
# remove last sig from buffer
if debug:
print "adding last",pulse_us,
print ("adding last", pulse_us)
try:
detected_sig_buf.pop()
except IndexError:
return
pulse_us+=last_pulse_us
if debug:
print pulse_us
print (pulse_us)

if last_sig_type == sig_type: # if a similar signal type was detected then add it in the next pulse
add_next_flag=1

if debug:
if add_next_flag ==0:
print "d:",pulse_us
print ("d:", pulse_us)

detected_sig_buf.append(pulse_us)
else:
Expand All @@ -476,42 +492,46 @@ def main(process_ir):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

server_address = ('localhost', 21852)
# print 'connecting to %s port %s' % server_address
# print ('connecting to %s port %s' % server_address)
sock.connect(server_address)

# print 'sending "%s"' % str(i)
sock.sendall(saved_previous_key)
# on python2, sendall requires a str
# on python3, if sendall receives a str it will fail, so send bytes instead
try:
sock.sendall(saved_previous_key)
except TypeError:
sock.sendall(saved_previous_key.encode())

except socket.error:
print "Unable to connect to the server"
print ("Unable to connect to the server")
finally:
# print 'Closing socket'
sock.close()
if debug:
print "n:",pulse_us
print ("n:", pulse_us)

#Third check for 4k pulse
if header_detected_flag ==1 and header_1_detected_flag == 0:
if debug:
print "checking before_header1_flag==1",pulse_us,header_1_thresh-header_1_margin,header_1_thresh+header_1_margin
print ("checking before_header1_flag==1",pulse_us,header_1_thresh-header_1_margin,header_1_thresh+header_1_margin)

if add_next_flag:
if debug:
print "adding 4k pulse"
print pulse_us,last_pulse_us
print ("adding 4k pulse")
print (pulse_us, last_pulse_us)
pulse_us+=last_pulse_us

add_next_flag=0

if pulse_us > header_1_thresh-header_1_margin and pulse_us < header_1_thresh+header_1_margin:
# IR signal detected
if debug:
print "header_1_detected_flag=1"
print ("header_1_detected_flag=1")
header_1_detected_flag=1
else:
if last_sig_type == sig_type:
if debug:
print "setting 4k pulse flag"
print ("setting 4k pulse flag")
add_next_flag=1
last_pulse_us=pulse_us
return
Expand All @@ -522,19 +542,19 @@ def main(process_ir):
#Second check for 9k pulse
if before_header_flag==1 and header_detected_flag==0:
if debug:
print "checking before_header_flag==1",pulse_us,header_thresh-header_margin,header_thresh+header_margin
print ("checking before_header_flag==1",pulse_us,header_thresh-header_margin,header_thresh+header_margin)

if add_next_flag:
pulse_us+=last_pulse_us

add_next_flag=0
if debug:
print "checking_again before_header_flag==1",pulse_us,header_thresh-header_margin,header_thresh+header_margin,last_pulse_us
print ("checking_again before_header_flag==1",pulse_us,header_thresh-header_margin,header_thresh+header_margin,last_pulse_us)

if pulse_us > header_thresh-header_margin and pulse_us < header_thresh+header_margin:
header_detected_flag=1
if debug:
print "header_detected_flag=1"
print ("header_detected_flag=1")
else:
if last_sig_type == sig_type:
add_next_flag=1
Expand All @@ -545,7 +565,7 @@ def main(process_ir):
if before_header_flag==0 and pulse_us>sig_thresh_len_before_header:
before_header_flag=1
if debug:
print "before_header_flag=1",pulse_us
print ("before_header_flag=1",pulse_us)

last_pulse_us=pulse_us
last_sig_type= sig_type
Expand All @@ -556,7 +576,7 @@ def main(process_ir):

process_ir = Popen('mode2 -d /dev/lirc0', stdout = PIPE, stderr = STDOUT, shell = True)

print "Press any key on the remote to start"
print ("Press any key on the remote to start")

try:
with GracefullExiter() as exiter:
Expand Down
8 changes: 4 additions & 4 deletions Software/Python/ir_remote_control/server/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ GOPIGO_PATH=$DEXTER_PATH/GoPiGo

#Install the ir_receiver library systemwide
pushd $GOPIGO_PATH/Software/Python/ir_remote_control/server/
sudo python setup.py install
sudo rm -r build
sudo rm -r dist
sudo rm -r ir_receiver.egg-info
python setup.py install
rm -r build
rm -r dist
rm -r ir_receiver.egg-info

sudo cp ir-server.service /etc/systemd/system/ir-server.service
sudo systemctl daemon-reload
Expand Down

0 comments on commit 594a2ba

Please sign in to comment.