This is a remote control tester, it uses a CNC, Ethernet Communication with a JAVAFX interface and a PLC in order to control some valves.

Dependencies:   EthernetInterface mbed-rtos mbed

Fork of CNC_CONTROLLER by Lahis Almeida

Committer:
kevencastro7
Date:
Fri Dec 15 19:40:41 2017 +0000
Revision:
2:835c883d81b0
Parent:
1:ef18c260ce02
This is a remote control tester, it uses a CNC, Ethernet Communication with  a JAVAFX interface and a PLC in order to control some valves.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
waspSalander 0:7cedfb720712 1 #include "mbed.h"
waspSalander 0:7cedfb720712 2 #include "Debug.h"
waspSalander 0:7cedfb720712 3 #include "Stepp.h"
waspSalander 0:7cedfb720712 4 #include "EixoController.h"
waspSalander 0:7cedfb720712 5 #include "EixoMonitoring.h"
kevencastro7 2:835c883d81b0 6 #include "Cnc.h"
kevencastro7 2:835c883d81b0 7 #include "EthernetInterface.h"
kevencastro7 2:835c883d81b0 8 #include "Clp.h"
kevencastro7 2:835c883d81b0 9 #include "PCF8574.h"
waspSalander 0:7cedfb720712 10
kevencastro7 2:835c883d81b0 11 //=============ETHERNET==================
kevencastro7 2:835c883d81b0 12 #define MBED_DEV_IP "169.254.127.251"
kevencastro7 2:835c883d81b0 13 #define MBED_DEV_MASK "255.255.0.0"
kevencastro7 2:835c883d81b0 14 #define MBED_DEV_GW "0.0.0.0"
kevencastro7 2:835c883d81b0 15 #define ECHO_SERVER_PORT 5000
kevencastro7 2:835c883d81b0 16 //========================================
kevencastro7 2:835c883d81b0 17 //=============PINOS PCF=================
kevencastro7 2:835c883d81b0 18 #define ADRESS 0x41
kevencastro7 2:835c883d81b0 19 #define swOrignX 0//X0 --> origem
kevencastro7 2:835c883d81b0 20 #define swEndX 1//Xf --> fim
kevencastro7 2:835c883d81b0 21 #define swOrignY 2//X0 --> origem
kevencastro7 2:835c883d81b0 22 #define swEndY 3//Xf --> fim
kevencastro7 2:835c883d81b0 23 #define swOrignE 4//X0 --> origem
kevencastro7 2:835c883d81b0 24 #define swEndE 5//Xf --> fim
kevencastro7 2:835c883d81b0 25 #define sensorUp 6
kevencastro7 2:835c883d81b0 26 #define sensorDown 7
kevencastro7 2:835c883d81b0 27 //========================================
kevencastro7 2:835c883d81b0 28 //===========DISTANCIAS CNC==============
kevencastro7 2:835c883d81b0 29 #define RIGHT 1
kevencastro7 2:835c883d81b0 30 #define LEFT 0
kevencastro7 2:835c883d81b0 31 #define DOWN 1
kevencastro7 2:835c883d81b0 32 #define UP 0
kevencastro7 2:835c883d81b0 33 #define dHor1 1.3591
kevencastro7 2:835c883d81b0 34 #define dVer1 0.906
kevencastro7 2:835c883d81b0 35 #define dCirc 1.0948
kevencastro7 2:835c883d81b0 36 #define dVer2 0.9439
kevencastro7 2:835c883d81b0 37 #define dNetflix 0.4908
kevencastro7 2:835c883d81b0 38 #define dVer3 0.9816
kevencastro7 2:835c883d81b0 39 #define dSmartv 0.4153
kevencastro7 2:835c883d81b0 40 #define dVer4 1.2836
kevencastro7 2:835c883d81b0 41 #define dMute 0.132
kevencastro7 2:835c883d81b0 42 #define dHor2 3.058
kevencastro7 2:835c883d81b0 43 #define dVer5 0.9816
kevencastro7 2:835c883d81b0 44 #define dHor3 1.0948
kevencastro7 2:835c883d81b0 45 #define dVer6 0.8018
kevencastro7 2:835c883d81b0 46 #define dVoltaX 3.0202
kevencastro7 2:835c883d81b0 47 #define dVoltaY 15.2145
kevencastro7 2:835c883d81b0 48 #define noMove 0.0
kevencastro7 2:835c883d81b0 49 //========================================
waspSalander 0:7cedfb720712 50
kevencastro7 2:835c883d81b0 51 // =============== Pinos CLP ===============
kevencastro7 2:835c883d81b0 52 PinName receiverCnc = p28;
kevencastro7 2:835c883d81b0 53 PinName transmitterCnc = p27;
kevencastro7 2:835c883d81b0 54 PinName receiverElevator = p8;
kevencastro7 2:835c883d81b0 55 //PinName transmitterElevatorEnable = p9;
kevencastro7 2:835c883d81b0 56 //PinName transmitterElevatorSign = p10;
kevencastro7 2:835c883d81b0 57 PinName transmitterElevatorEnable = LED2;
kevencastro7 2:835c883d81b0 58 PinName transmitterElevatorSign = LED4;
kevencastro7 2:835c883d81b0 59 // ============================================
kevencastro7 2:835c883d81b0 60 // =============== Pinos PCF ===============
kevencastro7 2:835c883d81b0 61 PinName sda = p9;
kevencastro7 2:835c883d81b0 62 PinName scl = p10;
kevencastro7 2:835c883d81b0 63 // ============================================
kevencastro7 2:835c883d81b0 64 // =============== Pinos Driver X ===============
kevencastro7 2:835c883d81b0 65 PinName clkPinX = p21;
kevencastro7 2:835c883d81b0 66 PinName dirPinX = p22;
kevencastro7 2:835c883d81b0 67 PinName enPinX = p14;
kevencastro7 2:835c883d81b0 68
kevencastro7 2:835c883d81b0 69 // ============================================
kevencastro7 2:835c883d81b0 70 // =============== Pinos Driver Y ===============
kevencastro7 2:835c883d81b0 71 PinName clkPinY = p23;
kevencastro7 2:835c883d81b0 72 PinName dirPinY = p24;
kevencastro7 2:835c883d81b0 73 PinName enPinY = p13;
kevencastro7 2:835c883d81b0 74
kevencastro7 2:835c883d81b0 75 // ===========================================
kevencastro7 2:835c883d81b0 76 // =============== Pinos Driver E ===============
kevencastro7 2:835c883d81b0 77 PinName clkPinE = p25;
kevencastro7 2:835c883d81b0 78 PinName dirPinE = p26;
kevencastro7 2:835c883d81b0 79 PinName enPinE = p12;
kevencastro7 2:835c883d81b0 80
kevencastro7 2:835c883d81b0 81 // ===========================================
kevencastro7 2:835c883d81b0 82 //PinName valve = p11;
kevencastro7 2:835c883d81b0 83 PinName valve = LED3;
kevencastro7 2:835c883d81b0 84 DigitalOut eixoZ(valve);
waspSalander 0:7cedfb720712 85
waspSalander 1:ef18c260ce02 86
waspSalander 1:ef18c260ce02 87 Debug *debug = new Debug();
kevencastro7 2:835c883d81b0 88 PCF8574* pcf = new PCF8574(sda,scl,ADRESS);
waspSalander 0:7cedfb720712 89
waspSalander 0:7cedfb720712 90 Stepp* motorX = new Stepp(clkPinX, dirPinX, enPinX);
kevencastro7 2:835c883d81b0 91 EixoController* eixoX = new EixoController( 44500, 17, motorX, pcf,swOrignX, swEndX);
kevencastro7 2:835c883d81b0 92 EixoMonitoring* eixoMonitoringX = new EixoMonitoring( pcf,swOrignX, swEndX) ;
kevencastro7 2:835c883d81b0 93 Stepp* motorY = new Stepp(clkPinY, dirPinY, enPinY);
kevencastro7 2:835c883d81b0 94 EixoController* eixoY = new EixoController( 10000, 30, motorY,pcf, swOrignY, swEndY); //9483
kevencastro7 2:835c883d81b0 95 EixoMonitoring* eixoMonitoringY = new EixoMonitoring( pcf,swOrignY, swEndY) ;
kevencastro7 2:835c883d81b0 96 Stepp* motorE = new Stepp(clkPinE, dirPinE, enPinE);
kevencastro7 2:835c883d81b0 97 EixoController* eixoE = new EixoController( 10000, 30, motorE, pcf,swOrignE, swEndE);
kevencastro7 2:835c883d81b0 98 EixoMonitoring* eixoMonitoringE = new EixoMonitoring( pcf,swOrignE, swEndE) ;
kevencastro7 2:835c883d81b0 99 Cnc* cnc = new Cnc(eixoX, eixoY, valve,pcf,sensorUp,sensorDown);
kevencastro7 2:835c883d81b0 100 Clp* clp = new Clp(receiverCnc,transmitterCnc,receiverElevator,transmitterElevatorEnable,transmitterElevatorSign);
waspSalander 0:7cedfb720712 101
kevencastro7 2:835c883d81b0 102 Thread thread;
kevencastro7 2:835c883d81b0 103 Thread thread_2;
kevencastro7 2:835c883d81b0 104 Thread thread_3;
kevencastro7 2:835c883d81b0 105 bool control[6];
kevencastro7 2:835c883d81b0 106 Serial pc(USBTX,USBRX);
waspSalander 0:7cedfb720712 107
kevencastro7 2:835c883d81b0 108 void convert(int num,bool* bin){
kevencastro7 2:835c883d81b0 109 int i=0,r;
kevencastro7 2:835c883d81b0 110 while(num!=0){
kevencastro7 2:835c883d81b0 111 r = num%2;
kevencastro7 2:835c883d81b0 112 bin[i++] = r;
kevencastro7 2:835c883d81b0 113 num /= 2;
kevencastro7 2:835c883d81b0 114 }
kevencastro7 2:835c883d81b0 115 }
waspSalander 0:7cedfb720712 116
kevencastro7 2:835c883d81b0 117 void cnc_thread() {
kevencastro7 2:835c883d81b0 118 while(1){
kevencastro7 2:835c883d81b0 119 debug->debugPin(1,0);
kevencastro7 2:835c883d81b0 120 cnc->goToPosition(noMove,RIGHT,noMove,DOWN,1);//aoc1
kevencastro7 2:835c883d81b0 121 cnc->goToPosition(2*dHor1,RIGHT,noMove,DOWN,1);//aoc2
kevencastro7 2:835c883d81b0 122 cnc->goToPosition(noMove,LEFT,dVer1,DOWN,1);//aoc3
kevencastro7 2:835c883d81b0 123 cnc->goToPosition(dHor1,LEFT,noMove,DOWN,1);//aoc4
kevencastro7 2:835c883d81b0 124 cnc->goToPosition(dHor1,LEFT,noMove,DOWN,1);//aoc5
kevencastro7 2:835c883d81b0 125 cnc->goToPosition(noMove,LEFT,dVer1,DOWN,1);//aoc6
kevencastro7 2:835c883d81b0 126 cnc->goToPosition(dHor1,RIGHT,noMove,DOWN,1);//aoc7
kevencastro7 2:835c883d81b0 127 cnc->goToPosition(dHor1,RIGHT,noMove,DOWN,1);//aoc8
kevencastro7 2:835c883d81b0 128 cnc->goToPosition(noMove,LEFT,dVer1,DOWN,1);//aoc9
kevencastro7 2:835c883d81b0 129 cnc->goToPosition(dHor1,LEFT,noMove,DOWN,1);//aoc10
kevencastro7 2:835c883d81b0 130 cnc->goToPosition(dHor1,LEFT,noMove,DOWN,1);//aoc11
kevencastro7 2:835c883d81b0 131 cnc->goToPosition(noMove,LEFT,dVer1,DOWN,1);//aoc12
kevencastro7 2:835c883d81b0 132 cnc->goToPosition(dHor1,RIGHT,noMove,DOWN,1);//aoc13
kevencastro7 2:835c883d81b0 133 cnc->goToPosition(dHor1,RIGHT,noMove,DOWN,1);//aoc14
kevencastro7 2:835c883d81b0 134 cnc->goToPosition(noMove,LEFT,dVer1,DOWN,1);//aoc15
kevencastro7 2:835c883d81b0 135 cnc->goToPosition(dHor1,LEFT,noMove,DOWN,1);//aoc16
kevencastro7 2:835c883d81b0 136 cnc->goToPosition(dHor1,LEFT,noMove,DOWN,1);//aoc17
kevencastro7 2:835c883d81b0 137 cnc->goToPosition(noMove,LEFT,dVer1,DOWN,1);//aoc18
kevencastro7 2:835c883d81b0 138 cnc->goToPosition(2*dHor1,RIGHT,noMove,DOWN,1);//aoc19
kevencastro7 2:835c883d81b0 139 cnc->goToPosition(dHor1,LEFT,dVer1,DOWN,1); //aoc20
kevencastro7 2:835c883d81b0 140 cnc->goToPosition(dCirc,LEFT,dCirc,DOWN,1); //aoc21
kevencastro7 2:835c883d81b0 141 cnc->goToPosition(dCirc,RIGHT,noMove,DOWN,1); //aoc22
kevencastro7 2:835c883d81b0 142 cnc->goToPosition(dCirc,RIGHT,noMove,DOWN,1); //aoc23
kevencastro7 2:835c883d81b0 143 cnc->goToPosition(dCirc,LEFT,dCirc,DOWN,1); //aoc24
kevencastro7 2:835c883d81b0 144 cnc->goToPosition(dHor1,LEFT,dVer2,DOWN,1); //aoc25
kevencastro7 2:835c883d81b0 145 cnc->goToPosition(dHor1,RIGHT,dNetflix,DOWN,1); //aoc26
kevencastro7 2:835c883d81b0 146 cnc->goToPosition(dHor1,RIGHT,dNetflix,UP,1); //aoc27
kevencastro7 2:835c883d81b0 147 cnc->goToPosition(noMove,LEFT,dVer3,DOWN,1); //aoc28
kevencastro7 2:835c883d81b0 148 cnc->goToPosition(dHor1,LEFT,dSmartv,DOWN,1); //aoc29
kevencastro7 2:835c883d81b0 149 cnc->goToPosition(dHor1,LEFT,dSmartv,UP,1); //aoc30
kevencastro7 2:835c883d81b0 150 cnc->goToPosition(noMove,LEFT,dVer4,DOWN,1); //aoc31
kevencastro7 2:835c883d81b0 151 cnc->goToPosition(dHor1,RIGHT,dMute,DOWN,1); //aoc32
kevencastro7 2:835c883d81b0 152 cnc->goToPosition(dHor1,RIGHT,dMute,UP,1); //aoc33
kevencastro7 2:835c883d81b0 153 cnc->goToPosition(dHor2-2*dHor1,RIGHT,dVer5,DOWN,1); //aoc34
kevencastro7 2:835c883d81b0 154 cnc->goToPosition(dHor3,LEFT,noMove,DOWN,1);//aoc35
kevencastro7 2:835c883d81b0 155 cnc->goToPosition(dHor3,LEFT,noMove,DOWN,1);//aoc36
kevencastro7 2:835c883d81b0 156 cnc->goToPosition(dHor3,LEFT,noMove,DOWN,1);//aoc37
kevencastro7 2:835c883d81b0 157 cnc->goToPosition(noMove,LEFT,dVer6,DOWN,1);//aoc38
kevencastro7 2:835c883d81b0 158 cnc->goToPosition(dHor3,RIGHT,noMove,DOWN,1);//aoc39
kevencastro7 2:835c883d81b0 159 cnc->goToPosition(dHor3,RIGHT,noMove,DOWN,1);//aoc40
kevencastro7 2:835c883d81b0 160 cnc->goToPosition(dHor3,RIGHT,noMove,DOWN,1);//aoc41
kevencastro7 2:835c883d81b0 161 cnc->goToPosition(noMove,LEFT,dVer6,DOWN,1);//aoc42
kevencastro7 2:835c883d81b0 162 cnc->goToPosition(dHor3,LEFT,noMove,DOWN,1);//aoc43
kevencastro7 2:835c883d81b0 163 cnc->goToPosition(dHor3,LEFT,noMove,DOWN,1);//aoc44
kevencastro7 2:835c883d81b0 164 cnc->goToPosition(dHor3,LEFT,noMove,DOWN,1);//aoc45
kevencastro7 2:835c883d81b0 165 cnc->goToPosition(noMove,LEFT,dVer6,DOWN,1);//aoc46
kevencastro7 2:835c883d81b0 166 cnc->goToPosition(dHor3,RIGHT,noMove,DOWN,1);//aoc47
kevencastro7 2:835c883d81b0 167 cnc->goToPosition(dHor3,RIGHT,noMove,DOWN,1);//aoc48
kevencastro7 2:835c883d81b0 168 cnc->goToPosition(dHor3,RIGHT,noMove,DOWN,1);//aoc49
kevencastro7 2:835c883d81b0 169
kevencastro7 2:835c883d81b0 170 cnc->goToPosition(dVoltaX,LEFT,dVoltaY,UP,1);//RETORNAR
kevencastro7 2:835c883d81b0 171 debug->debugPin(1,1);
kevencastro7 2:835c883d81b0 172 wait(2);
waspSalander 1:ef18c260ce02 173 }
kevencastro7 2:835c883d81b0 174 }
kevencastro7 2:835c883d81b0 175
kevencastro7 2:835c883d81b0 176 void ethernet_thread(){
kevencastro7 2:835c883d81b0 177 EthernetInterface eth;
kevencastro7 2:835c883d81b0 178 eth.init(MBED_DEV_IP, MBED_DEV_MASK, MBED_DEV_GW); //Assign a device ip, mask and gateway
kevencastro7 2:835c883d81b0 179 eth.connect();
kevencastro7 2:835c883d81b0 180 pc.printf("IP Address is %s\n", eth.getIPAddress());
kevencastro7 2:835c883d81b0 181 TCPSocketServer server;
kevencastro7 2:835c883d81b0 182 server.bind(ECHO_SERVER_PORT);
kevencastro7 2:835c883d81b0 183 server.listen();
kevencastro7 2:835c883d81b0 184 TCPSocketConnection client;
kevencastro7 2:835c883d81b0 185
kevencastro7 2:835c883d81b0 186 while (true) {
kevencastro7 2:835c883d81b0 187 pc.printf("\nWait for new connection...\n");
kevencastro7 2:835c883d81b0 188 server.accept(client);
kevencastro7 2:835c883d81b0 189 client.set_blocking(false, 1500); // Timeout after (1.5)s
kevencastro7 2:835c883d81b0 190 pc.printf("Connection from: %s\n", client.get_address());
kevencastro7 2:835c883d81b0 191 int num;
kevencastro7 2:835c883d81b0 192 char buffer[2];
kevencastro7 2:835c883d81b0 193 while (client.is_connected()) {
kevencastro7 2:835c883d81b0 194 int n = client.receive_all(buffer, sizeof(buffer));
kevencastro7 2:835c883d81b0 195 num = buffer[0]*10+buffer[1]-528;
kevencastro7 2:835c883d81b0 196 convert(num,control);
kevencastro7 2:835c883d81b0 197 control[5] = num>31&&num<64;
kevencastro7 2:835c883d81b0 198 num = 0;
kevencastro7 2:835c883d81b0 199 buffer[0] = 0;
kevencastro7 2:835c883d81b0 200 buffer[1] = 0;
kevencastro7 2:835c883d81b0 201 //client.send("keven\n",5);
kevencastro7 2:835c883d81b0 202 }
kevencastro7 2:835c883d81b0 203 client.close();
kevencastro7 2:835c883d81b0 204 }
kevencastro7 2:835c883d81b0 205 }
kevencastro7 2:835c883d81b0 206
kevencastro7 2:835c883d81b0 207 void elevator_thread(){
kevencastro7 2:835c883d81b0 208 while(1){
kevencastro7 2:835c883d81b0 209 if(control[5]){
kevencastro7 2:835c883d81b0 210 //eixoE->goToOrigem(swOrignE,LEFT);
kevencastro7 2:835c883d81b0 211 clp->TransmitElevator(1);
kevencastro7 2:835c883d81b0 212 pc.printf("primeiro controle: %d\n",control[4]);
kevencastro7 2:835c883d81b0 213 eixoE->goToPosition(20,RIGHT);
kevencastro7 2:835c883d81b0 214 clp->TransmitElevator(control[4]);
kevencastro7 2:835c883d81b0 215 pc.printf("segundo controle: %d\n",control[3]);
kevencastro7 2:835c883d81b0 216 eixoE->goToPosition(20,RIGHT);
kevencastro7 2:835c883d81b0 217 clp->TransmitElevator(control[3]);
kevencastro7 2:835c883d81b0 218 pc.printf("terceiro controle: %d\n",control[2]);
kevencastro7 2:835c883d81b0 219 eixoE->goToPosition(20,RIGHT);
kevencastro7 2:835c883d81b0 220 clp->TransmitElevator(control[2]);
kevencastro7 2:835c883d81b0 221 pc.printf("quarto controle: %d\n",control[1]);
kevencastro7 2:835c883d81b0 222 eixoE->goToPosition(20,RIGHT);
kevencastro7 2:835c883d81b0 223 clp->TransmitElevator(control[1]);
kevencastro7 2:835c883d81b0 224 pc.printf("quinto controle: %d\n",control[0]);
kevencastro7 2:835c883d81b0 225 eixoE->goToPosition(20,RIGHT);
kevencastro7 2:835c883d81b0 226 clp->TransmitElevator(control[0]);
kevencastro7 2:835c883d81b0 227 control[5] = 0;
kevencastro7 2:835c883d81b0 228 }
kevencastro7 2:835c883d81b0 229 }
kevencastro7 2:835c883d81b0 230 }
kevencastro7 2:835c883d81b0 231
kevencastro7 2:835c883d81b0 232
kevencastro7 2:835c883d81b0 233 int main() {
kevencastro7 2:835c883d81b0 234 thread.start(cnc_thread);
kevencastro7 2:835c883d81b0 235 thread_2.start(ethernet_thread);
kevencastro7 2:835c883d81b0 236 thread_3.start(elevator_thread);
waspSalander 1:ef18c260ce02 237 }