Skip to content

Commit

Permalink
Merge pull request #37 from clearpathrobotics/lcamero/battery
Browse files Browse the repository at this point in the history
Battery
  • Loading branch information
luis-camero authored Oct 25, 2023
2 parents f785062 + d5e94d3 commit 2af12f7
Show file tree
Hide file tree
Showing 10 changed files with 233 additions and 2 deletions.
176 changes: 176 additions & 0 deletions clearpath_config/platform/battery.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
# Software License Agreement (BSD)
#
# @author Luis Camero <lcamero@clearpathrobotics.com>
# @copyright (c) 2023, Clearpath Robotics, Inc., All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# * Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# * Neither the name of Clearpath Robotics nor the names of its contributors
# may be used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
from clearpath_config.common.types.config import BaseConfig
from clearpath_config.common.types.platform import Platform
from clearpath_config.common.utils.dictionary import flip_dict


class BatteryConfig(BaseConfig):
BATTERY = "battery"

# Models
MODEL = "model"
UNKNOWN = "unknown"
# Husky Lead Acid
ES20_12C = "ES20_12C"
# Husky/Jackal Li_ION
HE2613 = "HE2613"
# Warthog Lead Acid
U1_35 = "U1_35"
# Warthog LiFEPO4
NEC_ALM12V35 = "NEC_ALM12V35"
VALENCE_U24_12XP = "VALENCE_U24_12XP"
VALENCE_U27_12XP = "VALENCE_U27_12XP"

# Configurations
CONFIGURATION = "configuration"
S2P1 = "S2P1"
S1P3 = "S1P3"
S1P4 = "S1P4"
S1P1 = "S1P1"
S4P3 = "S4P3"
S4P1 = "S4P1"

VALID = {
Platform.GENERIC: {
UNKNOWN: [UNKNOWN]
},
Platform.A200: {
ES20_12C: [S2P1],
HE2613: [S1P3, S1P4],
},
Platform.J100: {
HE2613: [S1P1],
},
Platform.W200: {
U1_35: [S4P3],
NEC_ALM12V35: [S4P3],
VALENCE_U24_12XP: [S4P1],
VALENCE_U27_12XP: [S4P1],
},
}

TEMPLATE = {
BATTERY: {
MODEL: MODEL,
CONFIGURATION: CONFIGURATION
}
}

KEYS = flip_dict(TEMPLATE)

DEFAULTS = {
MODEL: UNKNOWN,
CONFIGURATION: UNKNOWN
}

def __init__(
self,
config: dict = {},
model: str = DEFAULTS[MODEL],
configuration: str = DEFAULTS[CONFIGURATION],
) -> None:
# Initialization
self._config = {}
if model == self.DEFAULTS[self.MODEL] or model == self.UNKNOWN:
self.update_defaults()
self.model = self.DEFAULTS[self.MODEL]
else:
self.model = model
if configuration == self.DEFAULTS[self.CONFIGURATION] or model == self.UNKNOWN:
self.update_defaults()
self.configuration = self.DEFAULTS[self.CONFIGURATION]
else:
self.configuration = configuration
# Setter Template
setters = {
self.KEYS[self.MODEL]: BatteryConfig.model,
self.KEYS[self.CONFIGURATION]: BatteryConfig.configuration,
}
super().__init__(setters, config, self.BATTERY)

def update_defaults(self) -> None:
platform = BaseConfig.get_platform_model()
self.DEFAULTS[self.MODEL] = list(self.VALID[platform])[0]
self.DEFAULTS[self.CONFIGURATION] = list(
self.VALID[platform][self.DEFAULTS[self.MODEL]])[0]

def update(self, serial_number: bool = False) -> None:
if serial_number:
self.update_defaults()
self.model = self.DEFAULTS[self.MODEL]
self.configuration = self.DEFAULTS[self.CONFIGURATION]

@property
def model(self) -> str:
self.set_config_param(
key=self.KEYS[self.MODEL],
value=self._model
)
return self._model

@model.setter
def model(self, value: str) -> None:
platform = BaseConfig.get_platform_model()
assert platform in self.VALID, ((
"Platform %s is invalid. " % platform +
"Platform must be one of: %s" % list(self.VALID)
))
assert value in self.VALID[platform], ((
"Battery model %s is invalid. " % value +
"Battery model for platform '%s' must be one of: %s" % (
platform, list(self.VALID[platform]))
))
self._model = value

@property
def configuration(self) -> str:
self.set_config_param(
key=self.KEYS[self.CONFIGURATION],
value=self._configuration
)
return self._configuration

@configuration.setter
def configuration(self, value: str) -> None:
platform = BaseConfig.get_platform_model()
assert platform in self.VALID, ((
"Platform %s is invalid. " % platform +
"Platform must be one of: %s" % list(self.VALID)
))
assert self.model in self.VALID[platform], ((
"Battery model %s in invalid. " % self.model +
"Battery model for platform '%s' must be one of: %s" % (
platform, list(self.VALID[platform]))
))
assert value in self.VALID[platform][self.model], ((
"Battery configuration %s invalid. " % value +
"For platform '%s' and battery model '%s', it must be one of: %s" % (
platform, self.model, list(self.VALID[platform][self.model]))
))
self._configuration = value
35 changes: 33 additions & 2 deletions clearpath_config/platform/platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
# POSSIBILITY OF SUCH DAMAGE.
from clearpath_config.common.types.config import BaseConfig
from clearpath_config.common.utils.dictionary import flip_dict
from clearpath_config.platform.battery import BatteryConfig
from clearpath_config.platform.extras import ExtrasConfig
from clearpath_config.platform.attachments.config import BaseAttachmentsConfig
from clearpath_config.platform.attachments.mux import AttachmentsConfigMux
Expand All @@ -38,15 +39,19 @@ class PlatformConfig(BaseConfig):
# Controllers
PS4 = "ps4"
LOGITECH = "logitech"

