Skip to content
This repository has been archived by the owner on Feb 5, 2024. It is now read-only.

Xbees comms #161

Open
wants to merge 4 commits into
base: devel
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
98 changes: 98 additions & 0 deletions Autopilot/Inc/comms.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#ifndef XBEE_HPP
#define XBEE_HPP

#include "main.h"

#define START 0x7E
SnackkOverflowError marked this conversation as resolved.
Show resolved Hide resolved
#define TRANSMIT_TYPE 0x10
#define RECIEVE_TYPE 0x90
#define RESPONSE_TYPE 0x8B

#define GROUND_ADDY 0x0013A20041B16D1C
#define DRONE_ADDY 0x0013A20041B16ECD
#define SHORT_ADDY 0xFFFE
SnackkOverflowError marked this conversation as resolved.
Show resolved Hide resolved

#define BROADCAST_RADIUS 0x00
#define OPTIONS 0x00

// size
#define TRANSMIT_PAYLOAD_SIZE 22
#define RECIEVE_PAYLOAD_SIZE 20

#define TRANSMIT_SIZE 36
#define RECIEVE_SIZE 40


class pogiData {
public:
Copy link
Contributor

Choose a reason for hiding this comment

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

Could we fix the indentation here to make it more readable?

Copy link
Member

Choose a reason for hiding this comment

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

(Yes tab after both public: and private:)

uint8_t outputs[4] = { 0 };
uint8_t grabberPos = 0;
// 5
Copy link
Member

Choose a reason for hiding this comment

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

Is this needed? If so then make this comment clearer (eg. Byte offset: 5)

float pitch = 0.0;
float yaw = 0.0;
float roll = 0.0;
// 17
uint8_t statusDisplay = 0;
// 18
// use to send data as needed for debugging purposes only,
// for changes going into the final version of this struct, make a new field in the struct
uint8_t spareChannels[4] = { 0 };
// 22
void setArmState(bool arm); // 1 bit
void setControlState(uint8_t state); // 2 bits
void setPMState(uint8_t state); // 4 bits
Comment on lines +42 to +43
Copy link
Member

Choose a reason for hiding this comment

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

Is there a way to make it clearer the data format going into these functions?


bool getArmState();
uint8_t getControlState();
uint8_t getPMState();
};

struct transmitFrame {
uint8_t startDelim = START;
uint16_t length = TRANSMIT_SIZE;
uint8_t type = TRANSMIT_TYPE;
uint8_t id;

uint64_t address64 = GROUND_ADDY;
uint16_t address16 = SHORT_ADDY;
SnackkOverflowError marked this conversation as resolved.
Show resolved Hide resolved

uint8_t broadcastRadius = BROADCAST_RADIUS;
uint8_t options = OPTIONS;

pogiData payload;

uint8_t checksum;
};

struct responseFrame {
Copy link
Contributor

Choose a reason for hiding this comment

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

What is the purpose of the responseFrame?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

no purpose currently, but when you send an outgoing message, the xbee will send you a response telling you if it failed or succeeded. Im working on support for that in v2 which ill be done with soon.

uint8_t startDelim;
uint16_t length;
uint8_t type;
uint8_t id;

uint16_t address16;

uint8_t txRetryCount;
uint8_t deliveryStatus;
uint8_t discoveryStatus;

uint8_t checksum;
};

class Comms {
public:
SnackkOverflowError marked this conversation as resolved.
Show resolved Hide resolved
Comms(const Comms*) = delete;
static Comms* GetInstance();

void transmitMessage();

pogiData tx;
uint8_t rxBuffer[22];

private:
Comms();
static Comms* instance;

};
void TestComms(void);
#endif
16 changes: 16 additions & 0 deletions Autopilot/Src/Callbacks.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#include "Interchip_A.h"
SnackkOverflowError marked this conversation as resolved.
Show resolved Hide resolved
#include "imu.hpp"
#include "gps.hpp"
#include "comms.hpp"
#include "usart.h"
#include "spi.h"

