Skyward boardcore
Loading...
Searching...
No Matches
Propagator.cpp
Go to the documentation of this file.
1/* Copyright (c) 2024 Skyward Experimental Rocketry
2 * Author: Emilio Corigliano
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#include "Propagator.h"
24
27#include <utils/Debug.h>
28
29using namespace Eigen;
30
31namespace Boardcore
32{
33
34static constexpr bool useAcceleration =
35 true; // set true to use the propagator with acceleration
36
37Propagator::Propagator(std::chrono::milliseconds pUpdatePeriod)
38 : updatePeriod(static_cast<float>(pUpdatePeriod.count()) / 1000), state()
39{
40}
41
42bool Propagator::init() { return true; }
43
44void Propagator::step()
45{
46 miosix::Lock<miosix::FastMutex> lock(stateMutex);
47 // Take new rocket data only if it has been just updated, otherwise take
48 // last state available
49
50 PropagatorState oldState = state;
51
52 // updates with the last received NAS state if present, otherwise uses the
53 // last Propagator state
54 state = (oldState.nPropagations == 0
56 oldState.timestamp, oldState.nPropagations,
57 getRocketNasState()) // TODO: Uh? The timestamp? SUS
58 : oldState);
59
60 if (state.nPropagations == 0)
61 {
62 // Update the timestamp of the last received packet
63 lastReceivedTime = TimestampTimer::getTimestamp();
64
65 // Compute the acceleration to change the velocity
66 if (useAcceleration) // Update Position assuming constant acceleration
67 {
69 float dt = t1 - t0;
70 if (dt > 0 && dt < MAX_ACCELERATION_TIME.count() && t0 != 0)
71 state.setZAcceleration(
72 (state.getVelocity() - last_real_velocity) /
73 (dt / 1000000));
74 t0 = t1;
75 last_real_velocity = state.getVelocity();
76 }
77 }
78
79 // If we use the acceleration we update the velocity
80 if (useAcceleration &&
81 TimestampTimer::getTimestamp() < t0 + MAX_ACCELERATION_TIME.count())
82 state.setVelocity(state.getVelocity() +
83 state.getAcceleration() * updatePeriod);
84
85 // In case the time do not exceed the maximum propagation time or it is a
86 // new packet we update the state
88 lastReceivedTime + MAX_PROPAGATION_TIME.count() ||
89 state.nPropagations == 0)
90 {
91 state.setPosition(state.getPosition() +
92 state.getVelocity() * updatePeriod);
93 state.nPropagations++;
94 }
95 else
96 state = oldState;
97
99
100 // Log propagator state
101 PropagatorState logState(state);
102 Boardcore::Logger::getInstance().log(logState);
103}
104
105void Propagator::setRocketNasState(const NASState& newRocketNasState)
106{
107 miosix::Lock<miosix::FastMutex> lockState(stateMutex);
108 miosix::Lock<miosix::FastMutex> lock(nasStateMutex);
109
110 // Reset nPropagations to notify another received "real" packet
111 state.nPropagations = 0;
112 lastRocketNasState = newRocketNasState;
113
114 // Using as timestamp ARP timestamp
116}
117
118} // namespace Boardcore
NASState getRocketNasState()
Synchronized getter for the last rocket NAS State passed to the propagator.
Definition Propagator.h:80
void setRocketNasState(const NASState &newRocketNasState)
Synchronized setter for the latest rocket nas state. Also notifies the predictor of a new packet arri...
Propagator(std::chrono::milliseconds updatePeriod)
Constructor of the propagator class.
bool init() override
Dummy init since we don't have to setup anything.
static Logger & getInstance()
Definition Singleton.h:52
uint64_t getTimestamp()
Returns the current timer value in microseconds.
This file includes all the types the logdecoder script will decode.
State of the propagator, taking into account the prediction steps (0 if true NAS state) and the propa...
Eigen::Vector3f getVelocity()
Getter for the vector of velocities NED.
void setZAcceleration(Eigen::Vector3f acc)
Setter for the vector acceleration(only z-axis)
Eigen::Vector3f getPosition()
Getter for the vector of positions NED.
void setPosition(Eigen::Vector3f xProp)
Setter for the vector of positions NED.
void setVelocity(Eigen::Vector3f vProp)
Setter for the vector of velocities NED.
uint32_t nPropagations
Predictions from last received NAS state.
uint64_t timestamp
Prediction timestamp (ARP timestamp) [ms].
Eigen::Vector3f getAcceleration() const
Getter for the vector acceleration.