Saltar a contenido

Common Module

measure

Measure

Class that represents a single measurement from the RPLidar.

Source code in devices\raspberry_pi_5\src\common\measure.py
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
class Measure:
    """
    Class that represents a single measurement from the RPLidar.
    """

    # Measure attributes separator
    ATTRIBUTES_SEPARATOR = ","

    # Measures separator
    MEASURES_SEPARATOR = ";"

    def __init__(self, angle: float, distance: float, quality: int):
        """
        Initialize the Measure instance.
        """
        self.angle = angle
        self.distance = distance
        self.quality = quality

    def __str__(self):
        """
        String representation of the Measure object.
        """
        # return self.ATTRIBUTES_SEPARATOR.join([str(self.angle), str(self.distance), str(self.quality)])
        return self.ATTRIBUTES_SEPARATOR.join(
            [str(self.angle), str(self.distance)]
        )

    def __repr__(self):
        """
        String representation of the Measure object for debugging.
        """
        return f"Measure(angle={self.angle}, distance={self.distance}, quality={self.quality})"

    @property
    def angle(self) -> float:
        """
        Get the angle of the measure.

        Returns:
            float: Angle of the measure.
        """
        return self.__angle

    @angle.setter
    def angle(self, value: float) -> None:
        """
        Set the angle of the measure.

        Args:
            value (float): Angle to set.
        """
        is_instance(value, float)

        if not (0 <= value):
            raise ValueError(
                f"Angle must be a non-negative float, received: {value}"
            )
        self.__angle = 0.0 if value >= 360.0 else value

    @property
    def distance(self) -> float:
        """
        Get the distance of the measure.

        Returns:
            float: Distance of the measure.
        """
        return self.__distance

    @distance.setter
    def distance(self, value: float) -> None:
        """
        Set the distance of the measure.

        Args:
            value (float): Distance to set.
        """
        is_instance(value, float)

        if value < 0:
            raise ValueError(
                "Distance must be a non-negative float, received: {}".format(
                    value
                )
            )

        self.__distance = value

    @property
    def quality(self) -> int:
        """
        Get the quality of the measure.

        Returns:
            int: Quality of the measure.
        """
        return self.__quality

    @quality.setter
    def quality(self, value: int) -> None:
        """
        Set the quality of the measure.

        Args:
            value (int): Quality to set.
        """
        is_instance(value, int)

        if value < 0:
            raise ValueError(
                f"Quality must be a non-negative integer, received: {value}"
            )
        self.__quality = value

    @classmethod
    def from_string(cls, measure_str: str) -> Optional['Measure']:
        """
        Create a Measure object from a string representation.

        Args:
            measure_str (str): String representation of the measure.

        Returns:
            Measure: Measure object created from the string.
        """
        try:
            # Check the type of measure_str
            is_instance(measure_str, str)

            # Split the string by the attributes separator
            parts = measure_str.split(cls.ATTRIBUTES_SEPARATOR)

            if len(parts) < 2:
                raise ValueError(f"Invalid measure string: {measure_str}")

            # Convert parts to appropriate types
            angle = float(parts[0])
            distance = float(parts[1])

            # Quality is optional, default to 0 if not provided
            quality = int(parts[2]) if len(parts) > 2 else 0

        except ValueError as e:
            return None

        return cls(angle, distance, quality)

    @classmethod
    def measures_to_string(cls, measures: List) -> str:
        """
        Convert a list of Measure objects to a string representation.

        Args:
            measures (List): List of Measure objects.

        Returns:
            str: String representation of the measures.
        """
        # Check the type of measures
        is_instance(measures, List)

        # Convert each measure to string and join them with spaces
        return cls.MEASURES_SEPARATOR.join(str(measure) for measure in measures)

    @classmethod
    def from_string_to_measures(cls, measures_str: str) -> List:
        """
        Convert a string representation of measures back to a list of Measure objects.

        Args:
            measures_str (str): String representation of measures.

        Returns:
            List: List of Measure objects.
        """
        # Check the type of measures_str
        is_instance(measures_str, str)

        # Split the string by the measures separator and convert each part to Measure
        return [cls.from_string(measure_str) for measure_str in
                measures_str.split(cls.MEASURES_SEPARATOR) if measure_str]

    @classmethod
    def get_properties_from_string(cls, measure_str: str) -> tuple:
        """
        Get the properties of a Measure object from its string representation.

        Args:
            measure_str (str): String representation of the measure.

        Returns:
            tuple: A tuple containing the angle, distance, and quality of the measure.
        """
        # Check the type of measure_str
        is_instance(measure_str, str)

        # Split the string by the attributes separator
        parts = measure_str.split(cls.ATTRIBUTES_SEPARATOR)

        if len(parts) < 2:
            raise ValueError("Invalid measure string: {}".format(measure_str))

        return (
            float(parts[0]),
            float(parts[1]),
            int(parts[2]) if len(parts) > 2 else 0
            # Quality, default to 0 if not provided
        )

angle property writable

Get the angle of the measure.

Returns:

Name Type Description
float float

