Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: F7_Ethernet mbed mbed-rtos
main.cpp
00001 #include "mbed.h" 00002 #include "rtos.h" 00003 #include "EthernetInterface.h" 00004 #include "common.h" 00005 #include "stdio.h" 00006 00007 #define ECHO_SERVER_PORT 10004 00008 00009 /* Debug port */ 00010 Serial pc(USBTX, USBRX); // USBSerial serial setting 00011 00012 /* GPIO setting */ 00013 DigitalOut led1(LED1); // on board green 00014 DigitalOut led2(LED2); // on board blue 00015 DigitalOut led3(LED3); // on board red 00016 00017 DigitalInOut OneAxisSliderPulse(PE_11, PIN_OUTPUT, OpenDrain, 0); 00018 DigitalInOut OneAxisSliderDir(PE_9, PIN_OUTPUT, OpenDrain, 0); 00019 // DigitalOut OneAxisSliderPulse(PE_9); 00020 //DigitalOut OneAxisSliderDir(PE_11); 00021 00022 DigitalOut OneAxisSliderORG(PF_3); 00023 DigitalOut OneAxisSliderReset(PF_15); 00024 DigitalOut OneAxisSliderServoOn(PF_11); 00025 00026 DigitalOut Relay(PC_8); 00027 00028 //DigitalInOut lamp_out(p23, PIN_OUTPUT, OpenDrain, 0); 00029 //DigitalInOut lamp_out(p23, PIN_OUTPUT, OpenDrain, 0); 00030 00031 00032 DigitalOut out1( PG_9 ); 00033 AnalogOut linear_out( PA_5 ); 00034 AnalogOut linear_out2( PA_4 ); 00035 00036 DigitalIn limit1_din(PG_5); // limit1 in 00037 DigitalIn limit2_din(PG_6); // limit2 in 00038 AnalogIn weight_ain(PC_2); // analog input of load cell 00039 00040 00041 /* Network setting */ 00042 UDPSocket userver; // UDP Server 00043 const char* ip_address = "192.168.31.31"; 00044 const char* subnet_mask = "255.255.255.0"; 00045 const char* gateway = "192.168.31.0"; 00046 00047 Thread thread1; 00048 Thread thread2; 00049 Thread thread3; 00050 Thread thread4; 00051 00052 void move_distance( int dir , int dis_mm ); 00053 00054 00055 /* Thread routine */ 00056 void OneAxisSliderCtrl_thread() 00057 { 00058 OneAxisSliderReset = 1; 00059 led2 = 1; 00060 00061 wait(1); 00062 led2 = 0; 00063 00064 OneAxisSliderReset = 0; 00065 00066 wait_ms(3000); 00067 00068 led3 = 1; 00069 printf("Servo on\r\n"); 00070 00071 // ORG-S: OFF 00072 00073 OneAxisSliderServoOn = 1; 00074 printf("Servo on\r\n"); 00075 00076 00077 00078 wait(1); 00079 OneAxisSliderORG = 1; 00080 printf("Move to Origin\r\n"); 00081 00082 // ************************************** 00083 // while( ORG-S singnal is ON(=0V)) 00084 // if slider is located to ORIGIN then OGG-S signal will be ON(0V) until next RESET or SERVO off 00085 // ORG-S: OFF 00086 // ************************************** 00087 00088 00089 wait_ms(15000); 00090 00091 wait(1); 00092 00093 printf("1st move\r\n"); 00094 move_distance( DIR_CCW, L_010mm * 30 ); 00095 00096 wait_ms(8000); 00097 00098 while (true) 00099 { 00100 while (true) 00101 { 00102 printf("CW move\r\n"); 00103 move_distance( DIR_CW, L_010mm * 20 ); 00104 00105 wait_ms(8000); 00106 00107 00108 00109 printf("CCW move\r\n"); 00110 move_distance( DIR_CCW, L_010mm * 20 ); 00111 00112 break; 00113 } 00114 wait(1); 00115 printf(" weight: %d\r\n", weight_ain.read_u16()); 00116 } 00117 } 00118 00119 00120 00121 00122 void move_distance( int dir , int dis_mm ) 00123 { 00124 OneAxisSliderDir = dir; 00125 wait(0.1); 00126 for( int i = 0; i < dis_mm; i++ ) 00127 { 00128 OneAxisSliderPulse = MOTOR_ON; 00129 wait_us(5); 00130 OneAxisSliderPulse = MOTOR_OFF; 00131 wait_us(5); 00132 } 00133 } 00134 00135 00136 00137 void led3_thread() 00138 { 00139 while (true) 00140 { 00141 Relay = 0; 00142 // Thread::wait(BLINKING_RATE_MS-20); 00143 Thread::wait( 100 ); 00144 Relay = 1; 00145 Thread::wait( 100 ); 00146 } 00147 } 00148 00149 void linear_thread() 00150 { 00151 while( true ) 00152 { 00153 for( double i=0.0; i<1.0; i+=0.001) { 00154 linear_out = i; 00155 Thread::wait(50); 00156 } 00157 } 00158 } 00159 00160 void network_thread() 00161 { 00162 EthernetInterface eth; 00163 eth.init( ip_address, subnet_mask, gateway ); //Use DHCP 00164 eth.connect(); 00165 printf("\nServer IP Address is %s\r\n", eth.getIPAddress()); 00166 00167 TCPSocketServer server; 00168 server.bind(ECHO_SERVER_PORT); 00169 server.listen(); 00170 00171 while (true) 00172 { 00173 printf("\nWait for new connection...\r\n"); 00174 TCPSocketConnection client; 00175 server.accept(client); 00176 client.set_blocking(false, 1500); // Timeout after (1.5)s 00177 00178 printf("Connection from: %s\r\n", client.get_address()); 00179 char buffer[256]; 00180 while (true) 00181 { 00182 int n = client.receive(buffer, sizeof(buffer)); 00183 if (n <= 0) break; 00184 00185 led1 = 1; 00186 // print received message to terminal 00187 buffer[n] = '\0'; 00188 printf("Received message from Client :'%s'\r\n",buffer); 00189 00190 if( !strcmp( buffer, "SLD_P0_0000" ) ) 00191 { 00192 printf("Command: SLD move to position 0\r\n"); 00193 } 00194 // Echo received message back to client 00195 client.send_all(buffer, n); 00196 if (n <= 0) break; 00197 } 00198 client.close(); 00199 } 00200 } 00201 00202 00203 00204 00205 00206 int main (void) { 00207 00208 pc.baud(115200); // Set UART(USB) baudrate 00209 DEBUG_PRINT_L0("\r\n"); 00210 DEBUG_PRINT_L0(" +--------------------------------------------------------------\r\n"); 00211 DEBUG_PRINT_L0(" | This is: Main Control Program of .....\r\n"); 00212 DEBUG_PRINT_L0(" +--------------------------------------------------------------\r\n"); 00213 00214 limit1_din.mode(PullUp); 00215 limit2_din.mode(PullUp); 00216 // Thread thread1(linear_thread, NULL, osPriorityNormal, 128 * 4); 00217 // Thread thread2(led2_thread, NULL, osPriorityNormal, 128 * 4); 00218 // Thread thread3(led3_thread, NULL, osPriorityNormal, 128 * 4); 00219 thread1.start(linear_thread); 00220 thread2.start(OneAxisSliderCtrl_thread); 00221 thread3.start(led3_thread); 00222 thread4.start(network_thread); 00223 00224 //while (true) 00225 //{ 00226 //led1 = !led1; 00227 //Thread::wait(1); 00228 //SliderPulse = !SliderPulse; 00229 00230 //} 00231 }
Generated on Sun Jul 17 2022 22:27:55 by
1.7.2