2023年10月29日 星期日

Arduino Mega2560 Test Fixture Application

Purpose:

1. 兩個按鍵作為進出平台的開關 另一按鍵為平台上另一上下動作的開關
2. 完成所有動作後按任一進出按鍵就進行退出動作在退出時 蜂鳴器要發聲
3. 完成進入動作 COUNTER 要記數加一次為了控制頂針壓合次數
4. 手動跟程式控制同時可以動作

1. Two buttons serve as switches for entering and exiting the platform. The other button serves as another switch for up and down movements on the platform.
2. After completing all actions, press any entry or exit button to exit. The buzzer will sound when exiting.
3. After completing the entry action COUNTER, the count needs to be added once in order to control the number of times the ejection pin is pressed.
4. Manual and program control can be performed at the same time

Architecture:

零件配置:
1. Arduino Mega2560 控制板

2. 8 Port Relay board
3. 3 個 push button
4. 3個氣壓控制閥
5. 1個蜂鳴器
6. 次數記數器
7. 4個氣壓缸
8. ACto12VDC變壓器
YouTube Demo;
Mega2560 Code:
#include <Arduino_FreeRTOS.h>
//---------------structure ----------------------------------------
//--------- Flag structure --------------------------------------
typedef struct _vFlag
{
  uint8_t LEDFlag=1;
  uint8_t BTFlag = 0;
  uint8_t ServoFlag = 0;
  uint8_t CloseFlag=0;
  uint8_t InFlag=0;
  uint8_t OutFlag=0;
  uint8_t UpFlag=0;
  uint8_t Up1Flag=0;
  uint8_t Up2Flag=0;
  uint8_t Buzzer_Flag=0;
 
} vFlag;
vFlag *flag_Ptr;
vFlag flag;
//----------uart--------------
#define LINE_BUFFER_LENGTH 64
//--------- uart structure --------------------------------------
typedef struct _vUart
{
  char c;
  int lineIndex = 0;
  int line1Index = 0;
  int BTlineIndex = 0;
  bool lineIsComment;
  bool lineSemiColon;
  char line[128];
  char BTline[20];
  String inputString;
  String BTinputString;
  String S1inputString;
  int V[16];
  char ctemp[30];
  char I2C_Data[80];
  int DC_Spped = 50;
  float Voltage[16];
  int Buffer[128];
  int StartCnt = 0;
  int ReadCnt = 0;
  int sensorValue = 0;
} vUart;
vUart *Uart_Ptr;
vUart Uart;
//------------------------------------------------
const uint8_t LED_PIN = 13;
const uint8_t F_PIN = 12;
int buzzerPin=11;
//----relay--------
uint8_t DIO_1 = 22;
uint8_t DIO_2 = 24;
uint8_t DIO_3 = 26;
uint8_t DIO_4 = 28;
uint8_t DIO_5 = 30;
uint8_t DIO_6 = 32;
uint8_t DIO_7 = 34;
uint8_t DIO_8 = 36;
//---------治具定義------------------------------------------
uint8_t In1_B_Pin = 47;  
uint8_t In2_B_Pin = 49;
uint8_t Up_B_Pin = 51;

uint8_t In_S_Pin = 23;
uint8_t Out_S_Pin = 25;
uint8_t UpDown1_S_Pin = 27;
uint8_t UpDown2_S_Pin = 29;
//----relay--------
uint8_t InOut_V_Pin = 22;
uint8_t UpDown1_V_Pin = 24;
uint8_t UpDown2_V_Pin = 26;