Angle of the measure.

distance property writable

Get the distance of the measure.

Returns:

Name Type Description
float float

Distance of the measure.

quality property writable

Get the quality of the measure.

Returns:

Name Type Description
int int

Quality of the measure.

__init__(angle, distance, quality)

Initialize the Measure instance.

Source code in devices\raspberry_pi_5\src\common\measure.py
17
18
19
20
21
22
23
def __init__(self, angle: float, distance: float, quality: int):
    """
    Initialize the Measure instance.
    """
    self.angle = angle
    self.distance = distance
    self.quality = quality

__repr__()

String representation of the Measure object for debugging.

Source code in devices\raspberry_pi_5\src\common\measure.py
34
35
36
37
38
def __repr__(self):
    """
    String representation of the Measure object for debugging.
    """
    return f"Measure(angle={self.angle}, distance={self.distance}, quality={self.quality})"

__str__()

String representation of the Measure object.

Source code in devices\raspberry_pi_5\src\common\measure.py
25
26
27
28
29
30
31
32
def __str__(self):
    """
    String representation of the Measure object.
    """
    # return self.ATTRIBUTES_SEPARATOR.join([str(self.angle), str(self.distance), str(self.quality)])
    return self.ATTRIBUTES_SEPARATOR.join(
        [str(self.angle), str(self.distance)]
    )

from_string(measure_str) classmethod

Create a Measure object from a string representation.

Parameters:

Name Type Description Default
measure_str str

String representation of the measure.

required

Returns:

Name Type Description
Measure Optional[Measure]

Measure object created from the string.

Source code in devices\raspberry_pi_5\src\common\measure.py
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
@classmethod
def from_string(cls, measure_str: str) -> Optional['Measure']:
    """
    Create a Measure object from a string representation.

    Args:
        measure_str (str): String representation of the measure.

    Returns:
        Measure: Measure object created from the string.
    """
    try:
        # Check the type of measure_str
        is_instance(measure_str, str)

        # Split the string by the attributes separator
        parts = measure_str.split(cls.ATTRIBUTES_SEPARATOR)

        if len(parts) < 2:
            raise ValueError(f"Invalid measure string: {measure_str}")

        # Convert parts to appropriate types
        angle = float(parts[0])
        distance = float(parts[1])

        # Quality is optional, default to 0 if not provided
        quality = int(parts[2]) if len(parts) > 2 else 0

    except ValueError as e:
        return None

    return cls(angle, distance, quality)

from_string_to_measures(measures_str) classmethod

Convert a string representation of measures back to a list of Measure objects.

Parameters:

Name Type Description Default
measures_str str

String representation of measures.

required

Returns:

Name Type Description
List List

List of Measure objects.

Source code in devices\raspberry_pi_5\src\common\measure.py
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
@classmethod
def from_string_to_measures(cls, measures_str: str) -> List:
    """
    Convert a string representation of measures back to a list of Measure objects.

    Args:
        measures_str (str): String representation of measures.

    Returns:
        List: List of Measure objects.
    """
    # Check the type of measures_str
    is_instance(measures_str, str)

    # Split the string by the measures separator and convert each part to Measure
    return [cls.from_string(measure_str) for measure_str in
            measures_str.split(cls.MEASURES_SEPARATOR) if measure_str]

get_properties_from_string(measure_str) classmethod

Get the properties of a Measure object from its string representation.

Parameters:

Name Type Description Default
measure_str str

String representation of the measure.

required

Returns:

Name Type Description
tuple tuple

A tuple containing the angle, distance, and quality of the measure.

Source code in devices\raspberry_pi_5\src\common\measure.py
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
@classmethod
def get_properties_from_string(cls, measure_str: str) -> tuple:
    """
    Get the properties of a Measure object from its string representation.

    Args:
        measure_str (str): String representation of the measure.

    Returns:
        tuple: A tuple containing the angle, distance, and quality of the measure.
    """
    # Check the type of measure_str
    is_instance(measure_str, str)

    # Split the string by the attributes separator
    parts = measure_str.split(cls.ATTRIBUTES_SEPARATOR)

    if len(parts) < 2:
        raise ValueError("Invalid measure string: {}".format(measure_str))

    return (
        float(parts[0]),
        float(parts[1]),
        int(parts[2]) if len(parts) > 2 else 0
        # Quality, default to 0 if not provided
    )

measures_to_string(measures) classmethod

Convert a list of Measure objects to a string representation.

Parameters:

Name Type Description Default
measures List

List of Measure objects.

required

Returns:

Name Type Description
str str

String representation of the measures.

Source code in devices\raspberry_pi_5\src\common\measure.py
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
@classmethod
def measures_to_string(cls, measures: List) -> str:
    """
    Convert a list of Measure objects to a string representation.

    Args:
        measures (List): List of Measure objects.

    Returns:
        str: String representation of the measures.
    """
    # Check the type of measures
    is_instance(measures, List)

    # Convert each measure to string and join them with spaces
    return cls.MEASURES_SEPARATOR.join(str(measure) for measure in measures)