Remote inc coolant nozzle control program

Dependencies:   mbed reScale USBDevice

Dependents:   Nozzle_inputs Nozzle_display

Revision:
1:e8452b75301e
Parent:
0:09419d572e90
Child:
3:bbbacfd62773
--- a/main.cpp	Sun Jan 06 18:28:18 2019 +0000
+++ b/main.cpp	Fri Feb 08 20:50:00 2019 +0000
@@ -1,6 +1,6 @@
  #include "mbed.h"
 #include "Adafruit_ST7735.h"
-#include "RotaryEncoder.h"
+#include "RPG.h"
 #include "reScale.h"
 #include "Ee24xx08.h"
 
@@ -16,169 +16,442 @@
 #define YELLOW   0xFFE0 
 #define WHITE    0xFFFF
 
-//const int addrW = 0xA2; //1010 0010 write word block B0
-//const int addrR = 0xA2; //1010 0011 read word block B0
-
-long currentSpot = 0;
+#define currentToolAddress 51
+#define numToolsAddress 52
+#define numNozAddress 53
 
 
-DigitalOut myled(LED1);
-//DigitalOut out(D2);
+//DigitalOut myled(P1_13);
+
 
-//I2C i2c(P0_5, P0_4);
-Serial ser(USBTX,USBRX);
 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(D11, D12, D13, D10, D7, D9); // MOSI, MISO, SCLK, SSEL, TFT_DC, TFT_RST
-
-
-RotaryEncoder knob1(D2, D3, -50, 0, 0);
+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.
 
-//SDFileSystem sd(P1_20, P1_21, P1_22, P1_23, "sd"); // the pinout on the mbed Cool Components workshop board
+ 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
 
-PwmOut myServo(D4);
+Timer t;
+int lastBackButton;
+int lastDownButton;
+int lastEncoderButton;
+
+int dirt = 0;
 
 int main() {
-  //  out =1;
+
+// ************************************** 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);  
     
-      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(true);
-  tft.printf("AccuBlast v0");
+t.start();                   // starts timer 
+lastBackButton = t.read_ms(); 
+lastDownButton = t.read_ms(); 
+lastEncoderButton =  t.read_ms();  
+ 
+ //**************************************************************************** 
   
-  int mode=1; // 0= manual, 1=auto
-  int lastMode=0; // placeholder for changing display.
+  int lastToolNo = 99; 
+  int lastNumTools = -1; // trigger to write eeprom value for number of tools
   
-  int toolNo = 12;
-  int lastToolNo = 99; 
+  int lastNumNoz = 0; //trigger display for number of nozzles after startup
   
-  int n1Pos = 0;
+  int n1Pos = lastAngle;                 //Set servo to last known postition prior to power off
   int lastN1Pos = 150;
   
   int servo1Pos = 0;
   
-    myServo.period_ms(20);
-   // myServo.pulsewidth_us(MID); //NB in microseconds
-   
-   
-   /*
-   char data[2];
-data[0] = 0x01;
-data[1] = 0x01;
-   
+   RPG rpg1(P2_2,P1_20,P0_23);  //  RPG (PinName pA, PinName pB, PinName pPB)P0_23 is unconnected!! ;
+  
+  myServo.period_ms(20);
 
- char cmd[3];
-        cmd[0] = 0x01;
-        cmd[1] = 0x03;
-        i2c.start();
-        i2c.write(addrW, cmd, 2);
-        cmd[0] = 0x00;
-        cmd[1] = 0x00;
-        i2c.write(addrW, cmd, 2);
-        i2c.stop();
-        i2c.start();
-        //i2c.write(addrR);
-        i2c.read(addrR, data, 2);
-       */ 
-     
-       char str[4] = "-45";
-       char str2[12] = "0";
-       char str3[4] = "3";
-    
-
-    eeprom.write(01,str,4);
-    wait(1);
-    eeprom.write(05,str3,4);
-    wait(1);
-    
-    
-    eeprom.read(01,str2,4);
-   // ser.printf("%s\r\n",str);
+/*
+      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",str2);
-
-int T1 = int(str2[4]);
-
-  tft.setCursor(30,80);
-  tft.setTextColor(WHITE);
-  tft.setTextWrap(true);
-        tft.printf("%d", T1);
+  // tft.printf("%s\r\n",t1Read);
+          tft.printf("%d", currentTool);
+    */
+    
+      uint8_t c = 0; // for 485 link
+    
     
     while(1) {
         
- n1Pos = knob1.getVal();
+//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;
+        }  
+        }    
  
-servo1Pos = servo1Scale.from(knob1.getVal());
+ //****************** 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);
 
- if (mode != lastMode) {
-        if (mode==1){
-        tft.fillRect(2, 2, 30, 25, WHITE);      
-        tft.setCursor(8, 4);
-        tft.setTextColor(BLACK);
-        tft.setTextSize(3);
-        tft.printf("A");
-        lastMode = mode;
-        }
-        
-        if (mode == 0){
-        tft.fillRect(2, 2, 40,25, WHITE);      
-        tft.setCursor(4, 4);
+//********************** 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(3);
-        tft.printf("M");
-        lastMode=mode;
-        }
-        }
-    
- if (toolNo != lastToolNo){
-        tft.fillRect(103, 2, 55,25, WHITE);      
-        tft.setCursor(105, 4);
-        tft.setTextColor(BLACK);
-        tft.setTextSize(3);
-        tft.printf("T");
-        tft.setCursor(118, 4);
-        tft.printf("%d", toolNo);
-        lastToolNo = toolNo;
+        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(80, 100);
+        tft.setCursor(40, 4);
         tft.setTextColor(BLACK);
         tft.setTextSize(2);
         tft.printf("%d", lastN1Pos);
-        tft.setCursor(80, 100);
-        tft.setTextColor(WHITE);
+        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"); 
+        }
+
+
+}
+}