hardbyte / hardbyte/python-can

Dealing with negative timestamps - MF4Writer Notifier

Abierto
#1,629 3 comentarios 0 reacciones 0 asignados Ver en GitHub
bug
Lenguaje dominante
Python
Estrellas
1.6k
Forks
697
Métricas de merge de PR
Sin PR fusionados en 30 d

Descripción

### DESCRIPTION

I'm using a notifier to read messages from my device CAN Bus, this notifier is equipped with the MF4 Writer listener. See below:

```
mf4_builder = can.MF4Writer( # type: ignore
file=Path(working_dir, f"mf4/can_listener_{log_filename}.mf4"),
database=dbc_path,
mode="wb",
compression_level=COMPRESSION_LEVEL,
)

listeners: List[can.notifier.MessageRecipient] = [
mf4_builder,
]

can_bus.flush_tx_buffer()
global notifier
notifier = can.Notifier(can_bus, listeners) # type: ignore
```

Everything all right until here.

To test my script I'm using a VECTOR 1630A + SW CANalyzer to generate some messages in my CAN bus.

Then, I start my CANalyzer simulation and my python script (notifier).

After my MF4 file reaches a specific file size, I close this file and start a new one, this is necessary to save RAM memory, I can't keep a large file size because I have a limited HW.

Below, the part of my code where I close the notifier, and so, also the MF4 file.

```
if flag == STATE["RUNNING"] and est_size >= FILE_SIZE:
logger.info("Completed file size: Stopping current notifier ...")
notifier.stop(timeout=5)
logger.info("Notifier stopped.")
flag = STATE["START"]
```

No problems until here.

BUT, this time between I close the current file (notifier) and open a new one, is enough to accumulate some messages in the CAN bus.

And here stands the problem, when the new file starts it consumes the messages accumulated in the CAN bus (before the new notifier start), and these messages are wrote in the MF4 file with negative timestamps (which make sense but generate a problem, because I don't want negative timestamps.)

See in the link below (google drive):

[Negative Timestamps](https://drive.google.com/file/d/1gf-OgfwIPE2Lg93GbOToRLCcHec-kQBf/view?usp=drive_link)

That's my problem.

### SOLUTION

To solve that I had to do a little change in the python-can library, in the mf4.py sheet.

The library timestamp adjustment occurs here:

```
channel = channel2int(msg.channel)
timestamp = msg.timestamp
if timestamp is None:
timestamp = self.last_timestamp
else:
self.last_timestamp = max(self.last_timestamp, timestamp)
timestamp -= self._start_time # timestamp = timestamp - self._start_time
```

Now, I'm doing this:

```
channel = channel2int(msg.channel)
timestamp = msg.timestamp
if timestamp is None:
timestamp = self.last_timestamp
else:
self.last_timestamp = max(self.last_timestamp, timestamp)

timestamp -= self._start_time # timestamp = timestamp - self._start_time

# Henrique R Silva 2023-07-04 - Start
if abs(self.timestamp_factor) == 0.0:
self.timestamp_factor = abs(timestamp)
timestamp = 0.0
else:
timestamp += self.timestamp_factor
# Henrique R Silva 2023-07-04 - End
```

### QUESTION

Could someone of you, please, give me a feedback on this ?

Is this the way to solve that ?

Are there any other more elegant way to do it ?

And besides that, these negative timestamps, did you see some behavior like that before ?

I would appreciate any help that I could get here.

Thank you.

### Additional context

OS and version: Debian 10
Python version: 3.7
python-can version: 4.2.2
python-can interface/s (if applicable): CAN1

Guía de contribución

Abrir la guía de contribución

Evaluación

Este issue todavía no se ha evaluado.

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.