Skip to content

Commit

Permalink
Merge pull request #25 from RoboTeamTwente/feature/channel_changing
Browse files Browse the repository at this point in the history
Implemented channel selection. no jumper = yellow
  • Loading branch information
emielsteerneman authored Jan 16, 2022
2 parents ae86000 + 7134601 commit f614af2
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
18 changes: 15 additions & 3 deletions Core/Inc/Wireless/Wireless.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,29 @@
#define MAX_BUF_LENGTH 128
#define AUTO_TX_TIME 120 // (us)

#define WIRELESS_FEEDBACK_CHANNEL -5 // 2.395 GHz
#define WIRELESS_COMMAND_CHANNEL -15 // 2.385 GHz
#define WIRELESS_YELLOW_CHANNELS 0
#define WIRELESS_BLUE_CHANNELS 1

#define WIRELESS_YELLOW_FEEDBACK_CHANNEL -5 // 2.395 GHz
#define WIRELESS_YELLOW_COMMAND_CHANNEL -15 // 2.385 GHz
#define WIRELESS_BLUE_FEEDBACK_CHANNEL -25
#define WIRELESS_BLUE_COMMAND_CHANNEL -35

typedef enum WIRELESS_CHANNEL {
YELLOW_CHANNEL = 0,
BLUE_CHANNEL = 1
} WIRELESS_CHANNEL;
///////////////////////////////////////////////////// PUBLIC FUNCTION DECLARATIONS

SX1280 * Wireless_Init(float channel, SPI_HandleTypeDef * WirelessSpi);
SX1280 * Wireless_Init(WIRELESS_CHANNEL channel, SPI_HandleTypeDef * WirelessSpi);
void Wireless_DeInit();
void SendPacket(SX1280* SX, uint8_t * data, uint8_t Nbytes);
void ReceivePacket(SX1280* SX);
void Wireless_IRQ_Handler(SX1280* SX, uint8_t * data, uint8_t Nbytes);
void Wireless_DMA_Handler(SX1280* SX, uint8_t output[]);
bool checkWirelessConnection();

void SX1280_updateChannel(WIRELESS_CHANNEL channel);
WIRELESS_CHANNEL SX1280_getCurrentChannel();

#endif /* WIRELESS_WIRELESS_H_ */
15 changes: 11 additions & 4 deletions Core/Src/Wireless/Wireless.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ static bool isWirelessTransmitting = false; // boolean to check whether we are t
uint8_t TX_buffer[MAX_BUF_LENGTH] __attribute__((aligned(16)));
uint8_t RX_buffer[MAX_BUF_LENGTH] __attribute__((aligned(16)));

volatile static WIRELESS_CHANNEL current_channel = YELLOW_CHANNEL;

SX1280 SX1280_struct;
SX1280* SX; // pointer to the datastruct

Expand Down Expand Up @@ -46,9 +48,11 @@ SX1280_Settings set = {
};
SX1280_Packet_Status PacketStat;

SX1280 * Wireless_Init(float channel, SPI_HandleTypeDef * WirelessSpi){
SX1280 * Wireless_Init(WIRELESS_CHANNEL channel, SPI_HandleTypeDef * WirelessSpi){
SX1280 * SX = &SX1280_struct;// pointer to the global struct

current_channel = channel;

SX->SPI_used = false;

SX->payloadLength = 0;
Expand All @@ -69,7 +73,8 @@ SX1280 * Wireless_Init(float channel, SPI_HandleTypeDef * WirelessSpi){

// link settings
SX->SX_settings = &set;
SX->SX_settings->channel = channel;
if (current_channel == YELLOW_CHANNEL) SX->SX_settings->channel = WIRELESS_YELLOW_COMMAND_CHANNEL;
if (current_channel == BLUE_CHANNEL ) SX->SX_settings->channel = WIRELESS_BLUE_COMMAND_CHANNEL;
SX->Packet_status = &PacketStat;

SX1280Setup(SX); // SX1280 init procedure
Expand Down Expand Up @@ -127,7 +132,8 @@ void Wireless_IRQ_Handler(SX1280* SX, uint8_t * data, uint8_t Nbytes){
isWirelessTransmitting = false;
//toggle_Pin(LED5_pin);
// start listening for a packet again
setChannel(SX, WIRELESS_COMMAND_CHANNEL); // set to channel 40 for basestation to robot
if (current_channel == YELLOW_CHANNEL) setChannel(SX, WIRELESS_YELLOW_COMMAND_CHANNEL);
if (current_channel == BLUE_CHANNEL ) setChannel(SX, WIRELESS_BLUE_COMMAND_CHANNEL);
SX->SX_settings->syncWords[0] = robot_syncWord[get_Id()];
setSyncWords(SX, SX->SX_settings->syncWords[0], 0x00, 0x00);

Expand All @@ -149,7 +155,8 @@ void Wireless_IRQ_Handler(SX1280* SX, uint8_t * data, uint8_t Nbytes){
if (wirelessFeedback && !isWirelessTransmitting) {
// feedback enabled, transmit a packet to basestation
isWirelessTransmitting = true;
setChannel(SX, WIRELESS_FEEDBACK_CHANNEL); // set to channel 40 for feedback to basestation
if (current_channel == YELLOW_CHANNEL) setChannel(SX, WIRELESS_YELLOW_FEEDBACK_CHANNEL);
if (current_channel == BLUE_CHANNEL ) setChannel(SX, WIRELESS_BLUE_FEEDBACK_CHANNEL);
SX->SX_settings->syncWords[0] = robot_syncWord[16]; // 0x82108610 for basestation Rx
setSyncWords(SX, SX->SX_settings->syncWords[0], 0x00, 0x00);
SendPacket(SX, data, Nbytes);
Expand Down
10 changes: 9 additions & 1 deletion Core/Src/robot.c
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,15 @@ void init(void){
/* Initialize the SX1280 wireless chip */
// TODO figure out why a hardfault occurs when this is disabled
Putty_printf("Initializing wireless\n");
SX = Wireless_Init(WIRELESS_COMMAND_CHANNEL, COMM_SPI);

if(read_Pin(IN2_pin)){
SX = Wireless_Init(BLUE_CHANNEL, COMM_SPI);
Putty_printf("Listening on BLUE CHANNEL\n");
}else{
SX = Wireless_Init(YELLOW_CHANNEL, COMM_SPI);
Putty_printf("Listening on YELLOW CHANNEL\n");
}

SX->SX_settings->syncWords[0] = robot_syncWord[ID];
setSyncWords(SX, SX->SX_settings->syncWords[0], 0x00, 0x00);
setRX(SX, SX->SX_settings->periodBase, WIRELESS_RX_COUNT);
Expand Down

0 comments on commit f614af2

Please sign in to comment.