Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mtc Nucleo board connection #115

Open
wants to merge 31 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
34e1877
Started frequency sender
TomLonergan03 Feb 12, 2022
0c753ff
Added STM CAN data frame
TomLonergan03 Feb 12, 2022
f4ac389
STM CAN sender
TomLonergan03 Feb 12, 2022
9c46d21
Rename stm to nucleo
TomLonergan03 Feb 12, 2022
0a16c38
Moved nucleo management to new class
TomLonergan03 Feb 12, 2022
dea68a6
Splitting out can sender from transceiver
TomLonergan03 Feb 12, 2022
cd8c79b
Split sender and transceiver
TomLonergan03 Feb 13, 2022
8d64707
Added nucleo frequency sender
TomLonergan03 Feb 13, 2022
b3dccab
Fixes
TomLonergan03 Feb 27, 2022
df754b2
Revert logger changes
TomLonergan03 Mar 3, 2022
49067e8
replaced int division with rounding
TomLonergan03 Mar 3, 2022
7d3024c
Moved nucleo manager to unique ptr
TomLonergan03 Mar 11, 2022
492b3bd
Nucleo manager owns own logger
TomLonergan03 Mar 11, 2022
59e4882
Changed all loggers to new style
TomLonergan03 Mar 14, 2022
546f159
Removed unused is_sending from non-fake can system
TomLonergan03 Mar 14, 2022
0084ef8
Can sender owns can instance, transceiver inherits from sender
TomLonergan03 Mar 15, 2022
3538b73
Moved nucleo_manager_ construction
TomLonergan03 Mar 15, 2022
966a5b2
Can transceiver is dead, long live can receiver
TomLonergan03 Mar 15, 2022
fbf3c25
Renamed CAN interfaces
TomLonergan03 Mar 15, 2022
dc8ad4a
Reorder and cleanup can ids
TomLonergan03 Mar 22, 2022
01f1fe1
Consts
TomLonergan03 Mar 22, 2022
e3fb1ab
Clarified casting
TomLonergan03 Mar 22, 2022
0744837
Arrays start at 0
TomLonergan03 Mar 22, 2022
b926539
Moved include statements
TomLonergan03 Mar 24, 2022
c6fb226
Fixes
TomLonergan03 Mar 24, 2022
bf038ca
Removed unused ids
TomLonergan03 Mar 24, 2022
b7948a3
Minor fixes
TomLonergan03 Apr 5, 2022
07c6652
Fix merge conflicts
TomLonergan03 Apr 5, 2022
42799b4
Merge branch 'master' into mtc-frequency_gen
TomLonergan03 May 10, 2022
3d4e3a8
Spelling
TomLonergan03 May 10, 2022
f3dd33f
Fixing merge issues
TomLonergan03 May 10, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/propulsion/can/can_ids.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#pragma once
#include <stdint.h>
namespace hyped {

namespace propulsion {

// Types of CANopen messages, these are used for CAN ID's
static constexpr uint32_t kEmgyTransmit = 0x80;
static constexpr uint32_t kSdoReceive = 0x600;
static constexpr uint32_t kSdoTransmit = 0x580;
static constexpr uint32_t kNmtReceive = 0x000;
static constexpr uint32_t kNmtTransmit = 0x700;
static constexpr uint32_t kPdo1Transmit = 0x180;
static constexpr uint32_t kPdo1Receive = 0x200;
static constexpr uint32_t kPdo2Transmit = 0x280;
static constexpr uint32_t kPdo2Receive = 0x300;
static constexpr uint32_t kPdo3Transmit = 0x380;
static constexpr uint32_t kPdo3Receive = 0x400;
static constexpr uint32_t kPdo4Transmit = 0x480;
static constexpr uint32_t kPdo4Receive = 0x500;
static constexpr uint32_t kNucleoTransmit = 0x900; // currently lowest priority, might change

static constexpr uint32_t canIds[14]{0x80, 0x600, 0x580, 0x000, 0x700, 0x180, 0x200,
TomLonergan03 marked this conversation as resolved.
Show resolved Hide resolved
0x280, 0x300, 0x380, 0x400, 0x480, 0x500, 0x900};

} // namespace propulsion
} // namespace hyped
40 changes: 40 additions & 0 deletions src/propulsion/can/can_receiver.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include "can_receiver.hpp"

namespace hyped::propulsion {

CanReceiver::CanReceiver(const uint8_t node_id, IController &controller)
: log_("CAN-TRANSCEIVER", utils::System::getSystem().config_.log_level_propulsion),
node_id_(node_id),
controller_(controller),
can_(utils::io::Can::getInstance())
{
can_.start();
}

void CanReceiver::registerController()
{
can_.registerProcessor(this);
}

void CanReceiver::processNewData(utils::io::can::Frame &message)
{
uint32_t id = message.id;
if (id == kEmgyTransmit + node_id_) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do we ensure that there are no conflicts here? Do we limit node IDs?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Each node_id_ is numbered from 0 and there is one node for each motor controller so we won't have one overflow unless we have 21 motor controllers

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we know the BMS is not using these nodes?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BMS starts at node 300 as far as i can tell

controller_.processEmergencyMessage(message);
} else if (id == kSdoTransmit + node_id_) {
controller_.processSdoMessage(message);
} else if (id == kNmtTransmit + node_id_) {
controller_.processNmtMessage(message);
} else {
log_.error("Controller %d: CAN message not recognised", node_id_);
}
}

