Руслан Бредун / Mbed 2 deprecated STM32-MC_node

Dependencies:   mbed Watchdog stm32-sensor-base2

main.cpp

Committer:
ommpy
Date:
2020-07-22
Revision:
6:a760ce6defbe
Parent:
5:97117a837d2c
Child:
7:2f218add711e

File content as of revision 6:a760ce6defbe:

#include <global.h>

Serial pc(UART1_TX, UART1_RX);
RS485 RS485(UART2_TX,UART2_RX,DE_TXD_2); // Tx, Rx , !RE and DE MAX485 pin
 
//Serial pc(USBTX, USBRX);
Timer timer;
typedef uint8_t byte;

DigitalOut select(DE_TXD_2);
byte full_value[9];
byte base_data[4] = {0xAA,0x55,0x03,0x01};
byte data[9] = {0x01,0x04,0x00,0x48,0x00,0x02,0xf1,0xdd};//your data

char test_buffer[10]; 

const unsigned char CRC7_POLY = 0x91;
 
unsigned char getCRC(unsigned char message[], unsigned char length)
{
  unsigned char i, j, crc = 0;
 
  for (i = 0; i < length; i++)
  {
    crc ^= message[i];
    for (j = 0; j < 8; j++)
    {
      if (crc & 1)
        crc ^= CRC7_POLY;
      crc >>= 1;
    }
  }
  return crc;
}
 
 
int main()
{
    DigitalOut led(DEBUG_LED);
    DigitalOut led2(DE_TXD_1, 1); // activate transmitting rs485-1


#if TEST_ULTRASONIC
    int time_to_check_us = 1000;
    JSN_SR04 sensor1(TIM1_CH2, TRIG_PA8_OUT);
//    JSN_SR04 sensor1(TIM1_CH2, TRIG_PB14_OUT); //no triger signal
//    JSN_SR04 sensor1(TIM1_CH2, TRIG_PB15_OUT);
    sensor1.setRanges(30, 200);
//    sensor2.setRanges(30, 200);
//    sensor3.setRanges(30, 200);
//    pc.printf("Min. range = %g cm\n\r",sensor1.getMinRange());
    while(true) {
        timer.reset();
        timer.start();
        sensor1.startMeasurement();
        //while (sensor1.isNewDataReady() == false && timer.read_ms() < time_to_check_us) {
        //pc.printf("Distance1: %5.1f mm -- Distance2: %5.1f mm -- -- Distance3: %5.1f mm \r\n", sensor1.getDistance_cm()), sensor2.getDistance_cm(), sensor3.getDistance_cm());
        pc.printf("Distance1: %5.1f mm \r\n", sensor1.getDistance_cm());
        

        timer.stop();
        wait_ms(2);

    }

#endif

#if TEST_ENCODERS
int min1_value = 2000;
int max1_value = 0;
    AS5045 encoder_1(SP1_NSS1);
    AS5045 encoder_2(SP1_NSS2);
    while(1) {
        led = !led;
        double encoder_1_Value = encoder_1.getPosition();
        wait_ms(30);
        double encoder_2_Value = encoder_2.getPosition();
        wait_ms(30);
        //encoder_1_Value =
        //if( encoder_1_Value.IsValid() && encoder_2_Value.IsValid() )
    
        pc.printf("Sensor Values = %f --- %f \n\r",encoder_1_Value, encoder_2_Value ); //
        //else
        //pc.printf("Invalid data read");

        //wait_ms(50);
    }

#endif

# if TEST_IR
PwmIn a(PB_4);
    //E18_D80NK infared1 (IR1_PB12_OUT);
//    E18_D80NK infared2 (IR2_PB13_OUT);
    while (true) {
       // pc.printf ("Is there any obstacle: %d ---- %d ",  infared1.checkObstacle(),infared2.checkObstacle() );
       
        pc.printf ("\n\r");
    }


#endif

# if DEMO_CODE

    JSN_SR04 sensor1(TIM1_CH2, TRIG_PA8_OUT);
    E18_D80NK infared1 (IR1_PB12_OUT);
    E18_D80NK infared2 (IR2_PB13_OUT);
    
    sensor1.setRanges(30, 200);
    //full_value[0] = base_data[0]
//    full_value[1] = base_data[1]
//    full_value[2] = base_data[2]
//    full_value[3] = base_data[3] 
//    full_value[4] = base_data[4]
    
    while(true) {
        timer.reset();
        timer.start();
        sensor1.startMeasurement();
        pc.printf("Distance1: %5.1f mm  ----- %d ---- %d  \r\n", sensor1.getDistance_cm() , infared1.checkObstacle(),infared2.checkObstacle());
        
        //full_value[5] = base_data
//        full_value[6] = base_data
//        full_value[7] = base_data
//        full_value[8] = base_data
        
        sprintf(test_buffer, "%5.1f_%d_%d ", sensor1.getDistance_cm() , infared1.checkObstacle(),infared2.checkObstacle()); 
        
        select = 1 ;   // Enable sending on MAX485
        RS485.sendMsg(data,sizeof(data));
        wait_ms(100);
        select = 0 ;  // Enable receiving on MAX485
        timer.stop();
        wait_ms(2);

    }


#endif

}