After reviewing your code I concluded you don’t need to use interrupts at all. My advice is as follows:
- Use
BufferedSerial
for serial communication rather than a custom class. It provides a non-blocking feature to read the recieved data:
...
BufferedSerial mainSerial(PA_0, PA_1);
BufferedSerial gyroSerial(PC_12, PD_2);
...
//------------------------
void classSetting()
{
mainSerial.set_baud(115200);
gyroSerial.set_baud(115200);
}
...
//------------------------
void input()
{
while (mainSerial.readable()) {
readMainSerial();
}
while (gyroSerial.readable()) {
readGyroSerial();
}
...
//------------------------
void readMainSerial()
{
...
char data;
mainSerial.read(&data, 1);
...
// replace `result = mainSerial.store_data(data, receive_data, HeadNum, foot_num, data_num - 1);`
// with something suitable.
...
//------------------------
void readGyroSerial()
{
char data1;
gyroSerial.read(&data1, 1);
...
// replace `res = gyroSerial.store_data(data1, receive_data_2, HeadNum, foot_num, data_num_gyro - 1);`
// with something suitable.
...
- Use
EventQueue
and its method call_every
to periodically call a function rather than a Ticker
.
...
Thread thread;
EventQueue eventQueue;
...
int main()
{
thread.start(callback(&eventQueue, &EventQueue::dispatch_forever));
eventQueue.call_every(50ms, control);
...
- To display debug
printf
messages on a serial terminal connect the NUCLEO-F446RE_2 board over a USB cable to a PC.
The modified code is below:
#include "mbed.h"
#include "MotorControler.h"
#include "QEI_speed.h"
#include "QEI_angle.h"
#include "calculation.h"
#include "defines.h"
#include "BusSerial_new.h"
#include <stdint.h>
/***************
*
* *
* *************/
BufferedSerial mainSerial(PA_0, PA_1);
BufferedSerial gyroSerial(PC_12, PD_2);
Thread thread;
EventQueue eventQueue;
/***************
*
* *
* *************/
void classSetting();
void init();
void ang_encoder_reset();
void input();
void calc_auto();
void calc_manual();
void calc_wheel_params(double coordinate);
void output();
void debug();
void control();
void readMainSerial();
void readGyroSerial();
void time_out();
void ang_encoder_reset();
const int data_num = 12;
const int data_num_gyro = 8;
using comm_data = int;
comm_data ReceiveData[data_num];
comm_data ReceiveData_2[data_num_gyro];
uint8_t receive_data[data_num];
uint8_t receive_data_2[data_num_gyro];
const int NUM_TX_DATA = 7;
#define MAIN_ID 252
//uint8_t send_data[NUM_TX_DATA];
uint8_t debug_box = 0;
bool zero_complete = false;
static bool RX_COMP_F = false;
static bool RX_COMP_F_2 = false;
/****************
*
*
*
* **************/
int main()
{
thread.start(callback(&eventQueue, &EventQueue::dispatch_forever));
eventQueue.call_every(50ms, control);
init();
while (1) {
input();
if (ZERO_ADJ && !zero_complete) {
printf("adj\n");
ang_encoder_reset();
}
else
zero_complete = false;
if (TIMER_FIRES) {
TIMER_FIRES = false;
if (OPERATE_MODE == AUTO) {
calc_auto();
}
else {
RGB[0] = 1;
calc_manual();
RGB[0] = 0;
}
}
output();
debug();
}
}
/*---------------------------------------------- -------------------------------------------------*/
void init()
{
printf("boot\n");
classSetting();
ang_encoder_reset();
}
/*-----------------------
*
* ---------------------*/
void input()
{
while (mainSerial.readable()) {
readMainSerial();
}
while (gyroSerial.readable()) {
readGyroSerial();
}
/* */
if (RX_COMP_F) {
for (int i = 0; i < data_num; i++) {
ReceiveData[i].all = receive_data[i];
printf("%d ", ReceiveData[i].all);
}
printf("\n");
RX_COMP_F = false;
}
/* */
if (RX_COMP_F_2) {
for (int i = 0; i < data_num_gyro; i++) {
ReceiveData_2[i].all = receive_data_2[i];
printf("%d ", receive_data_2[i]);
}
RX_COMP_F_2 = false;
}
}
/**
*/
void calc_auto()
{ }
/**
*/
void calc_manual()
{ }
/**
*/
void calc_wheel_params(double tar_vel_xy, double tar_robo_ang_vel)
{ }
/**
*/
void output()
{ }
/**
*/
void debug()
{
printf("decel_ratio = %d\r\n", ANG_ENC[0].getDegree());
printf("CURR_POS_X_UPPER = %d\r\n", CURR_POS_X_UPPER);
printf("CURR_POS_X_LOWER = %d\r\n", CURR_POS_X_LOWER);
printf("CURR_POS_Y_UPPER = %d\r\n", CURR_POS_Y_UPPER);
printf("CURR_POS_Y_LOWER = %d\r\n", CURR_POS_Y_LOWER);
}
/**
*/
void classSetting()
{
mainSerial.set_baud(115200);
gyroSerial.set_baud(115200);
}
/**-------------------------
------------------------*/
void control(void)
{
TIMER_FIRES = true;
RGB[0] = !RGB[0]; /* */
}
/* */
void readMainSerial()
{
rx_cnt_main = 0;
bool result = false;
char data;
mainSerial.read(&data, 1);
if (!RX_COMP_F) {
result = mainSerial.store_data(data, receive_data, HeadNum, foot_num, data_num - 1);
if (result == true) {
RGB[1] = !RGB[1];
RX_COMP_F = true;
}
}
}
/**
*/
void readGyroSerial()
{
rx_cnt_gyro = 0;
bool res = false;
RGB[2] = !RGB[2];
char data1;
gyroSerial.read(&data1, 1);
if (!RX_COMP_F_2) {
res = gyroSerial.store_data(data1, receive_data_2, HeadNum, foot_num, data_num_gyro - 1);
if (res == true) {
/* */
RX_COMP_F_2 = true;
RGB[2] = !RGB[2];
}
}
}
/**
*/
void ang_encoder_reset()
{ }