CONTROLLER = "controller"
ATTACHMENTS = "attachments"
# Extras
EXTRAS = "extras"
# Battery
BATTERY = "battery"

TEMPLATE = {
PLATFORM: {
CONTROLLER: CONTROLLER,
ATTACHMENTS: ATTACHMENTS,
BATTERY: BATTERY,
EXTRAS: EXTRAS
}
}
Expand All @@ -57,25 +62,29 @@ class PlatformConfig(BaseConfig):
# PLATFORM
CONTROLLER: PS4,
ATTACHMENTS: {},
EXTRAS: ExtrasConfig.DEFAULTS
BATTERY: BatteryConfig.DEFAULTS,
EXTRAS: ExtrasConfig.DEFAULTS,
}

def __init__(
self,
config: dict = {},
controller: str = DEFAULTS[CONTROLLER],
attachments: str = DEFAULTS[ATTACHMENTS],
extras: str = DEFAULTS[EXTRAS]
battery: dict = DEFAULTS[BATTERY],
extras: dict = DEFAULTS[EXTRAS],
) -> None:
# Initialization
self._config = {}
self.controller = controller
self.attachments = attachments
self._battery = BatteryConfig(battery)
self._extras = ExtrasConfig(extras)
# Setter Template
setters = {
self.KEYS[self.CONTROLLER]: PlatformConfig.controller,
self.KEYS[self.ATTACHMENTS]: PlatformConfig.attachments,
self.KEYS[self.BATTERY]: PlatformConfig.battery,
self.KEYS[self.EXTRAS]: PlatformConfig.extras
}
super().__init__(setters, config, self.PLATFORM)
Expand All @@ -87,6 +96,8 @@ def update(self, serial_number=False) -> None:
# TODO: Set PACS Profile
# Reload extras
self.extras.update(serial_number=serial_number)
# Reload battery
self.battery.update(serial_number=serial_number)

@property
def controller(self) -> str:
Expand Down Expand Up @@ -139,3 +150,23 @@ def extras(self, value: dict | ExtrasConfig) -> None:

def get_controller(self) -> str:
return self.controller

@property
def battery(self) -> BatteryConfig:
self.set_config_param(
key=self.KEYS[self.BATTERY],
value=self._battery.config[self.BATTERY]
)
return self._battery

@battery.setter
def battery(self, value: dict | BatteryConfig) -> None:
if isinstance(value, dict):
self._battery.config = value
elif isinstance(value, BatteryConfig):
self._battery = value
else:
assert isinstance(value, dict) or (
isinstance(value, BatteryConfig)), (
"Battery configuration must be of type 'dict' or 'BatteryConfig'"
)
3 changes: 3 additions & 0 deletions clearpath_config/sample/a200/a200_default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ system:
workspaces: []
platform:
controller: ps4
battery:
model: ES20_12C
configuration: S2P1
attachments:
front_bumper:
enabled: true
Expand Down
3 changes: 3 additions & 0 deletions clearpath_config/sample/a200/a200_dual_laser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ system:
workspaces: []
platform:
controller: ps4
battery:
model: ES20_12C
configuration: S2P1
attachments:
front_bumper:
enabled: true
Expand Down
3 changes: 3 additions & 0 deletions clearpath_config/sample/a200/a200_sample.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ system:
workspaces: []
platform:
controller: ps4
battery:
model: ES20_12C
configuration: S2P1
attachments:
front_bumper:
enabled: true
Expand Down
3 changes: 3 additions & 0 deletions clearpath_config/sample/a200/a200_velodyne.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ system:
workspaces: []
platform:
controller: ps4
battery:
model: ES20_12C
configuration: S2P1
attachments:
front_bumper:
enabled: true
Expand Down
3 changes: 3 additions & 0 deletions clearpath_config/sample/j100/j100_default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ system:
workspaces: []
platform:
controller: ps4
battery:
model: HE2613
configuration: S1P1
attachments:
front_fender:
enabled: true
Expand Down
3 changes: 3 additions & 0 deletions clearpath_config/sample/j100/j100_dual_laser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ system:
workspaces: []
platform:
controller: ps4
battery:
model: HE2613
configuration: S1P1
attachments:
front_fender:
enabled: true
Expand Down
3 changes: 3 additions & 0 deletions clearpath_config/sample/j100/j100_sample.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ system:
workspaces: []
platform:
controller: ps4
battery:
model: HE2613
configuration: S1P1
attachments:
front_fender:
enabled: true
Expand Down
3 changes: 3 additions & 0 deletions clearpath_config/sample/j100/j100_velodyne.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ system:
workspaces: []
platform:
controller: ps4
battery:
model: HE2613
configuration: S1P1
attachments:
front_fender:
enabled: true
Expand Down

0 comments on commit 2af12f7

Please sign in to comment.