"Hành trình vạn dặm bắt đầu từ những bước đi đầu tiên"

せんりのみちもいっぽから

Jan 6, 2020

Chương trinh bảo vệ

#include <TimerOne.h>
#include <TimerThree.h>

// define arduino pin
#define RDATA1 22
#define RSCK   23
#define RDATA  24
#define RCS    25
#define Led1   26
#define Led2   27
#define Led3   28
#define RstMCP 29

bool flick;
bool ledState;
int m_iCount;
int m_icountSend;
int m_icountSendnumber;
bool m_bstartProtect;
String dataTemp;
String m_RFID;
String datarecive;
bool m_bInc;
bool b_enableSend;
unsigned long previousMillis = 0;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(19200);
  Serial2.begin(19200);// TO BOARD

  Serial1.begin(38400);
  Serial3.begin(38400);
  InitIO();
  // Inital timer for update infomation
  //--- time 100ms to check and send data
  Timer1.initialize(200000); //100ms
  Timer1.attachInterrupt(UpdateValue);

  //---time 3s to check protect action---
  Timer3.initialize(3000000); //3s
  Timer3.attachInterrupt(ProtectAction);
}

void loop() {
  // put your main code here, to run repeatedly:
  //--- read serial 0 and 1
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= 100) {
    previousMillis = currentMillis;
    CountProtect(); // count pulse from software
  }

    Serial0Read(); // to AGV
    Serial2Read(); // to AGV

    Serial1Read(); // RFID reader
    Serial3Read(); // RFID reader
  }

  void InitIO()
  {
    pinMode(Led1, OUTPUT);
    pinMode(Led2, OUTPUT);
    pinMode(Led3, OUTPUT);
    pinMode(RstMCP, OUTPUT);
    pinMode(RDATA1, INPUT);
    digitalWrite(RstMCP, true);

  }

  void UpdateValue()
  {
    LedState();
    SendRFIDtoAGV();
  }

  void SendRFIDtoAGV()
  {
    if (!b_enableSend) return;
    m_icountSend ++;
    if (m_icountSend > 2) {
      m_icountSend = 0;
      m_icountSendnumber ++;
      SerialSend(m_RFID);// SEND TO BOARD SERIAL 2
      //Serial.println(m_RFID);
      if(m_icountSendnumber >20){
          b_enableSend = false;
          m_RFID = "";
          m_icountSend = 0;
          m_icountSendnumber=0;
      }
     
    }
  }

  void Serial0Read() // Recieve Data from AGV
  {
    //Serial.println("hehe");
    if (Serial.available()) {
      byte inByte = Serial.read();
      //Serial.write(inByte);
      if (inByte == 0x02) {
        //Serial.println("bat dau");
        datarecive = "";
      }

      else if (inByte == 0x03) {
        //Serial.println(dataTemp.length());
        //Serial.println(datarecive);
        if (datarecive = m_RFID) {
          b_enableSend = false;
          m_RFID = "";
          m_icountSend = 0;
        }
        if (datarecive = "RST") {
          b_enableSend = false;
          m_RFID = "";
          m_icountSend = 0;
        }
      }
      else
        datarecive += (char)inByte;
    }
  }

  void Serial2Read() // Recieve Data from AGV
  {
    //Serial.println("hehe");
    if (Serial2.available()) {
      byte inByte = Serial2.read();
      //Serial.write(inByte);
      if (inByte == 0x02) {
        //Serial.println("bat dau");
        datarecive = "";
      }

      else if (inByte == 0x03) {
        //Serial.println(dataTemp.length());
        //Serial.println(datarecive);
        if (datarecive = m_RFID) {
          b_enableSend = false;
          m_RFID = "";
          m_icountSend = 0;
        }
        if (datarecive = "RST") {
          b_enableSend = false;
          m_RFID = "";
          m_icountSend = 0;
        }
      }
      else
        datarecive += (char)inByte;
    }
  }

  void LedState()
  {
    //------- flick lamp to show arduino work----
    if (flick) {
      flick = false;
      digitalWrite(Led1, true);
      digitalWrite(Led2, false);
      ledState = true;
    }
    else {
      flick = true;
      digitalWrite(Led1, false);
      digitalWrite(Led2, true);
      ledState = false;
    }
    //------led protect flick when software on-------
    // ----- led off when soft ware off-------------
    if (m_bstartProtect) {
      digitalWrite(Led3, ledState);
    }
    else
    {
      digitalWrite(Led3, true);
    }
  }


  ////////////////////////////////
  ////// Read from RFID
  ////////////////////////////////

  void Serial1Read()
  {
    //Serial.println("hehe");
    if (Serial1.available()) {
      byte inByte = Serial1.read();
      //Serial.write(inByte);

      if (inByte == 0xAA) {
        //Serial.println("bat dau");
        dataTemp = "";
      }

      else if (inByte == 0xBB) {
        //Serial.println(dataTemp.length());

        if (dataTemp.length() == 4) {
          if (dataTemp == m_RFID) {
            return;
          }
          m_RFID = dataTemp;
          b_enableSend = true;
          m_icountSendnumber=0;
          //Serial.println(m_RFID);
        }
      }
      else
        dataTemp += (char)inByte;
    }

  }
  ////////////////////////////////
  ////// Read RFID
  ////////////////////////////////

  void Serial3Read()
  {
    //Serial.println("hehe");
    if (Serial3.available()) {
      byte inByte = Serial3.read();
      //Serial.write(inByte);

      if (inByte == 0xAA) {
        //Serial.println("bat dau");
        dataTemp = "";

      }

      else if (inByte == 0xBB) {
        //Serial.println(dataTemp.length());

        if (dataTemp.length() == 4) {
          if (dataTemp == m_RFID) {
            //Serial.println("trung roi");
            return;
          }
          m_RFID = dataTemp;
          b_enableSend = true;
          m_icountSendnumber=0;
          //Serial.println(m_RFID);
        }
      }
      else
        dataTemp += (char)inByte;
    }

  }

  void SerialSend(String str)
  {
    Serial2.write(2);
    Serial2.print(str);
    Serial2.write(3);
  }
  void CountProtect()
  {
    if (digitalRead(RDATA1) && !m_bInc) {
      m_bInc = true;
      m_iCount ++;
    }
    if (!digitalRead(RDATA1)) {
      m_bInc = false;
    }
  }

  void ProtectAction()
  {
    // off mcp23017 during 3s and then turn on again----
    if (!m_bstartProtect)
      // on output when stop during 3s
      digitalWrite(RstMCP, true);
    //--- off all output when didn't recieve pulse-----
    if (m_bstartProtect && m_iCount < 3 ) {
      // off output all-----------------
      m_bstartProtect = false;
      digitalWrite(RstMCP, false);
    }
    if (!m_bstartProtect && m_iCount >= 3 ) {
      //--- start protect---
      //---- enable protect action---
      m_bstartProtect = true;
    }

    m_iCount = 0;
  }

0 Post a Comment:

Post a Comment

Cảm ơn bạn đã chia sẻ ý kiến!
Chúc bạn có một ngày thật vui vẻ.

Kết nối bạn bè

Wikipedia

Search results

Video Nổi bật

Cảm ơn các bạn đã ghé thăm

 
Please Enable Javascript!Enable JavaScript