void HAL_SPI_TxRxCpltCallback(SPI_HandleTypeDef *hspi)
Expand All @@ -9,4 +12,17 @@ void HAL_SPI_TxRxCpltCallback(SPI_HandleTypeDef *hspi)
} else {
InterchipTxRxCallback();
}
}

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) {
if(huart == &huart4) {
Copy link
Contributor

Choose a reason for hiding this comment

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

https://www.u-blox.com/en/product/neo-m8-series

^ Is this what the NEOM8 refers to? The XBee module name?

NEOM8 * neoM8N = NEOM8::GetInstance();

neoM8N->set_uart_data_is_new(true);

HAL_UART_Receive_DMA(&huart4, neoM8N->get_byte_collection_buffer(), GPS_UART_BUFFER_SIZE);
}
if(huart == &huart2) {
HAL_UART_Receive_DMA(&huart2, Comms::GetInstance()->rxBuffer, 40);
}
}
Copy link
Member

Choose a reason for hiding this comment

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

add a line at the end of the file to make github happy

136 changes: 136 additions & 0 deletions Autopilot/Src/comms.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
#include "comms.hpp"
#include "usart.h"

uint8_t pogiData::getPMState() {
return this->statusDisplay & 00001111;
}

uint8_t pogiData::getControlState() {
return (this->statusDisplay & 00110000) >> 4;
}

bool pogiData::getArmState() {
return (this->statusDisplay & 01000000) >> 6;
}

void pogiData::setArmState(bool arm) {
this->statusDisplay = (this->statusDisplay & 00111111) | (arm << 6);
}

void pogiData::setControlState(uint8_t state) {
this->statusDisplay = (this->statusDisplay & 01001111) | ((state & 00000011) << 4);
}

void pogiData::setPMState(uint8_t state) {
this->statusDisplay = (this->statusDisplay & 01110000) | (state & 00001111);
Copy link
Contributor

Choose a reason for hiding this comment

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

Why isn't this statusDisplay & 11110000? If you're really only setting the last four bits, as you do with state & 00001111?

}

Comms* Comms::instance = nullptr;

Comms::Comms() {
//constructor
Copy link
Member

Choose a reason for hiding this comment

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

Can remove this comment

}

Comms* Comms::GetInstance() {
if(!instance) {
instance = new Comms;
}
return instance;
}

uint8_t calculateChecksum(uint8_t* data, uint8_t start, uint8_t end) {
uint8_t checksum = 0;
for (uint8_t i = start; i < end; i++) {
checksum += data[i];
}
return checksum;
}

// this is a weird dumb no good way to do this, but I am tired and this works
// manually unwrapping the class because a cast seems to not work (internet says that classes have padding)
Copy link
Member

Choose a reason for hiding this comment

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

Often there will be an additional 8 bit address associated with pointers, but if this works, that can be looked at later

// will work on a v2 using only structs to get rid of those issues
void Comms::transmitMessage() {
transmitFrame frame;
frame.payload = this->tx;
frame.id = 1;
Copy link
Contributor

Choose a reason for hiding this comment

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

what does id signify?


uint8_t rawData[40] = { 0 };
uint8_t checksum = 0xFF;

rawData[0] = frame.startDelim;
Copy link
Contributor

Choose a reason for hiding this comment

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

are you basically going through your transmit frame, putting it into an array of uint8_t, and transmitting that instead of the whole struct?


uint8_t *castedVal = (uint8_t*)&frame.length;
Copy link
Member

Choose a reason for hiding this comment

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

castVal? Casted feels like bad grammar

rawData[1] = castedVal[1];
rawData[2] = castedVal[0];

rawData[3] = frame.type;
rawData[4] = frame.id;
checksum -= frame.type;
checksum -= frame.id;

uint8_t index = 5;
castedVal = (uint8_t*)&frame.address64;
for(int i = 7; i > -1; i--) {
rawData[index] = castedVal[i];
checksum -= castedVal[i];
index++;
}

castedVal = (uint8_t*)&frame.address16;
for (int i = 1; i > -1; i--) {
rawData[index] = castedVal[i];
checksum -= castedVal[i];
index++;
}

rawData[15] = frame.broadcastRadius;
checksum -= frame.broadcastRadius;
rawData[16] = frame.options;
checksum -= frame.broadcastRadius;

index = 17;
for( int i = 0; i < 4; i++) {
rawData[index] = frame.payload.outputs[i];
checksum -= frame.payload.outputs[i];
index++;
}

rawData[21] = frame.payload.grabberPos;
index = 22;
castedVal = (uint8_t*)&frame.payload.pitch;
for( int i = 3; i > -1; i--) {
rawData[index] = castedVal[i];
checksum -= castedVal[1];
index++;
}

castedVal = (uint8_t*)&frame.payload.yaw;
for( int i = 3; i > -1; i--) {
rawData[index] = castedVal[i];
checksum -= castedVal[1];
index++;
}

castedVal = (uint8_t*)&frame.payload.roll;
for( int i = 3; i > -1; i--) {
rawData[index] = castedVal[i];
checksum -= castedVal[1];
index++;
}

rawData[34] = frame.payload.statusDisplay;
checksum -= frame.payload.statusDisplay;

index = 35;
for( int i = 0; i < 4; i++) {
rawData[index] = castedVal[i];
checksum -= castedVal[1];
index++;
}

rawData[39] = checksum;
uint8_t size = sizeof(rawData);

// send the data
HAL_UART_Transmit(&huart1, rawData, size,100);
Copy link
Contributor

Choose a reason for hiding this comment

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

Have you considered doing what Yashraj did in the UART driver for FW-CV comms? He is able to reinterpret and transmit structs effectively. That might be a better solution

}
10 changes: 0 additions & 10 deletions Autopilot/Src/gps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,16 +102,6 @@ NEOM8::NEOM8() : gpsData {},
HAL_UART_Receive_DMA(&huart4, byte_collection_buffer, GPS_UART_BUFFER_SIZE);
}

