Skyward boardcore
Loading...
Searching...
No Matches
BusLoadEstimation.h
Go to the documentation of this file.
1/* Copyright (c) 2018 Skyward Experimental Rocketry
2 * Author: Luca Erbetta
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22
23#pragma once
24
25#include <miosix.h>
27
28#include <cstdint>
29
30#include "CanDriverData.h"
31
32using miosix::FastMutex;
33using miosix::Lock;
34
35namespace Boardcore
36{
37
38namespace Canbus
39{
40
42{
43 static constexpr uint16_t BUFFER_LEN = 100;
44
45public:
47 {
51 };
52
53 BusLoadEstimation(uint32_t baudRate) : baudRate(baudRate) {}
54
56 {
57 Lock<FastMutex> l(mutex);
58 c.put(PacketInfo{p.timestamp, p.length});
59 }
60
62 {
63 Lock<FastMutex> l(mutex);
64 if (c.count() < 2)
65 return {0, 0, 0};
66
67 float dt =
68 (c.get(c.count() - 1).timestamp - c.get(0).timestamp) / 1000.0f;
69 uint32_t sizePayload = 0;
70 uint32_t sizeFrames = 0;
71
72 for (size_t i = 0; i < c.count(); ++i)
73 sizePayload += c.get(i).dataLength * 8;
74 sizeFrames = sizePayload + (64 + 8) * c.count();
75
76 return BusLoadInfo{(sizePayload / dt), (sizeFrames / dt),
77 ((sizeFrames / dt) / baudRate) * 100};
78 }
79
80private:
81 struct PacketInfo
82 {
83 uint32_t timestamp;
84 uint8_t dataLength;
85 };
86
87 CircularBuffer<PacketInfo, BUFFER_LEN> c;
88 mutable FastMutex mutex;
89 uint32_t baudRate;
90};
91
92} // namespace Canbus
93
94} // namespace Boardcore
T & put(const T &elem)
Puts a copy of the element in the buffer.
size_t count() const
Counts the elements in the buffer.
T & get(unsigned int i=0)
Gets an element from the buffer, without removing it.
PrintLogger l
Definition CanDriver.cpp:41
This file includes all the types the logdecoder script will decode.