Skip to content
This repository has been archived by the owner on Oct 28, 2023. It is now read-only.

Commit

Permalink
Merge branch 'main' into feature/ptcs-update-4
Browse files Browse the repository at this point in the history
  • Loading branch information
n4o847 committed Oct 13, 2023
2 parents d16eb08 + ab0689e commit eb8f230
Show file tree
Hide file tree
Showing 11 changed files with 154 additions and 7 deletions.
4 changes: 4 additions & 0 deletions ptcs/ptcs_bridge/train_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

NotifyPositionIdCallback = Callable[["TrainBase", int], None]
NotifyRotationCallback = Callable[["TrainBase", int], None]
NotifyVoltageCallback = Callable[["TrainBase", int], None]


class TrainBase:
Expand All @@ -24,3 +25,6 @@ async def start_notify_position_id(self, callback: NotifyPositionIdCallback) ->

async def start_notify_rotation(self, callback: NotifyRotationCallback) -> None:
raise NotImplementedError()

async def start_notify_voltage(self, callback: NotifyVoltageCallback) -> None:
raise NotImplementedError()
21 changes: 20 additions & 1 deletion ptcs/ptcs_bridge/train_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,18 @@
from bleak.backends.characteristic import BleakGATTCharacteristic
from bleak.backends.service import BleakGATTService

from .train_base import NotifyPositionIdCallback, NotifyRotationCallback, TrainBase
from .train_base import (
NotifyPositionIdCallback,
NotifyRotationCallback,
NotifyVoltageCallback,
TrainBase,
)

SERVICE_TRAIN_UUID = UUID("63cb613b-6562-4aa5-b602-030f103834a4")
CHARACTERISTIC_MOTOR_INPUT_UUID = UUID("88c9d9ae-bd53-4ab3-9f42-b3547575a743")
CHARACTERISTIC_POSITION_ID_UUID = UUID("8bcd68d5-78ca-c1c3-d1ba-96d527ce8968")
CHARACTERISTIC_ROTATION_UUID = UUID("aab17457-2755-8b50-caa1-432ff553d533")
CHARACTERISTIC_VOLTAGE_UUID = UUID("7ecc0ed2-5ef9-c9e6-5d16-582f86035ecf")


logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -55,6 +61,9 @@ def _get_characteristic_position_id(self) -> BleakGATTCharacteristic:
def _get_characteristic_rotation(self) -> BleakGATTCharacteristic:
return self._get_characteristic(CHARACTERISTIC_ROTATION_UUID)

def _get_characteristic_voltage(self) -> BleakGATTCharacteristic:
return self._get_characteristic(CHARACTERISTIC_VOLTAGE_UUID)

async def send_motor_input(self, motor_input: int) -> None:
assert isinstance(motor_input, int)
assert 0 <= motor_input <= 255
Expand All @@ -81,3 +90,13 @@ def wrapped_callback(_characteristic: BleakGATTCharacteristic, data: bytearray):

characteristic = self._get_characteristic_rotation()
await self._client.start_notify(characteristic, wrapped_callback)

async def start_notify_voltage(self, callback: NotifyVoltageCallback) -> None:
def wrapped_callback(_characteristic: BleakGATTCharacteristic, data: bytearray):
assert len(data) == 4
voltage = int.from_bytes(data, byteorder="little")
logger.info("%s notify voltage %s mV", self, voltage)
callback(self, voltage)

characteristic_voltage = self._get_characteristic_voltage()
await self._client.start_notify(characteristic_voltage, wrapped_callback)
10 changes: 9 additions & 1 deletion ptcs/ptcs_bridge/train_simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@
import logging
import math

from .train_base import NotifyPositionIdCallback, NotifyRotationCallback, TrainBase
from .train_base import (
NotifyPositionIdCallback,
NotifyRotationCallback,
NotifyVoltageCallback,
TrainBase,
)

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -81,6 +86,9 @@ async def start_notify_position_id(self, callback: NotifyPositionIdCallback) ->
async def start_notify_rotation(self, callback: NotifyRotationCallback) -> None:
self._notify_rotation_callback = callback

async def start_notify_voltage(self, callback: NotifyVoltageCallback) -> None:
raise NotImplementedError()


async def main():
t0 = TrainSimulator("t0")
Expand Down
1 change: 1 addition & 0 deletions ptcs/ptcs_client/dist/models/TrainState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ export type TrainState = {
stop_distance: number;
departure_time: (number | null);
speed_command: number;
voltage_mV: number;
};
1 change: 1 addition & 0 deletions ptcs/ptcs_control/components/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class Train(BaseComponent):
stop: Stop | None = field(default=None) # 列車の停止目標
stop_distance: float = field(default=0.0) # 停止目標までの距離[cm]
departure_time: int | None = field(default=None) # 発車予定時刻
voltage_mV: int = field(default=0) # 電池電圧[mV]

