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

[Multi Encoder Access][WIP] implement a sample to access multiple encoders via I2C MultiPlexer #659

Open
wants to merge 3 commits into
base: master
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
5 changes: 0 additions & 5 deletions aerial_robot_nerve/neuron/neuron_f4/STM32CubeIDE/.project
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,6 @@
<type>1</type>
<locationURI>$%7BPARENT-1-PROJECT_LOC%7D/Src/main.cpp</locationURI>
</link>
<link>
<name>Application/User/main.cpp</name>
<type>1</type>
<locationURI>$%7BPARENT-1-PROJECT_LOC%7D/Src/main.cpp</locationURI>
</link>
<link>
<name>Application/User/spi.c</name>
<type>1</type>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,6 @@
<type>1</type>
<locationURI>PARENT-1-PROJECT_LOC/Src/lwip.c</locationURI>
</link>
<link>
<name>Application/User/main.c</name>
<type>1</type>
<locationURI>PARENT-1-PROJECT_LOC/Src/main.c</locationURI>
</link>
<link>
<name>Application/User/main.cpp</name>
<type>1</type>
Expand Down
37 changes: 35 additions & 2 deletions aerial_robot_nerve/spinal/mcu_project/boards/stm32H7/Src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include "sensors/baro/baro_ms5611.h"
#include "sensors/gps/gps_ublox.h"
#include "sensors/encoder/mag_encoder.h"
#include "sensors/i2c_multiplexer/switcher.h"

#include "battery_status/battery_status.h"

Expand Down Expand Up @@ -101,6 +102,9 @@ osSemaphoreId uartTxSemHandle;
/* USER CODE BEGIN PV */
osMailQId canMsgMailHandle;

osThreadId multiEncoderHandle;
std::map<int, MagEncoder> encoders_;

ros::NodeHandle nh_;

/* sensor instances */
Expand Down Expand Up @@ -146,7 +150,7 @@ void servoTaskCallback(void const * argument);
void coreTaskEvokeCb(void const * argument);

/* USER CODE BEGIN PFP */

void encoderTaskCallback(void const * argument);
/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
Expand Down Expand Up @@ -261,7 +265,15 @@ int main(void)
Spine::init(&hfdcan1, &nh_, &estimator_, LED1_GPIO_Port, LED1_Pin);
Spine::useRTOS(&canMsgMailHandle); // use RTOS for CAN in spianl
#endif


I2C_MultiPlexer::init(&hi2c3);
// two encoders
const char* topic1 = "encoder_angle1";
encoders_[0] = MagEncoder(topic1);
const char* topic2 = "encoder_angle2";
encoders_[1] = MagEncoder(topic2);
encoders_.at(0).init(&hi2c3, &nh_);
encoders_.at(1).init(&hi2c3, &nh_);
/* USER CODE END 2 */

/* Create the mutex(es) */
Expand Down Expand Up @@ -340,6 +352,11 @@ int main(void)

/* USER CODE BEGIN RTOS_THREADS */
/* add threads, ... */

/* definition and creation of task to read from multiple encoders */
osThreadDef(multiEncoder, encoderTaskCallback, osPriorityLow, 0, 256);
multiEncoderHandle = osThreadCreate(osThread(multiEncoder), NULL);

/* USER CODE END RTOS_THREADS */

/* Start scheduler */
Expand Down Expand Up @@ -1036,6 +1053,22 @@ static void MX_GPIO_Init(void)
}

/* USER CODE BEGIN 4 */
void encoderTaskCallback(void const * argument)
{
/* USER CODE BEGIN rosPublishTask */
for(;;)
{
for(std::map<int, MagEncoder>::iterator it = encoders_.begin(); it != encoders_.begin(); it++) {
uint8_t ch = it->first;
int i2c_status = I2C_MultiPlexer::changeChannel(ch);
if(i2c_status == HAL_OK) it->second.update();
}

osDelay(1); // timer is controlled inside each `update` function

}
/* USER CODE END rosPublishTask */
}

/* USER CODE END 4 */

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,6 @@
<type>1</type>
<locationURI>PARENT-1-PROJECT_LOC/Src/lwip.c</locationURI>
</link>
<link>
<name>Application/User/main.c</name>
<type>1</type>
<locationURI>PARENT-1-PROJECT_LOC/Src/main.c</locationURI>
</link>
<link>
<name>Application/User/main.cpp</name>
<type>1</type>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,18 @@
#endif

#include "sensors/encoder/mag_encoder.h"
#include <string>

MagEncoder::MagEncoder():
angle_pub_("encoder_angle", &angle_msg_)
{
}

MagEncoder::MagEncoder(const char * topic_name):
angle_pub_(topic_name, &angle_msg_)
{
}

void MagEncoder::init(I2C_HandleTypeDef* hi2c, ros::NodeHandle* nh)
{
hi2c_ = hi2c;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class MagEncoder
{
public:
MagEncoder();
MagEncoder(const char * topic_name);
~MagEncoder(){};

static const uint8_t AS5600_I2C_ADDRESS = 0x6c; // 0x36 << 1; NOTE: STM: i2c_address << 1 !!!
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
******************************************************************************
* File Name : switcher.cpp
* Description : I2C Multi Plexer Interface
******************************************************************************
*/

#include "switcher.h"

namespace I2C_MultiPlexer
{
namespace
{
I2C_HandleTypeDef* hi2c_;

uint8_t hub_address_ = 0x70; // the default I2C adress of PCA9547D. TODO: reconfigurable
};

void init(I2C_HandleTypeDef* hi2c)
{
hi2c_ = hi2c;
}

bool changeChannel(uint8_t ch)
{
uint8_t val[1];
val[0] = ch & 0x07; // assign the channel
val[0] = ch | 0x08; // enable switch the channel

int i2c_status = HAL_I2C_Master_Transmit(hi2c_, hub_address_, val, 1, 100);

if(i2c_status == HAL_OK) return true;
else return false;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
******************************************************************************
* File Name : switcher.h
* Description : I2C Multi Plexer Interface
******************************************************************************
*/

#ifndef __cplusplus
#error "Please define __cplusplus, because this is a c++ based file "
#endif

#ifndef __I2C_MULTIPLEXER_H
#define __I2C_MULTIPLEXER_H

#include "config.h"

namespace I2C_MultiPlexer
{
void init(I2C_HandleTypeDef* hi2c);
bool changeChannel(uint8_t ch);
};



#endif
Loading