serial communication (UART)
I am trying to receive information from ROS on MSP432(micro-controller) via UART.
The information from ROS side is as following. I would like to take the number between * and #.
For example, the first number(90) between * and # is my motor 1 value and the second number(-53.2708) between * and # is my motor 2 value. Again, the first number(90) between * and # is my motor 1 vale and keep going. The values can be changed in real time.
The code doesn't take the number as I expected.
Could someone help me to debug it?
!90#-53.2708#90#-53.2708#90#-53.2708#90#-53.2708#90#-53.2708#90#-53.2708#90#-53.2708#90#-53.2708#90#-53.2708#90#-53.2708#90#-53.2708#90#-53.2708#90#-53.2708#90#-53.2708#90#-53.2708#90#-53.2708#90#-53.2708#90#-53.2708#90#-53.2708#90#-53.2708#90#-53.2708#90#-53.2708#90#-53.2708#90#-53.2708#90#-53.2708#90#-53.2708#90#-53.2708#90#-53.2708#90#-53.2708#90#-53.2708#90#-53.2708#90#-53.2708#90#-53.2708#90#-53.2708#90#-53.2708# .........
#include "driverlib.h"
#include "msp432p401r.h"
#include "msp.h"
#include "driverlib.h"
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
char receivedBuffer1[200]; // All letters from Putty
char receivedBuffer2[200];
int f=50; int s=50;
//Set configure UART communication
// Configure UART for 115200 baud at SMCLK = 3 MHz
const eUSCI_UART_Config uartConfig =
{
EUSCI_A_UART_CLOCKSOURCE_SMCLK, // selectClockSource
1, // clockPrescalar
10, // firstModReg
0, // secondModReg
EUSCI_A_UART_NO_PARITY, // no parity
EUSCI_A_UART_LSB_FIRST, // LSB first
EUSCI_A_UART_ONE_STOP_BIT, // One stop bit
EUSCI_A_UART_MODE, // UART mode
EUSCI_A_UART_OVERSAMPLING_BAUDRATE_GENERATION // Oversampling
};
int main(void)
{
// Halt watchdog timer
WDT_A_holdTimer();
// Set pins 2 and 3 of port 1 to the primary module function (UART)
GPIO_setAsPeripheralModuleFunctionInputPin(GPIO_PORT_P1,GPIO_PIN2 | GPIO_PIN3, GPIO_PRIMARY_MODULE_FUNCTION);
// Set clock frequency
unsigned int dcoFrequency = 3E+6;
CS_setDCOFrequency(dcoFrequency);
CS_initClockSignal(CS_SMCLK, CS_DCOCLK_SELECT, CS_CLOCK_DIVIDER_1);
// Initialize UART
UART_initModule(EUSCI_A0_BASE, &uartConfig);
UART_enableModule(EUSCI_A0_BASE);
// Disable all interrupts
Interrupt_disableMaster();
// Clear interrupt flag(UCA0IFG)
UART_clearInterruptFlag(EUSCI_A0_BASE, EUSCI_A_UART_RECEIVE_INTERRUPT);
// Arm interrupts when RXIFG is set
UART_enableInterrupt(EUSCI_A0_BASE, EUSCI_A_UART_RECEIVE_INTERRUPT);
// Set interrupt Priority
Interrupt_setPriority(INT_EUSCIA0, 0);
// Enable interrupt
Interrupt_enableInterrupt(INT_EUSCIA0);
// Enable NVIC
Interrupt_enableMaster();
// Main program loop
while(1)
{
PCM_gotoLPM0InterruptSafe();
}
}
volatile int a = 0;
int b;
float reference1;
float reference2;
// Interrupt Service Routine
void EUSCIA0_IRQHandler(void)
{
uint8_t letters; // storing letters in UCA0RXBUF
letters = UART_receiveData(EUSCI_A0_BASE);
if(letters == '!'){
s=1; }
if(letters == '*' && s==1 && f!=2 && f!=0 && f!=3){
f=0;
printf("f = %d\r\n", f); }
else if(f==0 && letters != '#' && s==1){
receivedBuffer1[a] = letters;
a++; }
else if(f==0 && letters == '#' && s==1){
f=1;
printf("f = %d\r\n", f);}
else if(f==2 && letters == '*' && s==1 && f!=5){
f=3;
printf("f = %d\r\n", f);}
else if(f==3 && letters != '#' && s==1 && f!=5){
receivedBuffer2[a] = letters;
a++; }
else if(f==3 && letters == '#' && s==1 && f!=5){
f=4;
printf("f = %d\r\n", f);}
if(f==1 && s==1){
reference1 = atof(receivedBuffer1);
for(b=0;b<=199;b++){
receivedBuffer1[b]= 0x00; } // Clearing out all the buffer with NULL}
a=0; f=2;
printf("f = %d\r\n", f); }
else if(f==4 && s==1){
reference2 = atof(receivedBuffer2);
for(b=0;b<=199;b++){
receivedBuffer2[b]= 0x00; } // Clearing out all the buffer with NULL}
a=0; f=5;
printf("f = %d\r\n", f); }
}
Asked by gariym on 2019-04-21 00:35:25 UTC
Comments
not what you were looking for I guess, but this is a ROS forum, where (with over 45000 questions) we try to focus on ROS. While there is something called
rosserial
and that is used on embedded platforms, your code doesn't appear to use it, which makes this a non-ROS question. Non-ROS questions are best asked on other fora.As such, I'm going to close this.
If you don't agree, please post a comment and let us know.
Asked by gvdhoorn on 2019-04-21 05:33:23 UTC