Remote inc coolant nozzle control program

Dependencies:   mbed reScale USBDevice

Dependents:   Nozzle_inputs Nozzle_display

main.cpp

Committer:
BETZtechnik
Date:
2019-02-08
Revision:
1:e8452b75301e
Parent:
0:09419d572e90
Child:
3:bbbacfd62773

File content as of revision 1:e8452b75301e:

 #include "mbed.h"
#include "Adafruit_ST7735.h"
#include "RPG.h"
#include "reScale.h"
#include "Ee24xx08.h"


// DISPLAY INIT:

#define BLACK    0x0000
#define BLUE     0x001F
#define RED      0xF800
#define GREEN    0x07E0
#define CYAN     0x07FF
#define MAGENTA  0xF81F
#define YELLOW   0xFFE0 
#define WHITE    0xFFFF

#define currentToolAddress 51
#define numToolsAddress 52
#define numNozAddress 53


//DigitalOut myled(P1_13);


I2C iic(P0_5, P0_4);
Serial device(P0_14, P0_13, 9600);  // RS 485  TX RX?

DigitalOut dir(P0_21);


Ee24xx08 eeprom(&iic);

Adafruit_ST7735 tft(P0_9, P0_8, P0_6, P0_2, P0_22, P0_7); // MOSI, MISO, SCLK, SSEL, TFT_DC, TFT_RST

reScale servo1Scale(-45,0,750,1800); //19850 is 1.0v from voltage divider at 10k.

 PwmOut myServo(P1_13); //display board
//PwmOut myServo(D4);

//DigitalIn tUpButton(P2_11);
//DigitalIn tDownButton(P2_12);
DigitalIn encoderFalseButton(P0_23); // FALSE!!! push button of knob
DigitalIn encoderButton(P0_20); // push button of knob
DigitalIn backButton(P0_15); // back button

Timer t;
int lastBackButton;
int lastDownButton;
int lastEncoderButton;

int dirt = 0;