uint8_t SPK_O_Pin = 28;  //30預留 12V relay
//uint8_t Count_O_Pin = 30;
uint8_t Count_O_Pin = 32;
//----------------------------------------------------
uint8_t DUTPin = 23;
uint8_t Sensor1Pin = 23;
uint8_t Sensor2Pin = 25;
uint8_t Sensor3Pin = 27;
uint8_t Sensor4Pin = 29;
//-------------------------------------------
char ctemp[20];
//------------------------------
TaskHandle_t hled;
TaskHandle_t huart0;
//------------------------------------------------------------------------------
void initial()
{
  Serial.println(F("Create Task"));
  //----------------------------------------------------------------------
  // create UART task
  xTaskCreate(vUARTTask, "UART Task", configMINIMAL_STACK_SIZE, NULL, 1, &huart0);
  // Check the results
  // create blink task
  xTaskCreate(vLEDFlashTask, "LED Task", configMINIMAL_STACK_SIZE, NULL, 2, &hled);
 
  //-------------------------------------------------------------------

}
void setup()
{
  Uart.inputString.reserve(60);
  initial();
  Serial.begin(9600);
  Serial.setTimeout(2000);
  Serial.println(F("init"));
  //-------------------------------------
  pinMode(LED_PIN, OUTPUT);
  digitalWrite(LED_PIN, LOW); // Turn LED off.
  //---------------IO setting-----------------
  pinMode(DIO_1, INPUT);
  pinMode(DIO_2, INPUT);
  pinMode(DIO_3, INPUT);
  pinMode(DIO_4, INPUT);
  pinMode(DIO_5, INPUT);
  pinMode(DIO_6, INPUT);
  pinMode(DIO_7, INPUT);
  pinMode(DIO_8, INPUT);
  pinMode(Sensor1Pin,INPUT_PULLUP); //23
  pinMode(Sensor2Pin,INPUT_PULLUP); //25
  pinMode(Sensor3Pin,INPUT_PULLUP); //27
  pinMode(Sensor4Pin,INPUT_PULLUP); //29
  //----治具 ----------------
  pinMode(In_S_Pin,INPUT_PULLUP); //23
  pinMode(Out_S_Pin,INPUT_PULLUP); //25
  pinMode(UpDown1_S_Pin,INPUT_PULLUP); //27
  pinMode(UpDown2_S_Pin,INPUT_PULLUP); //29
  pinMode(In1_B_Pin,INPUT_PULLUP); //47
  pinMode(In2_B_Pin,INPUT_PULLUP); //49
  pinMode(Up_B_Pin,INPUT_PULLUP); //51
  //----relay--------
  pinMode(InOut_V_Pin, INPUT);   //22
  pinMode(UpDown1_V_Pin, INPUT); //24
  pinMode(UpDown2_V_Pin, INPUT); //26
  pinMode(SPK_O_Pin, INPUT);     //28 //30預留 12V relay
  pinMode(Count_O_Pin, INPUT);   //32
 // start FreeRTOS
  Serial.println("Systom On!");
  vTaskStartScheduler();
  //if the scheduler start the code don't came here
  Serial.println(F("Die"));
  while (1)
    ;
}
//------------------------------------------------------------------------------
void loop()
{
 
}
//------------------------------------------------------------------------------
// high priority for blinking LED
void vLEDFlashTask(void *pvParameters)
{
  (void)pvParameters;

  pinMode(LED_PIN, OUTPUT);
  for (;;)
  {
    digitalWrite(LED_PIN, HIGH);                    // Turn LED on.
    vTaskDelay((150L * configTICK_RATE_HZ) / 1000L); // Sleep for 50 milliseconds.
    digitalWrite(LED_PIN, LOW);                      // Turn LED off.
    vTaskDelay((150L * configTICK_RATE_HZ) / 1000L); // Sleep for 150 milliseconds.
  }
}
//------------------------------------------------------------------------------
void vUARTTask(void *pvParameters)
{
  Uart.lineIsComment = false;
  Uart.lineSemiColon = false;

  while(1)
  {
//---------Fixture -------------------------
    vFixtureTask();   
while (Serial.available() > 0)
    {
      Uart.c = Serial.read();

      if ((Uart.c == '\n') || (Uart.c == '\r'))
      { // End of line reached
        if (Uart.lineIndex > 0)
        { // Line is complete. Then execute!
          Uart.line[Uart.lineIndex] = '\0'; // Terminate string
          //Serial.println( F("Debug") );
          //Serial.println( Uart.inputString );
          processCommand(Uart.line); // do something with the command
          //傳輸給藍芽
         
          Uart.lineIndex = 0;
          Uart.inputString = "";
        }
        else
        {
          // Empty or comment line. Skip block.
        }
        Uart.lineIsComment = false;
        Uart.lineSemiColon = false;
        Serial.println(F("ok>"));
      }
      else
      {
        //Serial.println( c );
        if ((Uart.lineIsComment) || (Uart.lineSemiColon))
        {
          if (Uart.c == ')')
            Uart.lineIsComment = false; // End of comment. Resume line.
        }
        else
        {
          if (Uart.c == '/')
          { // Block delete not supported. Ignore character.
          }
          else if (Uart.c == '~')
          { // Enable comments flag and ignore all characters until ')' or EOL.
            Uart.lineIsComment = true;
          }
          else if (Uart.c == ';')
          {
            Uart.lineSemiColon = true;
          }
          else if (Uart.lineIndex >= LINE_BUFFER_LENGTH - 1)
          {
            Serial.println("ERROR - lineBuffer overflow");
            Uart.lineIsComment = false;
            Uart.lineSemiColon = false;
          }
          else if (Uart.c >= 'a' && Uart.c <= 'z')
          { // Upcase lowercase
            Uart.line[Uart.lineIndex] = Uart.c - 'a' + 'A';
            Uart.lineIndex = Uart.lineIndex + 1;
            Uart.inputString += (char)(Uart.c - 'a' + 'A');
          }
          else
          {
            Uart.line[Uart.lineIndex] = Uart.c;
            Uart.lineIndex = Uart.lineIndex + 1;
            Uart.inputString += Uart.c;
          }
        }
      }
    } //while (Serial.available() > 0)
  }
}
//------------------------------------------------------------------------------
//----reset-----------
void (*resetFunc)(void) = 0;
//---------------------------------------------------------
void processCommand(char *data)
{

  int len, xlen, ylen, zlen, alen;
  int tempDIO;
  String stemp;
  unsigned int i, j, Comma;

  int val, maxv, minv;
  unsigned long duration;
  float Vpp;

  len = Uart.inputString.length();

  //-------------RESET---------------
  if (strstr(data, "VER") != NULL)
  {
    //Serial.println(F("W_ATE_Board_20201021"));
    Serial.println(F("Mega_SERIAL_20230723"));
  }
  if (strstr(data, "RESET") != NULL)
  {
    Serial.println(F("Reset"));
    resetFunc();
  }
  //===-----------------DIO port 1----------------
  if (strstr(data, "DIO1") != NULL)
  {
    //-DIO11_LOW
    if (data[3] == '1')
    {
      for (int i = 0; i < len; i++)
      {
        if (data[i] == '_')
        {
          //Serial.println("test");
          //Serial.println(i);
          xlen = i;
        }
        //ctemp[i-4]=data[i];
      }
      for (int i = 4; i < xlen; i++)
      {
        ctemp[i - 4] = data[i];
      }
      ctemp[xlen - 4] = '\0';
      tempDIO = atoi(ctemp);
      //Serial.println(tempDIO);
      for (int i = (xlen + 1); i < len; i++)
      {
        ctemp[i - (xlen + 1)] = data[i];
      }
      ctemp[len - (xlen + 1)] = '\0';

      tempDIO = tempDIO * 2 + 20;
      pinMode(tempDIO, OUTPUT);
      //if(strstr(ctemp, "ON") != NULL)
      //if(strcmp(ctemp, "ON")==0)
      //if (strcmp(Uart.ctemp, "LOW") == 0)
      if (strstr(ctemp, "LOW") != NULL)
      {
        digitalWrite(tempDIO, LOW);
      }
      else if (strstr(ctemp, "ON") != NULL)
      {
        digitalWrite(tempDIO, LOW);
      }
      else if (strstr(ctemp, "HIGH") != NULL)
      {
        digitalWrite(tempDIO, HIGH);
      }
      else if (strstr(ctemp, "OFF") != NULL)
      {
        digitalWrite(tempDIO, HIGH);
      }
    }
  }
  //------fixture --------
  if(strstr(data, "IN") != NULL )
  {
     if( (digitalRead(Out_S_Pin) == LOW) && flag.Up1Flag==0 && flag.Up2Flag==0)
     {
       flag.InFlag=3;
       pinMode(InOut_V_Pin , OUTPUT);
       digitalWrite(InOut_V_Pin , HIGH);    //in進
     }
     else
     {
       Serial.println(F("PLS OUT Fixture first!!"));
     }
  }
 
  if(strstr(data, "OUT") != NULL)
  {
     if( (digitalRead(In_S_Pin) == LOW) && flag.Up1Flag==0 && flag.Up2Flag==0)
     {
       flag.InFlag=5;
       pinMode(InOut_V_Pin , OUTPUT);
       digitalWrite(InOut_V_Pin , LOW); //出
     }
     else
     {
      Serial.println(F("PLS IN Fixture first!!"));
     }
  }
 
  if(strstr(data, "UP1") != NULL)
  {
     //if((flag.InFlag==2 || flag.InFlag==3) && flag.Up1Flag==1 && flag.Up2Flag==0 )
     if((flag.InFlag==2 || flag.InFlag==3) && (digitalRead(UpDown1_S_Pin ) == LOW) && flag.Up2Flag==0 )
     {
      pinMode(UpDown1_V_Pin , OUTPUT);
      digitalWrite(UpDown1_V_Pin , HIGH); //UP1
      flag.Up1Flag=0;
     }
     else
     {
      Serial.println(F("PLS DWON1 Fixture first!!"));
     }
  }

  if(strstr(data, "DOWN1") != NULL)
  {
     if((flag.InFlag==2 || flag.InFlag==3) && flag.Up1Flag==0 && flag.Up2Flag==0 )
     {
      pinMode(UpDown1_V_Pin , OUTPUT);
      digitalWrite(UpDown1_V_Pin , LOW); //UP1
      flag.Up1Flag=1;
     }
     else
     {
      Serial.println(F("PLS UP1 Fixture first!!"));
     }
  }

  if(strstr(data, "UP2") != NULL)
  {
     if((flag.InFlag==2 || flag.InFlag==3) && flag.Up1Flag==1 )
     {
       //Serial.println("flag.InFlag"+String(flag.InFlag));
       //Serial.println("flag.Up1Flag"+String(flag.Up1Flag));
       //Serial.println("flag.Up2Flag"+String(flag.Up2Flag));
       
       if((digitalRead(UpDown2_S_Pin) == LOW))
       {
         pinMode(UpDown2_V_Pin , OUTPUT);
         digitalWrite(UpDown2_V_Pin , HIGH); //UP1
         flag.Up2Flag=0;
       }
       
     }
     else
     {
       Serial.println(F("PLS DWON2 Fixture first!!"));
     }
  }

  if(strstr(data, "DOWN2") != NULL)
  {
     //Serial.println("flag.InFlag"+String(flag.InFlag));
     //Serial.println("flag.Up1Flag"+String(flag.Up1Flag));
     //Serial.println("flag.Up2Flag"+String(flag.Up2Flag));
     
     if((flag.InFlag==2 || flag.InFlag==3) && flag.Up1Flag==1 && flag.Up2Flag==0 )
     {
       pinMode(UpDown2_V_Pin , OUTPUT);
       digitalWrite(UpDown2_V_Pin , LOW); //UP1
       flag.Up2Flag=1;
       pinMode(Count_O_Pin , OUTPUT);
       digitalWrite(Count_O_Pin  , LOW);  
       delay(500);
       digitalWrite(Count_O_Pin  , HIGH);
     }
     else
     {
       Serial.println(F("PLS UP2 Fixture first!!"));
     }
  }
  //-----------fixture end---------------------------------------------
}
//------ vFixtureTask start --------------------------------
void vFixtureTask()
{
 
  if( (digitalRead(In1_B_Pin) == LOW && digitalRead(In2_B_Pin) == LOW ) && (flag.InFlag==5 || flag.InFlag==1 || flag.InFlag==9) )
  {
    flag.InFlag=1;
    pinMode(InOut_V_Pin , OUTPUT);
    digitalWrite(InOut_V_Pin  , HIGH);   //In 因為氣壓跟之前反接 所以 HIGH進

    if(digitalRead(In_S_Pin) == LOW)  
    {
      flag.InFlag=2;
      //Serial.println(flag.InFlag);
      //Serial.println(F("ok>"));
    }
  }

  if( (digitalRead(In_S_Pin) == LOW ) && ( flag.InFlag==2 ) )
  {
    pinMode(UpDown1_V_Pin, OUTPUT);
    digitalWrite(UpDown1_V_Pin , LOW);   //DOWN
    if(digitalRead(UpDown1_S_Pin) == LOW)  
    {
      flag.Up1Flag = 1;  //1在下面 0在上面
      //Serial.println(F("UpDown1_S_Pin"));
      //Serial.println(F("ok>"));
      pinMode(UpDown2_V_Pin, OUTPUT);
      digitalWrite(UpDown2_V_Pin , LOW);   //DOWN
      if(digitalRead(UpDown2_S_Pin) == LOW)  
      {
        flag.Up2Flag = 1;
        //Serial.println(F("UpDown2_S_Pin"));
        //Serial.println(F("ok>"));
        pinMode(Count_O_Pin , OUTPUT);
        digitalWrite(Count_O_Pin  , LOW);  
        delay(1000);
        flag.InFlag = 3;
        //Serial.println(flag.InFlag);
        //Serial.println(F("ok>"));
        digitalWrite(Count_O_Pin  , HIGH);
        //flag.OutFlag = 0;          
      }
    }
  }
  //if( (digitalRead(In1_B_Pin) == HIGH || digitalRead(In2_B_Pin) == HIGH ) && (flag.InFlag==0 || flag.InFlag==1 ) )
  if( (digitalRead(In1_B_Pin) == HIGH || digitalRead(In2_B_Pin) == HIGH ) && ( flag.InFlag == 1 ) )
  {
    //flag.InFlag=8;
    //Serial.println(flag.InFlag);
    //Serial.println(F("ok>"));
   
    pinMode(SPK_O_Pin , OUTPUT);
    digitalWrite(SPK_O_Pin , LOW);   //SPK
   

    pinMode(UpDown2_V_Pin, OUTPUT);
    digitalWrite(UpDown2_V_Pin, HIGH);   //上
    delay(300);
    pinMode(UpDown1_V_Pin, OUTPUT);
    digitalWrite(UpDown1_V_Pin , HIGH);   //上
    delay(300);
    pinMode(InOut_V_Pin , OUTPUT);
    digitalWrite(InOut_V_Pin  , LOW);   //出
   
    digitalWrite(SPK_O_Pin , HIGH);   //SPK

    if(digitalRead(Out_S_Pin) == LOW)  
    {
      digitalWrite(SPK_O_Pin , HIGH);   //SPK
      flag.InFlag=9;
      flag.Up1Flag = 0;
      flag.Up2Flag = 0;
    }
  }

  if( (digitalRead(In1_B_Pin) == LOW || digitalRead(In2_B_Pin) == LOW ) && (digitalRead(In_S_Pin) == LOW && flag.InFlag==0) )
  {
    //Serial.println(F("Out_S_Pin"));
    //Serial.println(F("ok>"));
    flag.InFlag=5;
    pinMode(InOut_V_Pin , OUTPUT);
    digitalWrite(InOut_V_Pin  , LOW);   //出
  }

  //if( (digitalRead(In1_B_Pin) == LOW || digitalRead(In2_B_Pin) == LOW ) && (flag.InFlag==2 || flag.InFlag==3) )
  if( (digitalRead(In1_B_Pin) == LOW || digitalRead(In2_B_Pin) == LOW ) && (flag.InFlag==3) )
  {
    flag.InFlag=1;
  }

  if( (digitalRead(Up_B_Pin ) == LOW ) && flag.Up2Flag == 1  && flag.Up1Flag == 1)
  {
    pinMode(UpDown2_V_Pin, OUTPUT);
    digitalWrite(UpDown2_V_Pin, HIGH);   //上
    delay(300);
    flag.Up2Flag=0;
  }
  else if( (digitalRead(Up_B_Pin ) == LOW )  && flag.Up2Flag == 0 && flag.Up1Flag == 1)
  {
    pinMode(UpDown2_V_Pin, OUTPUT);
    digitalWrite(UpDown2_V_Pin, LOW);   //下
    if(digitalRead(UpDown2_S_Pin) == LOW)  
    {
        flag.Up2Flag=1;
    }
  }
}
//-------------------------------------------



沒有留言:

張貼留言