Skip to content

Commit 0517bd1

Browse files
authored
Merge pull request #233 from DexterInd/develop
bring develop to master branch
2 parents 63a2fd5 + 47052db commit 0517bd1

File tree

2 files changed

+107
-2
lines changed

2 files changed

+107
-2
lines changed

Software/Python/easygopigo3.py

+97-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99
import time
1010
import os
1111
import math
12-
from I2C_mutex import Mutex
12+
import json
1313
import easysensors
14+
from I2C_mutex import Mutex
15+
from math import pi
1416

1517
try:
1618
from di_sensors import easy_distance_sensor
@@ -92,10 +94,11 @@ class EasyGoPiGo3(gopigo3.GoPiGo3):
9294
9395
"""
9496

95-
def __init__(self, use_mutex=False):
97+
def __init__(self, config_file_path="/home/pi/Dexter/gpg3_config.json", use_mutex=False):
9698
"""
9799
This constructor sets the variables to the following values:
98100
101+
:param str config_file_path = "/home/pi/Dexter/gpg3_config.json": Path to JSON config file that stores the wheel diameter and wheel base width for the GoPiGo3.
99102
:param bool use_mutex = False: When using multiple threads/processes that access the same resource/device, mutex has to be enabled.
100103
:var int speed = 300: The speed of the motors should go between **0-1000** DPS.
101104
:var tuple(int,int,int) left_eye_color = (0,255,255): Set Dex's left eye color to **turqoise**.
@@ -105,6 +108,13 @@ def __init__(self, use_mutex=False):
105108
:raises gopigo3.FirmwareVersionError: If the GoPiGo3 firmware needs to be updated. It also debugs a message in the terminal.
106109
:raises Exception: For any other kind of exceptions.
107110
111+
The ``config_file_path`` parameter represents the path to a JSON file. The presence of this configuration file is optional and is only required in cases where
112+
the GoPiGo3 has a skewed trajectory due to minor differences in these two constants: the **wheel diameter** and the **wheel base width**. In most cases, this won't be the case.
113+
114+
By-default, the constructor tries to read the ``config_file_path`` file and silently fails if something goes wrong: wrong permissions, non-existent file, improper key values and so on.
115+
To set custom values to these 2 constants, use :py:meth:`~easygopigo3.EasyGoPiGo3.set_robot_constants` method and for saving the constants to a file call
116+
:py:meth:`~easygopigo3.EasyGoPiGo3.save_robot_constants` method.
117+
108118
"""
109119
try:
110120
super(self.__class__, self).__init__()
@@ -117,6 +127,13 @@ def __init__(self, use_mutex=False):
117127
except Exception as e:
118128
raise e
119129

130+
# load wheel diameter & wheel base width
131+
# should there be a problem doing that then save the current default configuration
132+
try:
133+
self.load_robot_constants()
134+
except Exception:
135+
pass
136+
120137
self.sensor_1 = None
121138
self.sensor_2 = None
122139
self.DEFAULT_SPEED = 300
@@ -126,6 +143,84 @@ def __init__(self, use_mutex=False):
126143
self.right_eye_color = (0, 255, 255)
127144
self.use_mutex = use_mutex
128145

146+
def load_robot_constants(self, config_file_path="/home/pi/Dexter/gpg3_config.json"):
147+
"""
148+
Load wheel diameter and wheel base width constants for the GoPiGo3 from file.
149+
150+
This method gets called by the constructor.
151+
152+
:param str config_file_path = "/home/pi/Dexter/gpg3_config.json": Path to JSON config file that stores the wheel diameter and wheel base width for the GoPiGo3.
153+
:raises FileNotFoundError: When the file is non-existent.
154+
:raises KeyError: If one of the keys is not part of the dictionary.
155+
:raises ValueError: If the saved values are not positive numbers (floats or ints).
156+
:raises TypeError: If the saved values are not numbers.
157+
:raises IOError: When the file cannot be accessed.
158+
:raises PermissionError: When there are not enough permissions to access the file.
159+
160+
Here's how the JSON config file must look like before reading it. Obviously, the supported format is JSON so that anyone can come in
161+
and edit their own config file if they don't want to go through saving the values by using the API.
162+
163+
.. code-block:: json
164+
165+
{
166+
"wheel-diameter": 66.5,
167+
"wheel-base-width": 117
168+
}
169+
170+
"""
171+
with open(config_file_path, 'r') as json_file:
172+
data = json.load(json_file)
173+
if data['wheel-diameter'] > 0 and data['wheel-base-width'] > 0:
174+
self.set_robot_constants(data['wheel-diameter'], data['wheel-base-width'])
175+
else:
176+
raise ValueError('positive values required')
177+
178+
def save_robot_constants(self, config_file_path="/home/pi/Dexter/gpg3_config.json"):
179+
"""
180+
Save the current wheel diameter and wheel base width constants (from within this object's context) for the GoPiGo3 to file for future use.
181+
182+
:param str config_file_path = "/home/pi/Dexter/gpg3_config.json": Path to JSON config file that stores the wheel diameter and wheel base width for the GoPiGo3.
183+
:raises IOError: When the file cannot be accessed.
184+
:raises PermissionError: When there are not enough permissions to create the file.
185+
186+
Here's how the JSON config file will end up looking like. The values can differ from case to case.
187+
188+
.. code-block:: json
189+
190+
{
191+
"wheel-diameter": 66.5,
192+
"wheel-base-width": 117
193+
}
194+
195+
"""
196+
with open(config_file_path, 'w') as json_file:
197+
data = {
198+
"wheel-diameter": self.WHEEL_DIAMETER,
199+
"wheel-base-width": self.WHEEL_BASE_WIDTH
200+
}
201+
json.dump(data, json_file)
202+
203+
def set_robot_constants(self, wheel_diameter, wheel_base_width):
204+
"""
205+
Set new wheel diameter and wheel base width values for the GoPiGo3.
206+
207+
:param float wheel_diameter: Diameter of the GoPiGo3 wheels as measured in millimeters.
208+
:param float wheel_base_width: The distance between the 2 centers of the 2 wheels as measured in millimeters.
209+
210+
This should only be required in rare cases when the GoPiGo3's trajectory is skewed due to minor differences in the wheel-to-body measurements.
211+
212+
The GoPiGo3 class instantiates itself with default values for both constants:
213+
214+
1. ``wheel_diameter`` is by-default set to **66.5** *mm*.
215+
216+
2. ``wheel_base_width`` is by-default set to **117** *mm*.
217+
218+
"""
219+
self.WHEEL_DIAMETER = wheel_diameter
220+
self.WHEEL_CIRCUMFERENCE = self.WHEEL_DIAMETER * pi
221+
self.WHEEL_BASE_WIDTH = wheel_base_width
222+
self.WHEEL_BASE_CIRCUMFERENCE = self.WHEEL_BASE_WIDTH * pi
223+
129224
def volt(self):
130225
"""
131226
This method returns the battery voltage of the `GoPiGo3`_.

docs/source/api-basic/structure.rst

+10
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,16 @@ GoPiGo3 LEDs
149149
easygopigo3.EasyGoPiGo3.close_right_eye
150150
easygopigo3.EasyGoPiGo3.close_eyes
151151

152+
153+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
154+
GoPiGo3 Constants
155+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
156+
157+
.. autosummary::
158+
easygopigo3.EasyGoPiGo3.load_robot_constants
159+
easygopigo3.EasyGoPiGo3.save_robot_constants
160+
easygopigo3.EasyGoPiGo3.set_robot_constants
161+
152162
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
153163
GoPiGo3 Init Methods
154164
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

0 commit comments

Comments
 (0)