piracer 전압값을 통한 배터리 잔량 dbus 전송

jalee·2023년 8월 9일
0

piracer 내장 배터리의 전압값을 piracer 라이브러리를 통해 받아오고 이를 통해 배터리 잔량을 다음 식으로 추정이 가능하다.

send_fuel.py

# Copyright (C) 2022 twyleg
import time
import math
from piracer.vehicles import PiRacerStandard
from pydbus import SessionBus
from gi.repository import GLib

class dbusService:
    '''
    DBus Service Example
    '''
    dbus = """
    <node>
        <interface name='com.example.dbusService'>
            <method name='vol'>
                <arg type='s' name='voltage' direction='out'/>
            </method>
        </interface>
    </node>
    """

    def __init__(self)->str:
        self.piracer = PiRacerStandard()
        self.voltage =0
        

    def vol(self)->str:    
        self.piracer = PiRacerStandard()
        self.voltage = self.piracer.get_battery_voltage()
        self.voltage = 49.76*math.sin(1.546*self.voltage + 1.001) + 48.31
        volt = '{0:0>6.3f}'.format(self.voltage)
        return volt
            

bus = SessionBus()
bus.publish("com.example.dbusService", dbusService())
loop = GLib.MainLoop()
loop.run()

이를 받아온 후 pydbus를 사용하여 전송하는 코드이다. 전송값은 다음 코드를 통해 확인이 가능하다

recv_fuel.py

from pydbus import SessionBus
import time
#from gi.repository import GLib

# Connect to the session bus
bus = SessionBus()
remote_object = bus.get('com.example.dbusService',"/com/example/dbusService")


while(1):
    
    print(remote_object.vol())

다음과 같은 명령어로 실행 가능하다.

python3 send_fuel.py &
python3 recv_fuel.py

0개의 댓글