Skip to content

Commit 4cd38eb

Browse files
Cleaning the code
1 parent 86fc230 commit 4cd38eb

File tree

2 files changed

+54
-42
lines changed

2 files changed

+54
-42
lines changed

hardware/plc.py

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,27 @@
1-
"""Module for Modbus Communication with PLC"""
1+
"""Module for Modbus Communication with PLC."""
22

33
from pymodbus.client.sync import ModbusTcpClient
44
from pyModbusTCP.server import DataBank, DeviceIdentification
55

6+
67
def prepare_server():
7-
"""Prepare server properties"""
8+
"""Prepare server properties."""
89
identity = DeviceIdentification(
910
vendor_name=b"Lab. Schneider DTETI UGM",
1011
product_code=b"MCD",
1112
major_minor_revision=b"1.0",
1213
vendor_url=b"https://github.com/javanese-programmer/conveyor-object-detection",
1314
product_name=b"McDetector",
1415
model_name=b"McDetector-1",
15-
user_application_name=b"Conveyor Object Detection"
16+
user_application_name=b"Conveyor Object Detection",
1617
)
1718
databank = DataBank()
1819
return databank, identity
1920

21+
2022
def server_set_di(databank: DataBank, bit_value: str):
21-
"""Set a value to Discretes Input of a server
23+
"""Set values to Discrete Inputs of a server.
24+
2225
Args:
2326
databank: a DataBank object that store data
2427
bit_value: binary value in string format
@@ -28,10 +31,11 @@ def server_set_di(databank: DataBank, bit_value: str):
2831
for addr, bit in enumerate(bit_value):
2932
databank.set_discrete_inputs(addr, [int(bit)])
3033
print("Discretes Inputs updated!")
31-
34+
3235

3336
def server_set_ir(databank: DataBank, values: tuple):
34-
"""Set a value to Input Registers of a server
37+
"""Set values to Input Registers of a server.
38+
3539
Args:
3640
databank: a DataBank object that store data
3741
values: tuple of values to be written to register
@@ -43,49 +47,52 @@ def server_set_ir(databank: DataBank, values: tuple):
4347
print("Input registers updated!")
4448

4549

46-
class PLC():
47-
"""PLC Client Class"""
48-
50+
class PLC:
51+
"""PLC Client Class."""
52+
4953
def __init__(self, ip_address: str):
50-
"""Constructor method
54+
"""Init object instance of the class.
55+
5156
Args:
5257
ip_address: IP address of PLC to be connected
5358
"""
5459
# Define attribute for PLC IP Address
5560
self.plc_ip = ip_address
56-
61+
5762
def write_words(self, values: tuple):
58-
"""Write PLC Registers through Modbus TCP Communication
63+
"""Write PLC Registers through Modbus TCP Communication.
64+
5965
Args:
6066
values: tuple of values to be written to register
6167
"""
6268
# Define Modbus TCP Client and connect to it
6369
client = ModbusTcpClient(self.plc_ip)
6470
client.connect()
65-
71+
6672
# Write value to registers: %MW0, %MW1, and so on
6773
print("Writing value to PLC registers...")
6874
for reg, val in enumerate(values):
6975
client.write_registers(reg, int(val))
7076
print("Writing operation done!")
71-
77+
7278
# Close connection
7379
client.close()
74-
80+
7581
def write_bits(self, bit_value: str):
76-
"""Write PLC Coils through Modbus TCP Communication
82+
"""Write PLC Coils through Modbus TCP Communication.
83+
7784
Args:
7885
bit_value: binary value in string format
7986
"""
8087
# Define Modbus TCP Client and connect to it
8188
client = ModbusTcpClient(self.plc_ip)
8289
client.connect()
83-
90+
8491
# Write value to coils: %M0, %M1, and so on
8592
print("Writing value to PLC coils...")
8693
for coil, bit in enumerate(bit_value):
8794
client.write_coils(coil, [int(bit)])
8895
print("Writing operation done!")
89-
96+
9097
# Close connection
91-
client.close()
98+
client.close()

hardware/sensor.py

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,48 @@
1-
""" Script to define GPIO, sensor, and actuator """
1+
"""Script to initiate GPIO, sensor, and actuator."""
22

33
from RPi import GPIO
4-
from time import sleep
4+
55

66
def prepare_gpio():
7-
"""Function to prepare GPIO"""
7+
"""Prepare GPIO before being used."""
88
GPIO.setmode(GPIO.BOARD)
99
GPIO.setwarnings(False)
10-
10+
11+
1112
def clean_gpio():
12-
"""Function to clean GPIO port"""
13+
"""Clean GPIO port after being used."""
1314
GPIO.cleanup()
1415

15-
class Infrared():
16-
""" IR Sensor Class """
17-
16+
17+
class Infrared:
18+
"""IR Sensor Class."""
19+
1820
def __init__(self, right_ir: int, left_ir: int):
19-
""" Contructor method
21+
"""Init object instance of the class.
22+
2023
Args:
2124
right_ir (int): Right IR Sensor Pin
2225
left_ir (int): Left IR Sensor pin
2326
"""
2427
self.right_ir = right_ir
2528
self.left_ir = left_ir
26-
29+
2730
GPIO.setup(self.right_ir, GPIO.IN)
2831
GPIO.setup(self.left_ir, GPIO.IN)
29-
32+
3033
def read_sensor(self):
31-
"""Method to detect object presence using IR"""
34+
"""Detect object's presence using IR."""
3235
right_read = GPIO.input(self.right_ir)
3336
left_read = GPIO.input(self.left_ir)
3437
return right_read, left_read
3538

3639

37-
class Led():
38-
""" LED Class """
39-
40+
class Led:
41+
"""LED Class."""
42+
4043
def __init__(self, yellow, red, blue):
41-
""" Contructor method
44+
"""Init object instance of the class.
45+
4246
Args:
4347
yellow (int): Yellow LED Pin
4448
red (int): Red LED pin
@@ -47,15 +51,16 @@ def __init__(self, yellow, red, blue):
4751
self.yellow = yellow
4852
self.red = red
4953
self.blue = blue
50-
54+
5155
GPIO.setup(self.yellow, GPIO.OUT)
5256
GPIO.setup(self.red, GPIO.OUT)
5357
GPIO.setup(self.blue, GPIO.OUT)
54-
58+
5559
def turn_on(self, index: int):
56-
"""Method to turn-on an LED according to detected object
60+
"""Turn-on an LED according to detected object.
61+
5762
Args:
58-
index: Index of detected object class
63+
index: Index of detected object class
5964
"""
6065
if index == 0:
6166
GPIO.output(self.yellow, True)
@@ -73,9 +78,9 @@ def turn_on(self, index: int):
7378
GPIO.output(self.yellow, False)
7479
GPIO.output(self.red, False)
7580
GPIO.output(self.blue, False)
76-
81+
7782
def turn_off(self):
78-
"""Method to turn-off all LED"""
83+
"""Turn-off all LED."""
7984
GPIO.output(self.yellow, False)
8085
GPIO.output(self.red, False)
81-
GPIO.output(self.blue, False)
86+
GPIO.output(self.blue, False)

0 commit comments

Comments
 (0)