bool CanReceiver::hasId(uint32_t id, bool)
mifrandir marked this conversation as resolved.
Show resolved Hide resolved
{
for (uint32_t cobId : canIds) {
TomLonergan03 marked this conversation as resolved.
Show resolved Hide resolved
if (cobId + node_id_ == id) { return true; }
}
return false;
}
} // namespace hyped::propulsion
52 changes: 52 additions & 0 deletions src/propulsion/can/can_receiver.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#pragma once

#include "can_ids.hpp"
#include "receiver_interface.hpp"

#include <atomic>
#include <iostream>

#include <propulsion/controller_interface.hpp>
#include <utils/concurrent/thread.hpp>
#include <utils/io/can.hpp>
#include <utils/logger.hpp>
#include <utils/system.hpp>

namespace hyped::propulsion {

class CanReceiver : public utils::io::CanProccesor, public ICanReceiver {
public:
/**
* @brief Initialise the CanReceiver with the id and the controller as an
* attribute, to access it's attributes
*/
CanReceiver(const uint8_t node_id, IController &controller);

/**
* @brief Registers the controller to process incoming CAN messages
*/
void registerController() override;

/**
* @brief This function processes incoming CAN messages
*/
void processNewData(utils::io::can::Frame &message) override;

/**
* @brief If this function returns true, the CAN message is ment for this CAN node
*/
bool hasId(uint32_t id, bool extended) override;

private:
utils::Logger log_;
uint8_t node_id_;
utils::io::Can &can_;
IController &controller_;

static constexpr uint32_t kEmgyTransmit = 0x80;
TomLonergan03 marked this conversation as resolved.
Show resolved Hide resolved
static constexpr uint32_t kSdoTransmit = 0x580;
static constexpr uint32_t kNmtTransmit = 0x700;
static constexpr uint64_t kTimeout = 70000; // us
};

} // namespace hyped::propulsion
57 changes: 4 additions & 53 deletions src/propulsion/can/can_sender.cpp
Original file line number Diff line number Diff line change
@@ -1,65 +1,16 @@
#include "can_sender.hpp"

#include <utils/timer.hpp>

namespace hyped::propulsion {

CanSender::CanSender(utils::Logger &log, const uint8_t node_id, IController &controller)
: log_(log),
node_id_(node_id),
can_(utils::io::Can::getInstance()),
controller_(controller)
CanSender::CanSender()
: log_("CAN-SENDER", utils::System::getSystem().config_.log_level_propulsion),
can_(utils::io::Can::getInstance())
{
is_sending_ = false;
can_.start();
}

bool CanSender::sendMessage(utils::io::can::Frame &message)
TomLonergan03 marked this conversation as resolved.
Show resolved Hide resolved
{
log_.info("Sending Message");
can_.send(message);
is_sending_ = true;
const auto now = utils::Timer::getTimeMicros();
while (is_sending_) {
if ((utils::Timer::getTimeMicros() - now) > kTimeout) {
// TODO(Iain): Test the latency and set the TIMEOUT to a reasonable value.
log_.error("Sender timeout reached");
return false;
}
}
return true;
}

void CanSender::registerController()
{
can_.registerProcessor(this);
}

void CanSender::processNewData(utils::io::can::Frame &message)
{
is_sending_ = false;
uint32_t id = message.id;
if (id == kEmgyTransmit + node_id_) {
controller_.processEmergencyMessage(message);
} else if (id == kSdoTransmit + node_id_) {
controller_.processSdoMessage(message);
} else if (id == kNmtTransmit + node_id_) {
controller_.processNmtMessage(message);
} else {
log_.error("Controller %d: CAN message not recognised", node_id_);
}
}

bool CanSender::hasId(uint32_t id, bool)
{
for (uint32_t cobId : canIds) {
if (cobId + node_id_ == id) { return true; }
}
return false;
}

bool CanSender::getIsSending()
{
return is_sending_;
return can_.send(message);
}
} // namespace hyped::propulsion
49 changes: 4 additions & 45 deletions src/propulsion/can/can_sender.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,60 +2,19 @@

#include "sender_interface.hpp"

#include <atomic>
#include <iostream>

#include <propulsion/controller_interface.hpp>
#include <utils/concurrent/thread.hpp>
#include <utils/io/can.hpp>
#include <utils/logger.hpp>
#include <utils/system.hpp>
TomLonergan03 marked this conversation as resolved.
Show resolved Hide resolved