# commands
speed_command: float = field(default=0.0) # 速度指令値
Expand Down
7 changes: 7 additions & 0 deletions ptcs/ptcs_server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,18 @@ def handle_notify_rotation(train_client: TrainBase, _rotation: int):
return
train_control.move_forward_mr(1)

def handle_notify_voltage(train_client: TrainBase, _voltage_mV: int):
train_control = control.trains.get(train_client.id)
if train_control is None:
return
train_control.voltage_mV = _voltage_mV

for train in bridge.trains.values():
match train:
case TrainClient():
await train.start_notify_position_id(handle_notify_position_id)
await train.start_notify_rotation(handle_notify_rotation)
# await train.start_notify_voltage(handle_notify_voltage)

def handle_notify_collapse(obstacle_client: WirePoleClient, is_collapsed: bool):
obstacle_control = control.obstacles.get(obstacle_client.id)
Expand Down
2 changes: 2 additions & 0 deletions ptcs/ptcs_server/types/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class TrainState(BaseModel):
stop_distance: float
departure_time: int | None
speed_command: float
voltage_mV: int

@staticmethod
def from_control(train: Train) -> TrainState:
Expand All @@ -80,6 +81,7 @@ def from_control(train: Train) -> TrainState:
stop_distance=train.stop_distance,
departure_time=train.departure_time,
speed_command=train.speed_command,
voltage_mV=train.voltage_mV,
)


Expand Down
78 changes: 78 additions & 0 deletions ptcs/ptcs_ui/dist/assets/index-5699ed18.js

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions ptcs/ptcs_ui/dist/assets/index-a74e297b.js

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions ptcs/ptcs_ui/src/components/Information.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export const Information: React.FC = () => {
<th></th>
<th>train</th>
<th>speed</th>
<th>voltage</th>
</tr>
</thead>
<tbody>
Expand All @@ -30,6 +31,7 @@ export const Information: React.FC = () => {
</td>
<td>{id}</td>
<td>{train.speed_command.toFixed(2)}</td>
<td>{train.voltage_mV}</td>
</tr>
))}
</tbody>
Expand Down
31 changes: 26 additions & 5 deletions train/train_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,25 @@
if platform.system() == "Windows":
ADDRESS_T0 = 'e0:5a:1b:e2:7a:f2'
ADDRESS_T1 = '94:b5:55:84:15:42'
ADDRESS_T2 = 'e0:5a:1b:e2:7b:1e'
ADDRESS_T3 = '1c:9d:c2:66:84:32'
ADDRESS_T4 = '24:4c:ab:f5:c6:3e'


elif platform.system() == "Darwin":
ADDRESS_T0 = "00B55AE6-34AA-23C2-8C7B-8C11E6998E12"
ADDRESS_T1 = "F2158243-18BB-D34C-88BC-F8F193CAD15E"
ADDRESS_T2 = 'EB57E065-90A0-B6D0-98BA-81096FA5765E'
ADDRESS_T3 = '4AA3AAE5-A039-8484-013C-32AD94F50BE0'
ADDRESS_T4 = 'FC44FB3F-CF7D-084C-EA29-7AFD10C47A57'

else:
raise Exception(f"{platform.system()} not supported")

####### TODO: 車両のアドレスを指定してください #######
address = ADDRESS_T2
#################################################

SERVICE_UUID = "63cb613b-6562-4aa5-b602-030f103834a4"
CHARACTERISTIC_SPEED_UUID = "88c9d9ae-bd53-4ab3-9f42-b3547575a743"
CHARACTERISTIC_POSITIONID_UUID = "8bcd68d5-78ca-c1c3-d1ba-96d527ce8968"
Expand All @@ -32,8 +44,7 @@ async def main():
for d in devices:
print(" - ", d)

####### TODO: クライアントのアドレスを選択してください #######
async with BleakClient(ADDRESS_T0) as client:
async with BleakClient(address) as client:
print("Connected to", client)
print("Services:")
for service in client.services:
Expand All @@ -52,12 +63,22 @@ async def main():
await client.start_notify(characteristicRotation, rotationNotification_callback)
await client.start_notify(characteristicVoltage, voltageNotification_callback)

i=150 #3V時171で回り始める
while(i<=255):
i = i+1
if address == ADDRESS_T0:
i = 220
elif address == ADDRESS_T1:
i = 200
elif address == ADDRESS_T2:
i = 210
elif address == ADDRESS_T3:
i = 220
elif address == ADDRESS_T4:
i = 210

while(i<256):
await client.write_gatt_char(characteristicSpeed, f"{i}".encode())
print("motorInput:", i)
await asyncio.sleep(0.3)



async def positionIdNotification_callback(sender, data):
Expand Down

0 comments on commit eb8f230

Please sign in to comment.