int main() {

// ************************************** SCREEN INIT *************************************************

 tft.initR(INITR_GREENTAB);   // initialize a ST7735S chip, black tab
      
  tft.setRotation(3);
  tft.fillScreen(BLACK);
  tft.setCursor(30,120);
  tft.setTextColor(WHITE);
  tft.setTextWrap(false);
  tft.printf("AccuBlast v0");
  
        tft.setCursor(4, 4);
        tft.setTextColor(WHITE);
        tft.setTextSize(2);
        tft.printf("N1");
        
        tft.setCursor(80, 4);
        tft.setTextColor(WHITE);
        tft.setTextSize(2);
        tft.printf("N2");
        
        tft.setCursor(4, 28);
        tft.setTextColor(WHITE);
        tft.setTextSize(2);
        tft.printf("T");   
        
        tft.setCursor(4, 50);
        tft.setTextColor(WHITE);
        tft.setTextSize(1);
        tft.printf("Nozzle");   
        
        tft.setCursor(4, 65);
        tft.setTextColor(WHITE);
        tft.setTextSize(1);
        tft.printf("Res."); 
        
        tft.setCursor(4, 80);
        tft.setTextColor(WHITE);
        tft.setTextSize(1);
        tft.printf("Res.");     
        
        tft.setCursor(4, 95);
        tft.setTextColor(WHITE);
        tft.setTextSize(1);
        tft.printf("Num Tls");     
        
        tft.setCursor(4, 110);
        tft.setTextColor(WHITE);
        tft.setTextSize(1);
        tft.printf("Num Noz");   
        
 // **************************************************************************************************************       
  
int received = 9;
  
  int count = 0;
  int maxCount = 7; //max number of menu items
  int lastCount = 99;
  
  //************* Pull previous setting from eeprom *******************
  
  char tempToolNo = 0;                  // last tool in spindle
  eeprom.read(currentToolAddress, &tempToolNo);
  int currentToolNo = (int)tempToolNo;
  
  char tempNumTools = 0;                  // number of tools in ATC
  eeprom.read(numToolsAddress, &tempNumTools);
  int numTools = (int)tempNumTools;
  
  char tempNumNoz = 0;                  // number of nozzles installed
  eeprom.read(numNozAddress, &tempNumNoz);
  int numNoz = (int)tempNumNoz;
  
  char tempLastAngle = 0;                  // last angle of last tool in spindle
  eeprom.read(currentToolNo, &tempLastAngle);
  int lastAngle = (int)tempLastAngle;
  lastAngle = lastAngle - 45;              // Not storing signed int, so signed value is 45 less than what was stored to memory.
  
  //******************** Button debounce *********************************
  


 backButton.mode(PullUp);     // button pull up   
 //tDownButton.mode(PullUp);     // button pull up   
 encoderButton.mode(PullUp); 
 encoderFalseButton.mode(PullUp);  
    
t.start();                   // starts timer 
lastBackButton = t.read_ms(); 
lastDownButton = t.read_ms(); 
lastEncoderButton =  t.read_ms();  
 
 //**************************************************************************** 
  
  int lastToolNo = 99; 
  int lastNumTools = -1; // trigger to write eeprom value for number of tools
  
  int lastNumNoz = 0; //trigger display for number of nozzles after startup
  
  int n1Pos = lastAngle;                 //Set servo to last known postition prior to power off
  int lastN1Pos = 150;
  
  int servo1Pos = 0;
  
   RPG rpg1(P2_2,P1_20,P0_23);  //  RPG (PinName pA, PinName pB, PinName pPB)P0_23 is unconnected!! ;
  
  myServo.period_ms(20);

/*
      char t1Ang = 200;
      char t1Read = 0;
      
      eeprom.write(1, t1Ang);
      wait(1);
      eeprom.read(1, &t1Read);
      

  tft.setCursor(30,110);
  tft.setTextColor(WHITE);
  tft.setTextWrap(true);
  // tft.printf("%s\r\n",t1Read);
          tft.printf("%d", currentTool);
    */
    
      uint8_t c = 0; // for 485 link
    
    
    while(1) {
        
//myled = 1;        
        
if (t.read() > 1200){              // timer can only hold 30 min, so reset after 20
t.reset();
lastBackButton = t.read_ms();
lastDownButton = t.read_ms();
lastEncoderButton = t.read_ms();
} 

if (backButton == 0){  //Back button was pressed so back to default selection
     count = 0;
     }    
     
if (encoderButton == 0){
if (t.read_ms() > (lastEncoderButton + 200)){
count = count + 1;  //Reset count if PB pressed
}
 if (count > maxCount){
    count = 0;
    }      
lastEncoderButton = t.read_ms();
   
  if (count > maxCount){
        count = 0;
        }  
        }    
 
 //****************** ENCODER ACTIONS ************************************************
 if (count == 0){   // knob controls servo value
dirt = rpg1.dir(); //Get Dir
n1Pos = n1Pos + dirt; //Ad Dirt to n1pos
if (n1Pos > 0){
 n1Pos = 0;
 }
 if (n1Pos < -45){
     n1Pos = -45;
     }
     }
     
if (count == 2){     //knob controls tool number
dirt = rpg1.dir(); //Get Dir
currentToolNo = currentToolNo + dirt; //Ad Dirt to n1pos
if (currentToolNo > numTools){
 currentToolNo = numTools;
 }
 if (currentToolNo < 1){
     currentToolNo = 1;
     }
     }
     
     if (count == 6){     //knob controls tool number
dirt = rpg1.dir(); //Get Dir
numTools = numTools + dirt; //Ad Dirt to n1pos
if (numTools > 50){
 numTools = 50;
 }
 if (numTools < 1){
     numTools = 1;
     }
     }
     
     if (count == 7){     //knob controls tool number
dirt = rpg1.dir(); //Get Dir
numNoz = numNoz + dirt; //Ad Dirt to n1pos
if (numNoz> 2){
 numNoz = 2;
 }
 if (numNoz < 1){
     numNoz = 1;
     }
     }     
     
 
// ************* Rectangles for identifying active selection *****************************
 
if (count == 0){ // N1 adjust
tft.drawRect(2,2,74,18,GREEN);
}
else{
tft.drawRect(2,2,74,18,WHITE);
}

if ((count == 1) && (numNoz == 2)){ // N2 adjust
tft.drawRect(78,2,74,18,GREEN);
}

if ((count != 1) && (numNoz == 2)){ // N2 adjust
tft.drawRect(78,2,74,18,WHITE);
}


if (count == 2){ 
tft.drawRect(20,26,28,18,GREEN);
}
else{
tft.drawRect(20,26,28,18,BLACK);
}
 
if (count == 3){
tft.drawRect(53,48,20,12,GREEN);
}
else{
tft.drawRect(53,48,20,12,BLACK);
} 
 
if (count == 4){
tft.drawRect(53,63,20,12,GREEN);
}
else{
tft.drawRect(53,63,20,12,BLACK);
} 

if (count == 5){
tft.drawRect(53,78,20,12,GREEN);
}
else{
tft.drawRect(53,78,20,12,BLACK);
} 

if (count == 6){
tft.drawRect(53,93,20,12,GREEN);
}
else{
tft.drawRect(53,93,20,12,BLACK);
} 

if (count == 7){
tft.drawRect(53,108,20,12,GREEN);
}
else{
tft.drawRect(53,108,20,12,BLACK);
} 

//****************************************************************************************
 
servo1Pos = servo1Scale.from(n1Pos);

myServo.pulsewidth_us(servo1Pos);

//********************** update display values **********************************************

    
 if (currentToolNo != lastToolNo){
       /*
        tft.fillRect(103, 2, 55,25, BLACK);      
        tft.setCursor(105, 4);
        */
        tft.setCursor(22, 28);
        tft.setTextColor(BLACK);
        tft.setTextSize(2);
        tft.printf("%d", lastToolNo);      
        tft.setTextColor(WHITE);
        //tft.setTextSize(2);
        // tft.printf("T");
        tft.setCursor(22,28);
        tft.printf("%d", currentToolNo);
        tempLastAngle = 0;                  
        eeprom.read(currentToolNo, &tempLastAngle);
        lastAngle = (int)tempLastAngle;
        lastAngle = lastAngle - 45;                       // Not storing signed int, so signed value is 45 less than what was stored to memory.
        //RotaryEncoder knob1(D2, D3, -45, 45, lastAngle);  //  RotaryEncoder(PinName pin1_name, PinName pin2_name, int min = 0, int max = 100, int val = 50);
        //knob1.val = lastAngle;
        n1Pos = lastAngle;
        eeprom.write(currentToolAddress, currentToolNo);
        //wait(0.1);
        lastToolNo = currentToolNo;
        }
       
    
   if (n1Pos != lastN1Pos){     
        tft.setCursor(40, 4);
        tft.setTextColor(BLACK);
        tft.setTextSize(2);
        tft.printf("%d", lastN1Pos);
        tft.setCursor(40,4);
        tft.setTextColor(GREEN);
        tft.setTextSize(2);
        tft.printf("%d", n1Pos);
        eeprom.write(currentToolNo, n1Pos + 45);        // store new angle in eeprom, add 45 because its an unsigned value.
        //wait(0.1); // adjusted from 1
        dir=1;
        device.printf("N");
        device.printf("%d", n1Pos + 145); //send over RS485 add 145 so no negative values and always 3 digits. 1xx for N1, 2xx for nozzle 2
       //device.printf("N245");  // TEST!!!
       tft.setCursor(100, 50);
         tft.setTextSize(2);
       tft.printf("%d", n1Pos + 145);
        //wait(0.05);
        dir=0;
        lastN1Pos = n1Pos;
        }  
        
   if (numTools != lastNumTools){     
        tft.setCursor(55, 95);
        tft.setTextColor(BLACK);
        tft.setTextSize(1);
        tft.printf("%d", lastNumTools);
        tft.setCursor(55,95);
        tft.setTextColor(WHITE);
        tft.setTextSize(1);
        tft.printf("%d", numTools);
        eeprom.write(numToolsAddress, numTools);        // store new angle in eeprom, add 45 because its an unsigned value.
        //wait(0.1); // adjusted from 1
        lastNumTools = numTools;
        }          

   if (numNoz != lastNumNoz){     
        tft.setCursor(55, 110);
        tft.setTextColor(BLACK);
        tft.setTextSize(1);
        tft.printf("%d", lastNumNoz);
        tft.setCursor(55,110);
        tft.setTextColor(WHITE);
        tft.setTextSize(1);
        tft.printf("%d", numNoz);
        eeprom.write(numNozAddress, numNoz);        // store new angle in eeprom, add 45 because its an unsigned value.
        //wait(0.1); // adjusted from 1
        
        if (numNoz == 2){
        tft.setCursor(80, 4);
        tft.setTextColor(WHITE);
        tft.setTextSize(2);
        tft.printf("N2");
        tft.drawRect(78,2,74,18,WHITE);
        }
        if (numNoz == 1){
        tft.setCursor(80, 4);
        tft.setTextColor(BLACK);
        tft.setTextSize(2);
        tft.printf("N2");
        tft.drawRect(78,2,74,18,BLACK);        
        }
        lastNumNoz = numNoz;
        }    




   if (count != lastCount){     
        tft.setCursor(150, 100);
        tft.setTextColor(BLACK);
        tft.setTextSize(1);
        tft.printf("%d", lastCount);
        tft.setCursor(150,100);
        tft.setTextColor(RED);
        tft.setTextSize(1);
        tft.printf("%d", count);
        lastCount = count;
        }                 

//**************************** RS485 link ***************************************
    
  //dir =0; // receiver enable  
    
if (device.readable())
            {
                c = device.getc();
                if(c=='B'){
                received = 1;   
                } 
}

if (received == 1){
        tft.setCursor(150, 80);
        tft.setTextColor(WHITE);
        tft.setTextSize(1);
        tft.printf("1"); 
        }


}
}