namespace hyped::propulsion {

class CanSender : public utils::io::CanProccesor, public ISender {
class CanSender : public ICanSender {
public:
/**
* @brief Initialise the CanSender with the logger, the id and the controller as an attribute,
* to access it's attributes
*/
CanSender(utils::Logger &log, const uint8_t node_id, IController &controller);
CanSender();

/**
* @brief Sends CAN messages
*/
bool sendMessage(utils::io::can::Frame &message) override;

/**
* @brief Registers the controller to process incoming CAN messages
*/
void registerController() override;

/**
* @brief This function processes incoming CAN messages
*/
void processNewData(utils::io::can::Frame &message) override;

/**
* @brief If this function returns true, the CAN message is ment for this CAN node
*/
bool hasId(uint32_t id, bool extended) override;

/**
* @brief Return if the can_sender is sending a CAN message right now
*/
bool getIsSending() override;

private:
utils::Logger &log_;
uint8_t node_id_;
utils::io::Can &can_;
std::atomic<bool> is_sending_;
TomLonergan03 marked this conversation as resolved.
Show resolved Hide resolved
IController &controller_;

static constexpr uint32_t kEmgyTransmit = 0x80;
static constexpr uint32_t kSdoTransmit = 0x580;
static constexpr uint32_t kNmtTransmit = 0x700;
static constexpr uint64_t kTimeout = 70000; // us
utils::Logger log_;
};

} // namespace hyped::propulsion
30 changes: 30 additions & 0 deletions src/propulsion/can/fake_can_receiver.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include "fake_can_receiver.hpp"

namespace hyped::propulsion {
FakeCanReceiver::FakeCanReceiver(uint8_t)
TomLonergan03 marked this conversation as resolved.
Show resolved Hide resolved
: log_("FAKE-CAN-SENDER", utils::System::getSystem().config_.log_level_propulsion),
is_sending_(false),
can_endpoint_(*this)
{
}

void FakeCanReceiver::registerController()
{
}

void FakeCanReceiver::processNewData(utils::io::can::Frame &)
{
log_.info("processNewData");
is_sending_ = false;
}

bool FakeCanReceiver::hasId(uint32_t, bool)
{
return true;
}

bool FakeCanReceiver::getIsSending()
{
return is_sending_;
}
} // namespace hyped::propulsion
34 changes: 34 additions & 0 deletions src/propulsion/can/fake_can_receiver.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#pragma once

#include "fake_can_endpoint.hpp"
#include "receiver_interface.hpp"

#include <atomic>
#include <iostream>

#include <utils/io/can.hpp>
#include <utils/logger.hpp>
#include <utils/system.hpp>

namespace hyped::propulsion {

class FakeCanReceiver : public utils::io::CanProccesor, public ICanReceiver {
public:
FakeCanReceiver(uint8_t id);

void registerController() override;

void processNewData(utils::io::can::Frame &message) override;

bool hasId(uint32_t id, bool extended) override;

bool getIsSending();

private:
utils::Logger log_;
// Can& can_;
std::atomic<bool> is_sending_;
FakeCanEndpoint can_endpoint_;
};

} // namespace hyped::propulsion
19 changes: 2 additions & 17 deletions src/propulsion/can/fake_can_sender.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include "fake_can_sender.hpp"

namespace hyped::propulsion {
FakeCanSender::FakeCanSender(utils::Logger &log_, uint8_t)
: log_(log_),
FakeCanSender::FakeCanSender(uint8_t)
: log_("FAKE-CAN-SENDER", utils::System::getSystem().config_.log_level_propulsion),
is_sending_(false),
can_endpoint_(*this)
{
Expand All @@ -19,21 +19,6 @@ bool FakeCanSender::sendMessage(utils::io::can::Frame &)
return true;
}

void FakeCanSender::registerController()
{
}

void FakeCanSender::processNewData(utils::io::can::Frame &)
{
log_.info("processNewData");
is_sending_ = false;
}

bool FakeCanSender::hasId(uint32_t, bool)
{
return true;
}

bool FakeCanSender::getIsSending()
{
return is_sending_;
Expand Down
13 changes: 4 additions & 9 deletions src/propulsion/can/fake_can_sender.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,17 @@

#include <utils/io/can.hpp>
#include <utils/logger.hpp>
#include <utils/system.hpp>

namespace hyped::propulsion {

class FakeCanSender : public utils::io::CanProccesor, public ISender {
class FakeCanSender : public utils::io::CanProccesor, public ICanSender {
public:
FakeCanSender(utils::Logger &log_, uint8_t id);
FakeCanSender(uint8_t id);
TomLonergan03 marked this conversation as resolved.
Show resolved Hide resolved

bool sendMessage(utils::io::can::Frame &message) override;

void registerController() override;

void processNewData(utils::io::can::Frame &message) override;

bool hasId(uint32_t id, bool extended) override;

bool getIsSending() override;
bool getIsSending();

private:
utils::Logger log_;
Expand Down
Loading