scan2

Dependencies:   LcdAcm1602ni QEI mbed

scan2.cpp

Committer:
ekyykysn
Date:
2018-03-22
Revision:
0:ae1d54517aa0

File content as of revision 0:ae1d54517aa0:

/*--------------------------------ライブラリ------------------------------------*/
//
// 以下の3つのライブラリを使用するので、それぞれのライブラリをImportする。
// (1) EthernetInterface
// (2) mbed-rtos
// (3) TextLCD

#include "mbed.h"//20170621 バージョン固定
#include "LPC17xx.h"//LPCExpresso
#include "LcdAcm1602ni.h"
#include "string.h"
#include <limits.h>
//#include "TextLCD.h"
#include "Serial.h"
//#include "QEI.h"
#
/*-----------------------------------定義--------------------------------------*/

//MOTOR
#define CW 0
#define CCW 1


//MLED
#define OFF 0
#define ON  1

/*--------------------------------ピン定義-------------------------------------*/    

//MLED mbed内LEDの設定
DigitalOut mled0(LED1);
DigitalOut mled1(LED2);
DigitalOut mled2(LED3);
DigitalOut mled3(LED4);

//TextLCD lcd(p24, p26, p27, p28, p29, p30);
LcdAcm1602ni lcd(p28,p27);// sda,scl

//インターフェイス
//LED
//DigitalOut open_led(p9);
//DigitalOut close_led(p10);

//スイッチ
//DigitalIn close_sw(p19);
DigitalIn open_sw(p20);

//エンコーダー
//InterruptIn pulse_input(p17);


//モーター① 入出力ピン
DigitalOut ccw_pwm(p21);//CW方向へ回転 
DigitalOut cw_pwm(p22);//CCW方向へ回転
DigitalIn close_limit(p15);
DigitalIn open_limit(p16);
DigitalIn pulse_input(p9);

Ticker flipper;
Serial pc(USBTX,USBRX);
/*-------------------------------グローバル変数---------------------------------*/
 
long wt = 500;//モーターパルス用変数 wt = 周期T/2 max500
int dir = 1;
float tim_pulse = 0;
float lastpulse = 0;
float tim_d = 0;
int tim_arry[30] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
int state = 0;
int nstep = 0;
int cstep = 0;
int count = 0;
int pulse = 0;

/*-----------------------------set up -----------------------------------*/
/*for (int i = 0; i < 2; i++) {
    //    pinMode(sw[i],INPUT_PULLUP);
    pinMode(tim_pulse, INPUT_PULLUP); //光電センサ変更時に変更 プルダウン必要
  }*/
/*-----------------------------flip 割り込み-----------------------------------*/
//tim.mode(PullNone);
void flip(){
      //tim_pulse = tim.read();
      //tim_d = last_tim - tim_pulse;
      //last_tim = tim_pulse;
      //palse = wheel.getPulses();
      //wheel.reset();
      //rpm = 60000*palse/1024;
    mled1 = ON;
    pulse = pulse_input.read();
    //pc.printf("%d",pulse);
    if (pulse != lastpulse){   
           //ラストパルス(一つ前のパルス)と同じではないとき !←同じでない
        if (pulse == 1) count ++;        //パルスが1の時カウント
            lcd.initialize();
            lcd.locate(0,1);
            lcd.printf(" count");
            //pc.printf("%d",count);
            mled2 = ON;
    }
    lastpulse = pulse;                 //00または、11の意味
    mled3 = ON;
    //pc.printf("int : %d",pulse_input);
}
  
/*-----------------------------シリアル通信-------------------------------------*/
/*int recvStr(void) {
  int i = 0, flg = 0;
  char c, buf[30] = "";
  state = -1;
  pc.printf("waiting");
  do {
    while (Serial.available()) {
      c = Serial.read();
      buf[i] = c;
      if (i == 1 && state < 0) {
        if (strcmp(buf, "sp") == 0) {
          state = SP;
          i = -1;
        }
        else if (strcmp(buf, "gc") == 0)  state = GC;
        else if (strcmp(buf, "gp") == 0)  state = GP;
        else if (strcmp(buf, "gv") == 0)  state = GV;
        else if (strcmp(buf, "rp") == 0)  state = RP;
      }

      if (c == ';') {
        buf[i + 1] = '\0';
        nstep = atoi(buf);
        if (nstep == 0 && state == SP)
          state = RP;
        flg = 1;
        //CRLFを読みとり、シリアルバッファをクリアにする
        Serial.read();
        Serial.read();
        break;
      }
      else if (state >= 0 || (buf[0] == 'g' || buf[0] == 's' || buf[0] == 'r')) {
        i++;
        delay(1);
      }
    }
  } while (flg == 0);

  return flg;
}*/


/*-----------------------------MotorContoroll---------------------------------*/
void motorControll(){
    
   // if(long i = 0; i < abs(nstep[ch]); i++){
       // if(i == 0){
      //      digitalWrite(con[0],HIGH);)
    if(open_sw == ON){
        if(open_limit == OFF){
        //mled1 = ON;
        cw_pwm = ON;
        wait_us(wt);
          
        cw_pwm = OFF;
        wait_us(wt);
        }
        else
        {
        //mled2 = ON;
        ccw_pwm = ON;
        wait_us(wt);
        
        ccw_pwm =OFF;
        wait_us(wt);         
        }

    }
    else
    {
        //mled1= OFF;
        //mled2= OFF;
    }

}

int main(){
    lcd.initialize();
    lcd.locate(0,0);
    lcd.printf("*scan*");
    pc.printf("*AutoSlideCover*");
    lcd.locate(1,0);
    lcd.printf("setup....");
    pc.printf("setup....");
    //char c;
    //count = 0;
    pc.baud(115200);
    flipper.attach(&flip, 2.0);
    //tim.mode(PullNone);
    
        while(1){
        //flipper.attach(&flip, 2.0);
        dir = CW;
        motorControll();
        dir = CCW;
        motorControll();
        
        //pc.printf("Pulses is: %i\n", wheel.getPulses());
        //pc.printf("rpm is: %i\n", rpm);
        }
}