Skip to content

Commit 594a2ba

Browse files
authored
flip develop to master branch for DexterOS (#304)
* 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)
1 parent 707a8a1 commit 594a2ba

File tree

11 files changed

+151
-60
lines changed

11 files changed

+151
-60
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -216,3 +216,5 @@ pip-log.txt
216216

217217
#ATOM
218218
.ftpconfig
219+
.vscode/sftp.json
220+

Setup/install.sh

+3-2
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ install_dependencies() {
7676
echo " "
7777
feedback "Installing Dependencies"
7878
feedback "======================="
79-
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
79+
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
8080
pip install -U RPi.GPIO
8181
pip install pyusb
8282
pip install numpy
@@ -286,7 +286,8 @@ python3 setup.py install --force
286286
install_DHT
287287
install_wiringpi
288288
install_spi_i2c
289-
install_avr
289+
# no longer installing avr for arduino
290+
# install_avr
290291
install_line_follower
291292
install_control_panel
292293

Software/Python/easygopigo.py

+60-2
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,17 @@
2323
def _ifMutexAcquire(mutex_enabled = False):
2424
"""
2525
Acquires the I2C if the ``use_mutex`` parameter of the constructor was set to ``True``.
26+
Always acquires if system-wide mutex has been set.
27+
2628
"""
27-
if mutex_enabled:
29+
if mutex_enabled or mutex.overall_mutex()==True:
2830
mutex.acquire()
2931

3032
def _ifMutexRelease(mutex_enabled = False):
3133
"""
3234
Releases the I2C if the ``use_mutex`` parameter of the constructor was set to ``True``.
3335
"""
34-
if mutex_enabled:
36+
if mutex_enabled or mutex.overall_mutex()==True:
3537
mutex.release()
3638

3739
try:
@@ -194,6 +196,50 @@ def trim_write(self,set_trim_to):
194196
pass
195197
_ifMutexRelease(self.use_mutex)
196198

199+
def init_light_sensor(self, port="A1"):
200+
return LightSensor(port, gpg=self, use_mutex=self.use_mutex)
201+
202+
def init_sound_sensor(self, port="A1"):
203+
return SoundSensor(port, gpg=self, use_mutex=self.use_mutex)
204+
205+
def init_loudness_sensor(self, port="AD1"):
206+
return LoudnessSensor(port, gpg=self, use_mutex=self.use_mutex)
207+
208+
def init_ultrasonic_sensor(self, port="A1"):
209+
return UltraSonicSensor(port, gpg=self, use_mutex=self.use_mutex)
210+
211+
def init_buzzer(self, port="D11"):
212+
return Buzzer(port, gpg=self, use_mutex=self.use_mutex)
213+
214+
def init_led(self, port="D11"):
215+
return Led(port, gpg=self, use_mutex=self.use_mutex)
216+
217+
def init_button_sensor(self, port="D11"):
218+
return ButtonSensor(port, gpg=self, use_mutex=self.use_mutex)
219+
220+
def init_line_follower(self, port="I2C"):
221+
return LineFollower(port, gpg=self, use_mutex=self.use_mutex )
222+
223+
def init_servo(self, port="SERVO"):
224+
return Servo(port, gpg=self, use_mutex=self.use_mutex)
225+
226+
def init_distance_sensor(self, port="I2C"):
227+
try:
228+
from di_sensors import easy_distance_sensor
229+
return DistanceSensor(port, gpg=self, use_mutex=self.use_mutex)
230+
except:
231+
print("DI Sensor library not found")
232+
return None
233+
234+
def init_dht_sensor(self, port="SERIAL", sensor_type = 0):
235+
return DHTSensor(port, self, sensor_type, use_mutex=self.use_mutex)
236+
237+
def init_remote(self, port="SERIAL"):
238+
return Remote(port="SERIAL", gpg=self, use_mutex=self.use_mutex)
239+
240+
def init_motion_sensor(self, port="D11"):
241+
return MotionSensor(port, gpg=self, use_mutex=self.use_mutex)
242+
197243

198244

199245
#############################################################
@@ -273,6 +319,12 @@ def isDigital(self):
273319

274320
def set_descriptor(self, descriptor):
275321
self.descriptor = descriptor
322+
323+
def reconfig_bus(self):
324+
'''
325+
Does nothing. Placeholder for compatibility with GoPiGo3
326+
'''
327+
pass
276328
##########################
277329

278330

@@ -596,6 +648,12 @@ def get_remote_code(self):
596648
key = ir_receiver.nextcode(consume=False)
597649
else:
598650
key = ""
651+
try:
652+
# in python3, key will come in as a byte
653+
key = key.decode("utf-8")
654+
except AttributeError:
655+
# in python2, key will be a string, so no need to do anything
656+
pass
599657

600658
return key
601659

Software/Python/ir_remote_control/README.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@ Connect the `IR receiver` to the Serial port on the GoPiGo. This will act as a p
2626
## Setup the Software
2727

2828
In your `Raspberry Pi`, open up a terminal and enter the following commands:
29-
1. `sudo bash lirc/install.sh`.
30-
2. `sudo bash lirc/setup_older_version.sh` - run this script too only if your version of IR receiver `<= v1.0`.
31-
3. `sudo bash server/install.sh`.
29+
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.
30+
2. `sudo bash lirc/install.sh`.
31+
3. `sudo bash lirc/setup_older_version.sh` - run this script too only if your version of IR receiver `<= v1.0`.
32+
4. `sudo bash server/install.sh`.
3233

3334
## Enabling / Disabling Service
3435

Software/Python/ir_remote_control/examples/ir_recv_example.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
while True:
4242
#Wait for the next IR code to arrive. The codes are queued in a buffer before printing
4343
a= ir_receiver.nextcode()
44+
if not isinstance(a, str):
45+
a = a.decode('utf-8')
4446
if len(a) !=0:
45-
print a
47+
print (a)
4648
time.sleep(.1)

Software/Python/ir_remote_control/examples/lirc_example.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,4 @@
4242
#Wait for the next IR code to arrive. The codes are queued in a buffer before printing
4343
a= lirc.nextcode()
4444
if len(a) !=0:
45-
print a[0]
45+
print (a[0])

Software/Python/ir_remote_control/server/di_ir_reader.py

+47-27
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import os
99

1010
call("sudo /etc/init.d/lirc stop", shell=True)
11-
debug = 1
11+
debug = True
1212

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

323323
# print 'sending "%s"' % str(i)
324324
if found_flag:
325-
print keys[key]
326-
sock.sendall(keys[key])
325+
print (keys[key])
326+
try:
327+
sock.sendall(keys[key]) # works with Python 2
328+
except:
329+
sock.sendall(keys[key].encode()) # works with Python 3
327330
global saved_previous_key
328331
saved_previous_key = keys[key]
329332
else:
330-
print "NO_MATCH"
331-
sock.sendall("NO_MATCH")
333+
print ("NO_MATCH")
334+
try:
335+
sock.sendall("NO_MATCH")
336+
except TypeError:
337+
sock.sendall("NO_MATCH".encode())
332338
except socket.error:
333-
print "Unable to connect to the server"
339+
print ("Unable to connect to the server")
340+
except:
341+
print("something happened")
334342
finally:
335343
# print 'Closing socket'
336344
sock.close()
@@ -339,7 +347,7 @@ def match_with_button(inp):
339347
list_to_parse=[]
340348
large_val_found=0
341349
if debug:
342-
print inp,len(inp)
350+
print (inp,len(inp))
343351

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

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

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

421+
# detect if pulse_us_string is a byte array - which implies python3
422+
# transfer to str type if needed
423+
if not isinstance(pulse_us_string, str):
424+
pulse_us_string = pulse_us_string.decode('utf-8')
425+
# print(type(pulse_us_string))
426+
411427
# check if we got a positive integer number
412428
if str.isdigit(pulse_us_string):
413429
pulse_us= int(pulse_us_string) # signal length
@@ -424,7 +440,7 @@ def main(process_ir):
424440
# If noise was there in current pulse, just skip it
425441
if pulse_us < noise_thresh:
426442
if debug:
427-
print "noise:",pulse_us
443+
print ("noise:", pulse_us)
428444
return
429445

430446
# There are 3 checks to detect the keypresses
@@ -442,7 +458,7 @@ def main(process_ir):
442458
header_detected_flag=0
443459
before_header_flag=0
444460
if debug:
445-
print "de:",pulse_us
461+
print ("de:", pulse_us)
446462
header_1_detected_flag=0
447463
#*********************************************
448464
match_with_button(detected_sig_buf)
@@ -452,21 +468,21 @@ def main(process_ir):
452468
add_next_flag = 0
453469
# remove last sig from buffer
454470
if debug:
455-
print "adding last",pulse_us,
471+
print ("adding last", pulse_us)
456472
try:
457473
detected_sig_buf.pop()
458474
except IndexError:
459475
return
460476
pulse_us+=last_pulse_us
461477
if debug:
462-
print pulse_us
478+
print (pulse_us)
463479

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

467483
if debug:
468484
if add_next_flag ==0:
469-
print "d:",pulse_us
485+
print ("d:", pulse_us)
470486

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

478494
server_address = ('localhost', 21852)
479-
# print 'connecting to %s port %s' % server_address
495+
# print ('connecting to %s port %s' % server_address)
480496
sock.connect(server_address)
481497

482-
# print 'sending "%s"' % str(i)
483-
sock.sendall(saved_previous_key)
498+
# on python2, sendall requires a str
499+
# on python3, if sendall receives a str it will fail, so send bytes instead
500+
try:
501+
sock.sendall(saved_previous_key)
502+
except TypeError:
503+
sock.sendall(saved_previous_key.encode())
484504

485505
except socket.error:
486-
print "Unable to connect to the server"
506+
print ("Unable to connect to the server")
487507
finally:
488508
# print 'Closing socket'
489509
sock.close()
490510
if debug:
491-
print "n:",pulse_us
511+
print ("n:", pulse_us)
492512

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

498518
if add_next_flag:
499519
if debug:
500-
print "adding 4k pulse"
501-
print pulse_us,last_pulse_us
520+
print ("adding 4k pulse")
521+
print (pulse_us, last_pulse_us)
502522
pulse_us+=last_pulse_us
503523

504524
add_next_flag=0
505525

506526
if pulse_us > header_1_thresh-header_1_margin and pulse_us < header_1_thresh+header_1_margin:
507527
# IR signal detected
508528
if debug:
509-
print "header_1_detected_flag=1"
529+
print ("header_1_detected_flag=1")
510530
header_1_detected_flag=1
511531
else:
512532
if last_sig_type == sig_type:
513533
if debug:
514-
print "setting 4k pulse flag"
534+
print ("setting 4k pulse flag")
515535
add_next_flag=1
516536
last_pulse_us=pulse_us
517537
return
@@ -522,19 +542,19 @@ def main(process_ir):
522542
#Second check for 9k pulse
523543
if before_header_flag==1 and header_detected_flag==0:
524544
if debug:
525-
print "checking before_header_flag==1",pulse_us,header_thresh-header_margin,header_thresh+header_margin
545+
print ("checking before_header_flag==1",pulse_us,header_thresh-header_margin,header_thresh+header_margin)
526546

527547
if add_next_flag:
528548
pulse_us+=last_pulse_us
529549

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

534554
if pulse_us > header_thresh-header_margin and pulse_us < header_thresh+header_margin:
535555
header_detected_flag=1
536556
if debug:
537-
print "header_detected_flag=1"
557+
print ("header_detected_flag=1")
538558
else:
539559
if last_sig_type == sig_type:
540560
add_next_flag=1
@@ -545,7 +565,7 @@ def main(process_ir):
545565
if before_header_flag==0 and pulse_us>sig_thresh_len_before_header:
546566
before_header_flag=1
547567
if debug:
548-
print "before_header_flag=1",pulse_us
568+
print ("before_header_flag=1",pulse_us)
549569

550570
last_pulse_us=pulse_us
551571
last_sig_type= sig_type
@@ -556,7 +576,7 @@ def main(process_ir):
556576

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

559-
print "Press any key on the remote to start"
579+
print ("Press any key on the remote to start")
560580

561581
try:
562582
with GracefullExiter() as exiter:

Software/Python/ir_remote_control/server/install.sh

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ GOPIGO_PATH=$DEXTER_PATH/GoPiGo
1212

1313
#Install the ir_receiver library systemwide
1414
pushd $GOPIGO_PATH/Software/Python/ir_remote_control/server/
15-
sudo python setup.py install
16-
sudo rm -r build
17-
sudo rm -r dist
18-
sudo rm -r ir_receiver.egg-info
15+
python setup.py install
16+
rm -r build
17+
rm -r dist
18+
rm -r ir_receiver.egg-info
1919

2020
sudo cp ir-server.service /etc/systemd/system/ir-server.service
2121
sudo systemctl daemon-reload

0 commit comments

Comments
 (0)