Bitwise operators

How can you do bitwise operations in library formula ? I’ve tried to use &,== and bitarray() but I couldn’t get a reliable results.

My goal is something like “If bit 4 of message.data[53:54] is 1 return bytes_to_int(message.data[13:14]) else return 0”

Thanks,

Pete

1 Like

Hi Peter

It should just work, as the formula is plain python code, although isolated.
I will take a look if we need to change the implementation to allow it.

EDIT: It seems that i’m able to at least do basic bitwise operations like &, ^ and ==

Best regards
/Malte

Doesn’t seem to work.

As a starter I tried bytes_to_int(message.data[12:13]) -

peter26@Kona $ obd.query Charge_Port mode=220 pid=101 header=7E
4 bytes=64 formula='bytes_to_int(message.data[12:13])' unit=% baudrate=500000 protocol=6 verify=fase force=true
value: 3
_stamp: '2019-03-22T17:56:56.623503'
_type: charge_port
unit: '%' 

So as expected. But then tried bytes_to_int(message.data[12:13])&4 -

peter26@Kona $ obd.query Charge_Port mode=220 pid=101 header=7E4 bytes=64 formula='bytes_to_int(message.data[12:13])&4' unit=% baudrate=500000 protocol=6 verify=false force=true
value: |-
  7EC103E620101FFF7E7
  7EC21FFBA1C21465003
  7EC2200170FC70B0B0A
  7EC230A0A0A00000ACE
  7EC2430CD4600009200
  7EC25007D3E00007A1D
  7EC2600002F8400002C
  7EC278800156BBE0D01
  7EC28930000000003E8
_stamp: '2019-03-22T17:57:17.011798'
_type: charge_port
unit: '%'

BTW, whet I really want to do is an if then else, but I’ve not managed to get that to work either.

Exploring this a little more.

My electric car gives battery voltage and current. We can multiply these together to get battery power - when driving this can sometimes become negative ( when regenerating ) and is always negative when charging. So my goal is to display driving power and charging power separately.

I believe I’ve located a way to detect charging, but this requires bitwise operations. So I’ve started to plot the following -

  • Raw byte that contains charging flag
    bytes_to_int(message.data[53:54])
  • Bit that contains the charging flag - should give 1 when driving, 0 when charging
    (((bytes_to_int(message.data[53:54]))&4)/4)
  • Battery power (A * V in Kw)
    (twos_comp(bytes_to_int(message.data[13:14])*256+bytes_to_int(message.data[14:15]),16)/10.0)*((bytes_to_int(message.data[15:16])*256+bytes_to_int(message.data[16:17]))/10.0)/1000.0
  • Finally only driving power
    ((bytes_to_int(message.data[53:54])&4)/4)*((twos_comp(bytes_to_int(message.data[13:14])*256+bytes_to_int(message.data[14:15]),16)/10.0)*((bytes_to_int(message.data[15:16])*256+bytes_to_int(message.data[16:17]))/10.0)/1000.0)

All of these are sampled every 10 seconds ( I assume that autopi optimizes OBD queries so that the same PId is fetched only ones ).

The first oddity I noticed is that I only see one data point for the charging flag -

30

I assume this is a plotting optimization … but I would have expected at least a second point on the second plot.

But secondly I see very different plots between the battery power and driving power - given that driving power is battery power multiplied by either 1 or 0 I would expect these two to match up when actually driving -

10

Is there an issue here ?

Thanks.

( BTW if you try to name a widget with a long name, the save dashboard command never completes )

1 Like

This data seems to be confirmed when fetching via the API ( https://api.autopi.io/logbook/storage/read/ ) -

raw charging flag ( two data points ) -

[
  {
    "max_ts": 1553792567552,
    "ts": "2019-03-28T17:02:40.000Z",
    "value": 13
  },
  {
    "max_ts": 1553794535032,
    "ts": "2019-03-28T17:35:30.000Z",
    "value": 1
  }
]

Charging bit ( just one data point ) -

[
  {
    "max_ts": 1553792569336,
    "ts": "2019-03-28T17:02:40.000Z",
    "value": 1
  }
]

Battery power -

[
  {
    "max_ts": 1553792566436,
    "ts": "2019-03-28T17:02:40.000Z",
    "value": 15.414750099182129
  },
  {
    "max_ts": 1553792579659,
    "ts": "2019-03-28T17:02:50.000Z",
    "value": 20.94243049621582
  },
  {
    "max_ts": 1553792591512,
    "ts": "2019-03-28T17:03:10.000Z",
    "value": -5.890989780426025
  },
  {
    "max_ts": 1553792604275,
    "ts": "2019-03-28T17:03:20.000Z",
    "value": 5.027339935302734
  },

Driving power ( battery power * charging bit ) -

[
  {
    "max_ts": 1553792568617,
    "ts": "2019-03-28T17:02:40.000Z",
    "value": -31.720500946044922
  },
  {
    "max_ts": 1553792580745,
    "ts": "2019-03-28T17:03:00.000Z",
    "value": -11.970720291137695
  },
  {
    "max_ts": 1553792592700,
    "ts": "2019-03-28T17:03:10.000Z",
    "value": -5.82735013961792
  },
  {
    "max_ts": 1553792607758,
    "ts": "2019-03-28T17:03:20.000Z",
    "value": -8.264249801635742
  },

Any thoughts ?

Hi Peter

FYI I am looking into this.

Br
/Malte

1 Like