วันจันทร์ที่ 6 กรกฎาคม พ.ศ. 2558

วันที่35 ที่Fibo

-วันนี้ก็ส่ง serial ไป control PWM ได้ แต่ยังคงติดปัญหาคิอส่ง serial ไปยัง sensor PH EC ยังคงไม่ได้ แต่ได้แนวโน้มที่จะได้แล้วพอสมควรอาจจะติดที่ส่งไปแล้วมันไม่ทำงานเพราะคำสั่งที่ส่งไปยังคงไม่ถูกตามที่คำสั่งตัวอย่าง


-ตอนนี้ก็สามารถกรองคำสั่งและตัดตัวแปรที่ไม่ต้องการได้เรียบร้อบแล้ว ส่งไปได้คำสั่งแล้วเหลือแต่ส่งไปยัง โมดูล sensor ทั่งสอง
โค้ดตัวอย่างที่ทำมา มีทั้งหมด2ส่วน


//___Pin assigned___//
const int pin_Pwm = 9; 
String inputstring = "";                                                       //a string to hold incoming data from the PC
int inputstringLength=0;

//motor interpreter
float time1 = 0.0;
float time2 = 0.0;
float s=0.0;
float counter = 0.0;
float j = 0.0;
float k=0.0;
float rpm=0.0;
float rpm_set = 0.0;
float err = 0;
float sum_err = 0;
float old_err = 0;
float Kp = 1;           //ค่าคงที่ kp
float Ki = 0.5;         //ค่าคงที่ki
float Kd = 1;           //ค่าคงที่kd
float u = 0;
float pwm_set = 0;
unsigned long Cur_time=0;
unsigned long Prev_time=0;
int   inputmotor;

//inputstring                                                    
boolean input_stringcomplete = false;                                          //have we received all the data from the PC

String sensorstringPH = "";                                                      //a string to hold the data from the Atlas Scientific product
boolean sensor_stringcompletePH = false;                                         //have we received all the data from the Atlas Scientific product

String sensorstringEC = "";                                                      //a string to hold the data from the Atlas Scientific product
boolean sensor_stringcompleteEC = false;                                         //have we received all the data from the Atlas Scientific product

void setup() 
{
  Serial.begin(115200);                                                       //Serial with PC to 38400
  Serial2.begin(38400);                                                      //Serial with pH meter to 38400
  Serial3.begin(38400);                                                      //Serial with EC meter to 38400
  
  attachInterrupt(0,Millis,RISING);
  pinMode(pin_Pwm,OUTPUT);
  digitalWrite(7,HIGH);
  digitalWrite(8,LOW);
  Serial.println("Initialize DONE!!!");
}

void Millis()
 {
  counter++;
 }

void loop() 
{
  //___Receive Command___//
  if(Serial.available()>0)
  {
    while(Serial.available()>0)
    {
      
      inputstring += char(Serial.read());
      inputstringLength += 1;
      delay(1);
    }
  }
  //___Command Checker___//
  Command_Checker();
}

ส่วนที่2Command_Checker


void Command_Checker()
{
  if (inputstringLength == 6)
  {
    if ((inputstring[4] == 'Y') && (inputstring[5] == 'U'))
    {
      if (inputstring[0] == 'M')
      {
        Serial.print("Motor Command : ");
        Serial.println(inputstring);

        String temp = "";
        temp.reserve(30);
        for (int i = 1; i < (inputstringLength - 2); i++)
        {
          temp += inputstring[i];
        }
        Serial.println(temp);
        inputmotor = temp.toInt();
        if (inputmotor <= 50 )                                                  //send to serial over 50
        {
          Cur_time = millis();                                                   //set Cur_time is millis
          s = Cur_time - Prev_time;
          if (s >= 10)
          {
            j = (counter * 10.0) / 360.0;
            k = j / (s * 0.001);
            rpm = k * 60.0;  //rpm
            Serial.print(rpm);
            err = inputmotor - rpm / 100;
            sum_err += err;
            u = Kp * err + Ki * sum_err + Kd * (err - old_err);
            old_err = err;
            pwm_set = u * 1.2;
            analogWrite(pin_Pwm, pwm_set);

            pwm_set += 0;
            counter = 0;
            Prev_time = Cur_time;
            pwm_set = 0;
          }
        }

        inputstring = "";
        inputstringLength = 0;
      }
      else if (inputstring[0] == 'P')
      {
        Serial.print("PH Command : ");
        Serial.println(inputstring);

        String temp = "";
        temp.reserve(30);
        for (int i = 1; i < (inputstringLength - 2); i++)
        {
          temp += inputstring[i];

        }
        Serial.println(temp);
        if (temp == "C04")
        {
          Serial.println("cal,low,4");
          Serial3.print("cal,low,4\r");
        }
        else if (temp == "C07")
        {
          Serial.println("cal,mid,7");
          Serial3.println("cal,mid,7\r");
        }
        else if (temp == "C10")
        {
          Serial.println("cal,high,10");
          Serial3.println("cal,high,10\r");
          delay(3000);
          Serial3.read();
        }
        else if (temp == "L00")
        {
          Serial.println("Turn off pH LED");
          Serial3.println("L,0\r");
          delay(3000);

          }

        }
        else if (temp == "L11")
        {
          Serial.println("Turn on pH LED");
          Serial3.println("L,1\r");
        }
        else if (temp == "X00")
        {

        }
        else if (temp == "I00")
        {

        }

        inputstring = "";
        inputstringLength = 0;
      }
      else if (inputstring[0] == 'E')
      {
        Serial.print("EC Command : ");
        Serial.println(inputstring);

        String temp = "";
        temp.reserve(30);
        for (int i = 1; i < (inputstringLength - 2); i++)
        {
          temp += inputstring[i];
        }
        Serial.println(temp);
        if (temp == "12K")
        {

        }
        else if (temp == "80K")
        {

        }
        else if (temp == "L00")
        {

        }
        else if (temp == "X00")
        {

        }
        else if (temp == "I00")
        {

        }

        inputstring = "";
        inputstringLength = 0;
      }
      else
      {
        Serial.print("Command (heading) ERROR!!! : ");
        Serial.println(inputstring);
        inputstring = "";
        inputstringLength = 0;
      }
    }
    else
    {
      Serial.print("Command (ending) ERROR!!! : ");
      Serial.println(inputstring);
      inputstring = "";
      inputstringLength = 0;
    }
  }
  else if (inputstringLength == 0)
  {
    // Do nothing
  }
  else
  {
    Serial.print("Command (overall) ERROR!!! : ");
    Serial.println(inputstring);
    inputstring = "";
    inputstringLength = 0;
  }
}

-ตอนนี้ทำแค่ตัว PH ส่วนECนั้นยังคงไม่ได้ทำเพราะถ้าทำ2ตัวพร้อมๆกันอาจเกิดปัญหาความเข้าใจหรือสับสนในส่วนโครงสร้างโปรแกรมก็เป็นไปได้

ไม่มีความคิดเห็น:

แสดงความคิดเห็น