/**
* Method is called when our uart buffer (byte_collection_buffer) is full
*/
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) {
NEOM8 * neoM8N = NEOM8::GetInstance();

neoM8N->set_uart_data_is_new(true);

HAL_UART_Receive_DMA(&huart4, neoM8N->get_byte_collection_buffer(), GPS_UART_BUFFER_SIZE);
}

uint8_t* NEOM8::get_byte_collection_buffer() {
return byte_collection_buffer;
Expand Down
34 changes: 30 additions & 4 deletions Autopilot/boardfiles/Autopilot_ZeropilotRev1.ioc
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ ADC3.Rank-0\#ChannelRegularConversion=1
ADC3.SamplingTime-0\#ChannelRegularConversion=ADC_SAMPLETIME_3CYCLES
Dma.Request0=UART4_RX
Dma.Request1=UART4_TX
Dma.RequestsNb=2
Dma.Request2=USART2_RX
Dma.Request3=USART2_TX
Dma.RequestsNb=4
Dma.UART4_RX.0.Direction=DMA_PERIPH_TO_MEMORY
Dma.UART4_RX.0.FIFOMode=DMA_FIFOMODE_DISABLE
Dma.UART4_RX.0.Instance=DMA1_Stream2
Expand All @@ -48,6 +50,26 @@ Dma.UART4_TX.1.PeriphDataAlignment=DMA_PDATAALIGN_BYTE
Dma.UART4_TX.1.PeriphInc=DMA_PINC_DISABLE
Dma.UART4_TX.1.Priority=DMA_PRIORITY_LOW
Dma.UART4_TX.1.RequestParameters=Instance,Direction,PeriphInc,MemInc,PeriphDataAlignment,MemDataAlignment,Mode,Priority,FIFOMode
Dma.USART2_RX.2.Direction=DMA_PERIPH_TO_MEMORY
Dma.USART2_RX.2.FIFOMode=DMA_FIFOMODE_DISABLE
Dma.USART2_RX.2.Instance=DMA1_Stream5
Dma.USART2_RX.2.MemDataAlignment=DMA_MDATAALIGN_BYTE
Dma.USART2_RX.2.MemInc=DMA_MINC_ENABLE
Dma.USART2_RX.2.Mode=DMA_NORMAL
Dma.USART2_RX.2.PeriphDataAlignment=DMA_PDATAALIGN_BYTE
Dma.USART2_RX.2.PeriphInc=DMA_PINC_DISABLE
Dma.USART2_RX.2.Priority=DMA_PRIORITY_LOW
Dma.USART2_RX.2.RequestParameters=Instance,Direction,PeriphInc,MemInc,PeriphDataAlignment,MemDataAlignment,Mode,Priority,FIFOMode
Dma.USART2_TX.3.Direction=DMA_MEMORY_TO_PERIPH
Dma.USART2_TX.3.FIFOMode=DMA_FIFOMODE_DISABLE
Dma.USART2_TX.3.Instance=DMA1_Stream6
Dma.USART2_TX.3.MemDataAlignment=DMA_MDATAALIGN_BYTE
Dma.USART2_TX.3.MemInc=DMA_MINC_ENABLE
Dma.USART2_TX.3.Mode=DMA_NORMAL
Dma.USART2_TX.3.PeriphDataAlignment=DMA_PDATAALIGN_BYTE
Dma.USART2_TX.3.PeriphInc=DMA_PINC_DISABLE
Dma.USART2_TX.3.Priority=DMA_PRIORITY_LOW
Dma.USART2_TX.3.RequestParameters=Instance,Direction,PeriphInc,MemInc,PeriphDataAlignment,MemDataAlignment,Mode,Priority,FIFOMode
FREERTOS.FootprintOK=true
FREERTOS.INCLUDE_vTaskDelayUntil=1
FREERTOS.IPParameters=Tasks01,FootprintOK,INCLUDE_vTaskDelayUntil
Expand Down Expand Up @@ -165,6 +187,8 @@ MxDb.Version=DB.6.0.10
NVIC.BusFault_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false
NVIC.DMA1_Stream2_IRQn=true\:5\:0\:false\:false\:true\:true\:false\:true
NVIC.DMA1_Stream4_IRQn=true\:5\:0\:false\:false\:true\:true\:false\:true
NVIC.DMA1_Stream5_IRQn=true\:5\:0\:false\:false\:true\:true\:false\:true
NVIC.DMA1_Stream6_IRQn=true\:5\:0\:false\:false\:true\:true\:false\:true
NVIC.DebugMonitor_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false
NVIC.ForceEnableDMAVector=true
NVIC.HardFault_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false
Expand Down Expand Up @@ -380,7 +404,7 @@ ProjectManager.FreePins=false
ProjectManager.HalAssertFull=false
ProjectManager.HeapSize=0x200
ProjectManager.KeepUserCode=true
ProjectManager.LastFirmware=false
ProjectManager.LastFirmware=true
ProjectManager.LibraryCopy=1
ProjectManager.MainLocation=Src
ProjectManager.NoMain=false
Expand All @@ -393,7 +417,7 @@ ProjectManager.StackSize=0x400
ProjectManager.TargetToolchain=Makefile
ProjectManager.ToolChainLocation=
ProjectManager.UnderRoot=false
ProjectManager.functionlistsort=1-MX_GPIO_Init-GPIO-false-HAL-true,2-MX_DMA_Init-DMA-false-HAL-true,3-SystemClock_Config-RCC-false-HAL-true,4-MX_SPI5_Init-SPI5-false-HAL-true,5-MX_SPI4_Init-SPI4-false-HAL-true,6-MX_I2C1_Init-I2C1-false-HAL-true,7-MX_I2C4_Init-I2C4-false-HAL-true,8-MX_USART2_UART_Init-USART2-false-HAL-true,9-MX_SPI1_Init-SPI1-false-HAL-true,10-MX_ADC3_Init-ADC3-false-HAL-true,11-MX_ADC2_Init-ADC2-false-HAL-true,12-MX_UART4_Init-UART4-false-HAL-true,13-MX_USART3_UART_Init-USART3-false-HAL-true,14-MX_USART1_UART_Init-USART1-false-HAL-true,15-MX_SPI2_Init-SPI2-false-HAL-true,16-MX_TIM10_Init-TIM10-false-HAL-true,17-MX_TIM11_Init-TIM11-false-HAL-true,18-MX_CRC_Init-CRC-false-HAL-true,19-MX_I2C2_Init-I2C2-false-HAL-true,20-MX_SPI6_Init-SPI6-false-HAL-true,0-MX_CORTEX_M7_Init-CORTEX_M7-false-HAL-true
ProjectManager.functionlistsort=1-MX_GPIO_Init-GPIO-false-HAL-true,2-MX_DMA_Init-DMA-false-HAL-true,3-SystemClock_Config-RCC-false-HAL-true,4-MX_SPI5_Init-SPI5-false-HAL-true,5-MX_SPI4_Init-SPI4-false-HAL-true,6-MX_I2C1_Init-I2C1-false-HAL-true,7-MX_I2C4_Init-I2C4-false-HAL-true,8-MX_USART2_UART_Init-USART2-false-HAL-true,9-MX_SPI1_Init-SPI1-false-HAL-true,10-MX_ADC3_Init-ADC3-false-HAL-true,11-MX_ADC2_Init-ADC2-false-HAL-true,12-MX_UART4_Init-UART4-false-HAL-true,13-MX_USART3_UART_Init-USART3-false-HAL-true,14-MX_USART1_UART_Init-USART1-false-HAL-true,15-MX_SPI2_Init-SPI2-false-HAL-true,16-MX_TIM10_Init-TIM10-false-HAL-true,17-MX_TIM11_Init-TIM11-false-HAL-true,18-MX_CRC_Init-CRC-false-HAL-true,19-MX_I2C2_Init-I2C2-false-HAL-true,20-MX_SPI6_Init-SPI6-false-HAL-true,21-MX_TIM14_Init-TIM14-false-HAL-true,0-MX_CORTEX_M7_Init-CORTEX_M7-false-HAL-true
RCC.AHBFreq_Value=216000000
RCC.APB1CLKDivider=RCC_HCLK_DIV4
RCC.APB1Freq_Value=54000000
Expand Down Expand Up @@ -526,7 +550,9 @@ UART4.WordLength=WORDLENGTH_8B
USART1.IPParameters=VirtualMode-Asynchronous,WordLength
USART1.VirtualMode-Asynchronous=VM_ASYNC
USART1.WordLength=WORDLENGTH_8B
USART2.IPParameters=VirtualMode-Asynchronous,WordLength
USART2.BaudRate=9600
USART2.IPParameters=VirtualMode-Asynchronous,WordLength,BaudRate,OverSampling
USART2.OverSampling=UART_OVERSAMPLING_16
USART2.VirtualMode-Asynchronous=VM_ASYNC
USART2.WordLength=WORDLENGTH_8B
USART3.IPParameters=VirtualMode-Asynchronous,WordLength
Expand Down
2 changes: 2 additions & 0 deletions Autopilot/boardfiles/Inc/stm32f7xx_it.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ void UsageFault_Handler(void);
void DebugMon_Handler(void);
void DMA1_Stream2_IRQHandler(void);
void DMA1_Stream4_IRQHandler(void);
void DMA1_Stream5_IRQHandler(void);
void DMA1_Stream6_IRQHandler(void);
void TIM1_UP_TIM10_IRQHandler(void);
void TIM4_IRQHandler(void);
void SPI1_IRQHandler(void);
Expand Down
8 changes: 7 additions & 1 deletion Autopilot/boardfiles/Src/dma.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
******************************************************************************
* @attention
*
* <h2><center>&copy; Copyright (c) 2021 STMicroelectronics.
* <h2><center>&copy; Copyright (c) 2022 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under Ultimate Liberty license
Expand Down Expand Up @@ -48,6 +48,12 @@ void MX_DMA_Init(void)
/* DMA1_Stream4_IRQn interrupt configuration */
HAL_NVIC_SetPriority(DMA1_Stream4_IRQn, 5, 0);
HAL_NVIC_EnableIRQ(DMA1_Stream4_IRQn);
/* DMA1_Stream5_IRQn interrupt configuration */
HAL_NVIC_SetPriority(DMA1_Stream5_IRQn, 5, 0);
HAL_NVIC_EnableIRQ(DMA1_Stream5_IRQn);
/* DMA1_Stream6_IRQn interrupt configuration */
HAL_NVIC_SetPriority(DMA1_Stream6_IRQn, 5, 0);
HAL_NVIC_EnableIRQ(DMA1_Stream6_IRQn);

}

Expand Down
2 changes: 1 addition & 1 deletion Autopilot/boardfiles/Src/spi.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
******************************************************************************
* @attention
*
* <h2><center>&copy; Copyright (c) 2021 STMicroelectronics.
* <h2><center>&copy; Copyright (c) 2022 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under Ultimate Liberty license
Expand Down
Loading