Skip to content
Snippets Groups Projects
Commit 411e2f6a authored by Niels Post's avatar Niels Post
Browse files

- Fixed some connectivity issues with the NRF (likely because of low quality chips)

- Added a distinction between unicast and broadcast. Broadcasts are received and sent without auto acknowledge and on the address ending in FF
parent 20a8ef70
No related branches found
No related tags found
1 merge request!1Development
Pipeline #36 failed
......@@ -18,8 +18,6 @@ struct NRFConfiguration {
bool enable_dynamic_payload_length;
/// Static payload length (currently unneeded since static payload is unsupported)
uint8_t payload_size;
/// Have the NRF module automatically acknowledge incoming messages (needs to match the controller's setting)
bool enable_autoack;
/// CE pin the NRF is connected to
uint8_t pin_ce;
/// CSN pin the NRF is connected to
......
......@@ -10,11 +10,11 @@
*/
enum class SuccessCode : uint8_t {
/// The requested action was performed successfully
SUCCESS = 0,
SUCCESS = 0,
/// Special case: Action started is used when the robot cannot immediately send a response. The robot does not send this code
ACTION_STARTED = 1,
/// The robot cannot handle the command because it was busy with another action
ROBOT_BUSY = 2,
ROBOT_BUSY = 2,
/// Performing the requested action was impossible due to a hardware error
HARDWARE_ERROR = 3,
/// A reading was requested for a sensor that doesn't exist
......@@ -28,7 +28,9 @@ enum class SuccessCode : uint8_t {
/// The command requested is unknown
UNKNOWN_COMMAND = 8,
/// Do not send a response for this command
NO_RESPONSE = 9
NO_RESPONSE = 9,
/// Sent when a robot is already registered to a robot id
ID_ALREADY_SET = 10
};
......@@ -37,29 +39,29 @@ enum class SuccessCode : uint8_t {
*/
struct Response {
/// The message id of the original Command. This can be used to match a response to a sent command on the other side
uint8_t message_id;
uint8_t message_id;
/// ReturnCode for the message
SuccessCode successCode;
/// (Optional) the data returned from the Command
uint8_t data[4];
uint8_t data[4];
/// Size of the data returned
uint8_t data_size;
uint8_t data_size;
/// Should the message be broadcast or unicast?
bool broadcast = false;
/**
* Create a Response object
*/
Response(uint8_t messageId, SuccessCode returnCode, uint8_t data_length = 0, const uint8_t *dat = nullptr);
Response(uint8_t messageId, SuccessCode returnCode, uint8_t data_length = 0, const uint8_t *dat = nullptr, bool _broadcast = false);
/**
* Create a Response object
*/
Response(SuccessCode returnCode, uint8_t data_length = 0, const uint8_t *dat = nullptr);
Response(SuccessCode returnCode, uint8_t data_length = 0, const uint8_t *dat = nullptr, bool _broadcast = false);
/**
* Get the byte size of this command
* @return Total size of the command in bytes
*/
uint8_t size() const {
return 2 + data_size;
}
uint8_t size() const;
};
......@@ -10,11 +10,14 @@ Response CommandController_General::handle(const Command &cmd, ResponseCallback
break;
case SET_ID:
if(pmsvSettings.robot_id != 0) {
if(pmsvSettings.robot_id == cmd.parameters[0]) {
return {cmd.message_id, SuccessCode::ID_ALREADY_SET, 0, nullptr, true};
}
return {cmd.message_id, SuccessCode::NO_RESPONSE};
}
pmsvSettings.robot_id = cmd.parameters[0];
configRobotId.store();
Response res{cmd.message_id, SuccessCode::SUCCESS};
Response res{cmd.message_id, SuccessCode::SUCCESS, 0, nullptr, true};
callback(res);
rstc_start_software_reset(RSTC);
}
......
......@@ -5,7 +5,14 @@ NRF24CommunicationBoundary::NRF24CommunicationBoundary(NRFConfiguration &_config
: config(_config),
settings(settings), nrfRadio(config.pin_ce, config.pin_csn) {
nrfRadio.begin();
for (int i = 0; i < 5; i++) {
nrfRadio.closeReadingPipe(i);
}
nrfRadio.setPALevel(config.palevel);
nrfRadio.setDataRate(RF24_250KBPS);
if (config.enable_dynamic_payload_length) {
......@@ -15,27 +22,47 @@ NRF24CommunicationBoundary::NRF24CommunicationBoundary(NRFConfiguration &_config
nrfRadio.setPayloadSize(config.payload_size);
}
nrfRadio.setAutoAck(config.enable_autoack);
nrfRadio.setRetries(15, 15);
nrfRadio.setChannel(config.channel);
const uint64_t base_address_mask = 0x000000FFu;
nrfRadio.openReadingPipe(1, config.base_reading_pipe & ~base_address_mask);
nrfRadio.openReadingPipe(2, (config.base_reading_pipe & ~base_address_mask) | settings.robot_id);
nrfRadio.openWritingPipe((config.base_writing_pipe & ~base_address_mask) | settings.robot_id);
if (settings.robot_id != 0) {
nrfRadio.openReadingPipe(0, config.base_reading_pipe);
nrfRadio.openReadingPipe(1, (config.base_reading_pipe & ~base_address_mask) | settings.robot_id);
nrfRadio.setAutoAck(true);
nrfRadio.setAutoAck(0, false);
} else {
nrfRadio.openReadingPipe(0, config.base_reading_pipe);
nrfRadio.setAutoAck(false);
}
nrfRadio.powerUp();
nrfRadio.startListening();
nrfRadio.printDetails();
nrfRadio.startListening();
}
bool NRF24CommunicationBoundary::sendMessage(const Response &message) {
nrfRadio.stopListening();
const uint64_t base_address_mask = 0x000000FFu;
if(message.broadcast) {
nrfRadio.openWritingPipe(config.base_writing_pipe);
} else {
nrfRadio.openWritingPipe((config.base_reading_pipe & ~base_address_mask) | settings.robot_id);
nrfRadio.setAutoAck(0, true);
}
bool sendStatus = nrfRadio.write(&message, message.size());
if(!message.broadcast) {
nrfRadio.setAutoAck(0, false);
}
nrfRadio.startListening();
Serial.println(sendStatus);
return sendStatus;
}
......
#include <core/communication/Response.hpp>
#include <cstddef>
Response::Response(uint8_t messageId, SuccessCode returnCode, uint8_t data_length, const uint8_t *dat)
Response::Response(uint8_t messageId, SuccessCode returnCode, uint8_t data_length, const uint8_t *dat, bool _broadcast)
: message_id(messageId),
successCode(returnCode) {
successCode(returnCode), broadcast(_broadcast) {
for (size_t i = 0; i < 4; i++) {
data[i] = dat[i];
}
}
Response::Response(SuccessCode returnCode, uint8_t data_length, const uint8_t *dat): Response(0, returnCode, data_length, dat) {
Response::Response(SuccessCode returnCode, uint8_t data_length, const uint8_t *dat, bool _broadcast): Response(0, returnCode, data_length, dat, _broadcast) {
}
uint8_t Response::size() const {
return 2 + data_size;
}
......@@ -21,7 +21,7 @@
////################################################################## Configuration ###################################
PMSVSettings default_settings{
0,
1.434684f,
1.9f,
1.4545f,
0,
180
......@@ -38,19 +38,17 @@ auto c_max_speed = registerConfig("MAX_SPEED", default_settings.max_speed);
NRFConfiguration nrf_config{
RF24_PA_MAX,
50,
0xE0E0F1F1E4LL,
0xE0E0F1F1E4LL,
0x10,
0xE0E0F1F1FFLL,
0xE0E0F1F1FFLL,
true,
32,
true,
5,
4
};
auto c_nrf_channel = registerConfig("NRF_CHANNEL", nrf_config.channel);
auto c_nrf_dynamicpayload = registerConfig("NRF_ENABLE_DYNAMICPAYLOAD", nrf_config.enable_dynamic_payload_length);
auto c_nrf_autoack = registerConfig("NRF_ENABLE_AUTOACK", nrf_config.enable_autoack);
auto c_nrf_pin_ce = registerConfig("NRF_PIN_CE", nrf_config.pin_ce);
auto c_nrf_pin_csn = registerConfig("NRF_PIN_CSN", nrf_config.pin_csn);
......@@ -112,7 +110,6 @@ std::reference_wrapper<BaseConfigurationValue> configurationValues[]{
c_max_speed,
c_nrf_channel,
c_nrf_dynamicpayload,
c_nrf_autoack,
c_nrf_pin_ce,
c_nrf_pin_csn,
c_leftmotor_pin_ms1,
......@@ -182,5 +179,5 @@ void setup() {
void loop() {
}
//
//
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment