ああ
Dependencies: F7_Ethernet mbed mbed-rtos
main.cpp@9:a4cc7dc2d92f, 2020-09-14 (annotated)
- Committer:
- sayzyas
- Date:
- Mon Sep 14 06:38:16 2020 +0000
- Revision:
- 9:a4cc7dc2d92f
- Parent:
- 8:23b1fba109b0
1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 0:38cbb854d85f | 1 | #include "mbed.h" |
sayzyas | 9:a4cc7dc2d92f | 2 | #include "rtos.h" |
emilmont | 1:5cebe0e38cd2 | 3 | #include "EthernetInterface.h" |
sayzyas | 9:a4cc7dc2d92f | 4 | #include "common.h" |
sayzyas | 9:a4cc7dc2d92f | 5 | #include "stdio.h" |
mbed_official | 8:23b1fba109b0 | 6 | |
sayzyas | 9:a4cc7dc2d92f | 7 | #define ECHO_SERVER_PORT 10004 |
sayzyas | 9:a4cc7dc2d92f | 8 | |
sayzyas | 9:a4cc7dc2d92f | 9 | /* Debug port */ |
sayzyas | 9:a4cc7dc2d92f | 10 | Serial pc(USBTX, USBRX); // USBSerial serial setting |
sayzyas | 9:a4cc7dc2d92f | 11 | |
sayzyas | 9:a4cc7dc2d92f | 12 | /* GPIO setting */ |
sayzyas | 9:a4cc7dc2d92f | 13 | DigitalOut led1(LED1); // on board green |
sayzyas | 9:a4cc7dc2d92f | 14 | DigitalOut led2(LED2); // on board blue |
sayzyas | 9:a4cc7dc2d92f | 15 | DigitalOut led3(LED3); // on board red |
sayzyas | 9:a4cc7dc2d92f | 16 | |
sayzyas | 9:a4cc7dc2d92f | 17 | DigitalInOut OneAxisSliderPulse(PE_11, PIN_OUTPUT, OpenDrain, 0); |
sayzyas | 9:a4cc7dc2d92f | 18 | DigitalInOut OneAxisSliderDir(PE_9, PIN_OUTPUT, OpenDrain, 0); |
sayzyas | 9:a4cc7dc2d92f | 19 | // DigitalOut OneAxisSliderPulse(PE_9); |
sayzyas | 9:a4cc7dc2d92f | 20 | //DigitalOut OneAxisSliderDir(PE_11); |
sayzyas | 9:a4cc7dc2d92f | 21 | |
sayzyas | 9:a4cc7dc2d92f | 22 | DigitalOut OneAxisSliderORG(PF_3); |
sayzyas | 9:a4cc7dc2d92f | 23 | DigitalOut OneAxisSliderReset(PF_15); |
sayzyas | 9:a4cc7dc2d92f | 24 | DigitalOut OneAxisSliderServoOn(PF_11); |
sayzyas | 9:a4cc7dc2d92f | 25 | |
sayzyas | 9:a4cc7dc2d92f | 26 | DigitalOut Relay(PC_8); |
sayzyas | 9:a4cc7dc2d92f | 27 | |
sayzyas | 9:a4cc7dc2d92f | 28 | //DigitalInOut lamp_out(p23, PIN_OUTPUT, OpenDrain, 0); |
sayzyas | 9:a4cc7dc2d92f | 29 | //DigitalInOut lamp_out(p23, PIN_OUTPUT, OpenDrain, 0); |
sayzyas | 9:a4cc7dc2d92f | 30 | |
sayzyas | 9:a4cc7dc2d92f | 31 | |
sayzyas | 9:a4cc7dc2d92f | 32 | DigitalOut out1( PG_9 ); |
sayzyas | 9:a4cc7dc2d92f | 33 | AnalogOut linear_out( PA_5 ); |
sayzyas | 9:a4cc7dc2d92f | 34 | AnalogOut linear_out2( PA_4 ); |
sayzyas | 9:a4cc7dc2d92f | 35 | |
sayzyas | 9:a4cc7dc2d92f | 36 | DigitalIn limit1_din(PG_5); // limit1 in |
sayzyas | 9:a4cc7dc2d92f | 37 | DigitalIn limit2_din(PG_6); // limit2 in |
sayzyas | 9:a4cc7dc2d92f | 38 | AnalogIn weight_ain(PC_2); // analog input of load cell |
sayzyas | 9:a4cc7dc2d92f | 39 | |
sayzyas | 9:a4cc7dc2d92f | 40 | |
sayzyas | 9:a4cc7dc2d92f | 41 | /* Network setting */ |
sayzyas | 9:a4cc7dc2d92f | 42 | UDPSocket userver; // UDP Server |
sayzyas | 9:a4cc7dc2d92f | 43 | const char* ip_address = "192.168.31.31"; |
sayzyas | 9:a4cc7dc2d92f | 44 | const char* subnet_mask = "255.255.255.0"; |
sayzyas | 9:a4cc7dc2d92f | 45 | const char* gateway = "192.168.31.0"; |
sayzyas | 9:a4cc7dc2d92f | 46 | |
sayzyas | 9:a4cc7dc2d92f | 47 | Thread thread1; |
sayzyas | 9:a4cc7dc2d92f | 48 | Thread thread2; |
sayzyas | 9:a4cc7dc2d92f | 49 | Thread thread3; |
sayzyas | 9:a4cc7dc2d92f | 50 | Thread thread4; |
sayzyas | 9:a4cc7dc2d92f | 51 | |
sayzyas | 9:a4cc7dc2d92f | 52 | void move_distance( int dir , int dis_mm ); |
sayzyas | 9:a4cc7dc2d92f | 53 | |
mbed_official | 8:23b1fba109b0 | 54 | |
sayzyas | 9:a4cc7dc2d92f | 55 | /* Thread routine */ |
sayzyas | 9:a4cc7dc2d92f | 56 | void OneAxisSliderCtrl_thread() |
sayzyas | 9:a4cc7dc2d92f | 57 | { |
sayzyas | 9:a4cc7dc2d92f | 58 | OneAxisSliderReset = 1; |
sayzyas | 9:a4cc7dc2d92f | 59 | led2 = 1; |
sayzyas | 9:a4cc7dc2d92f | 60 | |
sayzyas | 9:a4cc7dc2d92f | 61 | wait(1); |
sayzyas | 9:a4cc7dc2d92f | 62 | led2 = 0; |
sayzyas | 9:a4cc7dc2d92f | 63 | |
sayzyas | 9:a4cc7dc2d92f | 64 | OneAxisSliderReset = 0; |
sayzyas | 9:a4cc7dc2d92f | 65 | |
sayzyas | 9:a4cc7dc2d92f | 66 | wait_ms(3000); |
sayzyas | 9:a4cc7dc2d92f | 67 | |
sayzyas | 9:a4cc7dc2d92f | 68 | led3 = 1; |
sayzyas | 9:a4cc7dc2d92f | 69 | printf("Servo on\r\n"); |
sayzyas | 9:a4cc7dc2d92f | 70 | |
sayzyas | 9:a4cc7dc2d92f | 71 | // ORG-S: OFF |
sayzyas | 9:a4cc7dc2d92f | 72 | |
sayzyas | 9:a4cc7dc2d92f | 73 | OneAxisSliderServoOn = 1; |
sayzyas | 9:a4cc7dc2d92f | 74 | printf("Servo on\r\n"); |
sayzyas | 9:a4cc7dc2d92f | 75 | |
sayzyas | 9:a4cc7dc2d92f | 76 | |
sayzyas | 9:a4cc7dc2d92f | 77 | |
sayzyas | 9:a4cc7dc2d92f | 78 | wait(1); |
sayzyas | 9:a4cc7dc2d92f | 79 | OneAxisSliderORG = 1; |
sayzyas | 9:a4cc7dc2d92f | 80 | printf("Move to Origin\r\n"); |
sayzyas | 9:a4cc7dc2d92f | 81 | |
sayzyas | 9:a4cc7dc2d92f | 82 | // ************************************** |
sayzyas | 9:a4cc7dc2d92f | 83 | // while( ORG-S singnal is ON(=0V)) |
sayzyas | 9:a4cc7dc2d92f | 84 | // if slider is located to ORIGIN then OGG-S signal will be ON(0V) until next RESET or SERVO off |
sayzyas | 9:a4cc7dc2d92f | 85 | // ORG-S: OFF |
sayzyas | 9:a4cc7dc2d92f | 86 | // ************************************** |
sayzyas | 9:a4cc7dc2d92f | 87 | |
sayzyas | 9:a4cc7dc2d92f | 88 | |
sayzyas | 9:a4cc7dc2d92f | 89 | wait_ms(15000); |
sayzyas | 9:a4cc7dc2d92f | 90 | |
sayzyas | 9:a4cc7dc2d92f | 91 | wait(1); |
sayzyas | 9:a4cc7dc2d92f | 92 | |
sayzyas | 9:a4cc7dc2d92f | 93 | printf("1st move\r\n"); |
sayzyas | 9:a4cc7dc2d92f | 94 | move_distance( DIR_CCW, L_010mm * 30 ); |
sayzyas | 9:a4cc7dc2d92f | 95 | |
sayzyas | 9:a4cc7dc2d92f | 96 | wait_ms(8000); |
sayzyas | 9:a4cc7dc2d92f | 97 | |
sayzyas | 9:a4cc7dc2d92f | 98 | while (true) |
sayzyas | 9:a4cc7dc2d92f | 99 | { |
sayzyas | 9:a4cc7dc2d92f | 100 | while (true) |
sayzyas | 9:a4cc7dc2d92f | 101 | { |
sayzyas | 9:a4cc7dc2d92f | 102 | printf("CW move\r\n"); |
sayzyas | 9:a4cc7dc2d92f | 103 | move_distance( DIR_CW, L_010mm * 20 ); |
sayzyas | 9:a4cc7dc2d92f | 104 | |
sayzyas | 9:a4cc7dc2d92f | 105 | wait_ms(8000); |
sayzyas | 9:a4cc7dc2d92f | 106 | |
sayzyas | 9:a4cc7dc2d92f | 107 | |
sayzyas | 9:a4cc7dc2d92f | 108 | |
sayzyas | 9:a4cc7dc2d92f | 109 | printf("CCW move\r\n"); |
sayzyas | 9:a4cc7dc2d92f | 110 | move_distance( DIR_CCW, L_010mm * 20 ); |
sayzyas | 9:a4cc7dc2d92f | 111 | |
sayzyas | 9:a4cc7dc2d92f | 112 | break; |
sayzyas | 9:a4cc7dc2d92f | 113 | } |
sayzyas | 9:a4cc7dc2d92f | 114 | wait(1); |
sayzyas | 9:a4cc7dc2d92f | 115 | printf(" weight: %d\r\n", weight_ain.read_u16()); |
sayzyas | 9:a4cc7dc2d92f | 116 | } |
sayzyas | 9:a4cc7dc2d92f | 117 | } |
sayzyas | 9:a4cc7dc2d92f | 118 | |
sayzyas | 9:a4cc7dc2d92f | 119 | |
sayzyas | 9:a4cc7dc2d92f | 120 | |
sayzyas | 9:a4cc7dc2d92f | 121 | |
sayzyas | 9:a4cc7dc2d92f | 122 | void move_distance( int dir , int dis_mm ) |
sayzyas | 9:a4cc7dc2d92f | 123 | { |
sayzyas | 9:a4cc7dc2d92f | 124 | OneAxisSliderDir = dir; |
sayzyas | 9:a4cc7dc2d92f | 125 | wait(0.1); |
sayzyas | 9:a4cc7dc2d92f | 126 | for( int i = 0; i < dis_mm; i++ ) |
sayzyas | 9:a4cc7dc2d92f | 127 | { |
sayzyas | 9:a4cc7dc2d92f | 128 | OneAxisSliderPulse = MOTOR_ON; |
sayzyas | 9:a4cc7dc2d92f | 129 | wait_us(5); |
sayzyas | 9:a4cc7dc2d92f | 130 | OneAxisSliderPulse = MOTOR_OFF; |
sayzyas | 9:a4cc7dc2d92f | 131 | wait_us(5); |
sayzyas | 9:a4cc7dc2d92f | 132 | } |
sayzyas | 9:a4cc7dc2d92f | 133 | } |
sayzyas | 9:a4cc7dc2d92f | 134 | |
sayzyas | 9:a4cc7dc2d92f | 135 | |
sayzyas | 9:a4cc7dc2d92f | 136 | |
sayzyas | 9:a4cc7dc2d92f | 137 | void led3_thread() |
sayzyas | 9:a4cc7dc2d92f | 138 | { |
sayzyas | 9:a4cc7dc2d92f | 139 | while (true) |
sayzyas | 9:a4cc7dc2d92f | 140 | { |
sayzyas | 9:a4cc7dc2d92f | 141 | Relay = 0; |
sayzyas | 9:a4cc7dc2d92f | 142 | // Thread::wait(BLINKING_RATE_MS-20); |
sayzyas | 9:a4cc7dc2d92f | 143 | Thread::wait( 100 ); |
sayzyas | 9:a4cc7dc2d92f | 144 | Relay = 1; |
sayzyas | 9:a4cc7dc2d92f | 145 | Thread::wait( 100 ); |
sayzyas | 9:a4cc7dc2d92f | 146 | } |
sayzyas | 9:a4cc7dc2d92f | 147 | } |
sayzyas | 9:a4cc7dc2d92f | 148 | |
sayzyas | 9:a4cc7dc2d92f | 149 | void linear_thread() |
sayzyas | 9:a4cc7dc2d92f | 150 | { |
sayzyas | 9:a4cc7dc2d92f | 151 | while( true ) |
sayzyas | 9:a4cc7dc2d92f | 152 | { |
sayzyas | 9:a4cc7dc2d92f | 153 | for( double i=0.0; i<1.0; i+=0.001) { |
sayzyas | 9:a4cc7dc2d92f | 154 | linear_out = i; |
sayzyas | 9:a4cc7dc2d92f | 155 | Thread::wait(50); |
sayzyas | 9:a4cc7dc2d92f | 156 | } |
sayzyas | 9:a4cc7dc2d92f | 157 | } |
sayzyas | 9:a4cc7dc2d92f | 158 | } |
sayzyas | 9:a4cc7dc2d92f | 159 | |
sayzyas | 9:a4cc7dc2d92f | 160 | void network_thread() |
sayzyas | 9:a4cc7dc2d92f | 161 | { |
emilmont | 1:5cebe0e38cd2 | 162 | EthernetInterface eth; |
sayzyas | 9:a4cc7dc2d92f | 163 | eth.init( ip_address, subnet_mask, gateway ); //Use DHCP |
emilmont | 1:5cebe0e38cd2 | 164 | eth.connect(); |
sayzyas | 9:a4cc7dc2d92f | 165 | printf("\nServer IP Address is %s\r\n", eth.getIPAddress()); |
emilmont | 1:5cebe0e38cd2 | 166 | |
emilmont | 1:5cebe0e38cd2 | 167 | TCPSocketServer server; |
emilmont | 3:36fd3cfad85a | 168 | server.bind(ECHO_SERVER_PORT); |
emilmont | 3:36fd3cfad85a | 169 | server.listen(); |
sayzyas | 9:a4cc7dc2d92f | 170 | |
sayzyas | 9:a4cc7dc2d92f | 171 | while (true) |
sayzyas | 9:a4cc7dc2d92f | 172 | { |
sayzyas | 9:a4cc7dc2d92f | 173 | printf("\nWait for new connection...\r\n"); |
emilmont | 1:5cebe0e38cd2 | 174 | TCPSocketConnection client; |
emilmont | 1:5cebe0e38cd2 | 175 | server.accept(client); |
emilmont | 3:36fd3cfad85a | 176 | client.set_blocking(false, 1500); // Timeout after (1.5)s |
emilmont | 1:5cebe0e38cd2 | 177 | |
sayzyas | 9:a4cc7dc2d92f | 178 | printf("Connection from: %s\r\n", client.get_address()); |
emilmont | 1:5cebe0e38cd2 | 179 | char buffer[256]; |
sayzyas | 9:a4cc7dc2d92f | 180 | while (true) |
sayzyas | 9:a4cc7dc2d92f | 181 | { |
emilmont | 3:36fd3cfad85a | 182 | int n = client.receive(buffer, sizeof(buffer)); |
emilmont | 1:5cebe0e38cd2 | 183 | if (n <= 0) break; |
emilmont | 1:5cebe0e38cd2 | 184 | |
sayzyas | 9:a4cc7dc2d92f | 185 | led1 = 1; |
mbedAustin | 7:a5ead1402704 | 186 | // print received message to terminal |
mbedAustin | 7:a5ead1402704 | 187 | buffer[n] = '\0'; |
sayzyas | 9:a4cc7dc2d92f | 188 | printf("Received message from Client :'%s'\r\n",buffer); |
mbedAustin | 7:a5ead1402704 | 189 | |
sayzyas | 9:a4cc7dc2d92f | 190 | if( !strcmp( buffer, "SLD_P0_0000" ) ) |
sayzyas | 9:a4cc7dc2d92f | 191 | { |
sayzyas | 9:a4cc7dc2d92f | 192 | printf("Command: SLD move to position 0\r\n"); |
sayzyas | 9:a4cc7dc2d92f | 193 | } |
mbedAustin | 7:a5ead1402704 | 194 | // Echo received message back to client |
emilmont | 2:ec5ae99791da | 195 | client.send_all(buffer, n); |
emilmont | 1:5cebe0e38cd2 | 196 | if (n <= 0) break; |
emilmont | 1:5cebe0e38cd2 | 197 | } |
emilmont | 1:5cebe0e38cd2 | 198 | client.close(); |
emilmont | 1:5cebe0e38cd2 | 199 | } |
emilmont | 1:5cebe0e38cd2 | 200 | } |
sayzyas | 9:a4cc7dc2d92f | 201 | |
sayzyas | 9:a4cc7dc2d92f | 202 | |
sayzyas | 9:a4cc7dc2d92f | 203 | |
sayzyas | 9:a4cc7dc2d92f | 204 | |
sayzyas | 9:a4cc7dc2d92f | 205 | |
sayzyas | 9:a4cc7dc2d92f | 206 | int main (void) { |
sayzyas | 9:a4cc7dc2d92f | 207 | |
sayzyas | 9:a4cc7dc2d92f | 208 | pc.baud(115200); // Set UART(USB) baudrate |
sayzyas | 9:a4cc7dc2d92f | 209 | DEBUG_PRINT_L0("\r\n"); |
sayzyas | 9:a4cc7dc2d92f | 210 | DEBUG_PRINT_L0(" +--------------------------------------------------------------\r\n"); |
sayzyas | 9:a4cc7dc2d92f | 211 | DEBUG_PRINT_L0(" | This is: Main Control Program of .....\r\n"); |
sayzyas | 9:a4cc7dc2d92f | 212 | DEBUG_PRINT_L0(" +--------------------------------------------------------------\r\n"); |
sayzyas | 9:a4cc7dc2d92f | 213 | |
sayzyas | 9:a4cc7dc2d92f | 214 | limit1_din.mode(PullUp); |
sayzyas | 9:a4cc7dc2d92f | 215 | limit2_din.mode(PullUp); |
sayzyas | 9:a4cc7dc2d92f | 216 | // Thread thread1(linear_thread, NULL, osPriorityNormal, 128 * 4); |
sayzyas | 9:a4cc7dc2d92f | 217 | // Thread thread2(led2_thread, NULL, osPriorityNormal, 128 * 4); |
sayzyas | 9:a4cc7dc2d92f | 218 | // Thread thread3(led3_thread, NULL, osPriorityNormal, 128 * 4); |
sayzyas | 9:a4cc7dc2d92f | 219 | thread1.start(linear_thread); |
sayzyas | 9:a4cc7dc2d92f | 220 | thread2.start(OneAxisSliderCtrl_thread); |
sayzyas | 9:a4cc7dc2d92f | 221 | thread3.start(led3_thread); |
sayzyas | 9:a4cc7dc2d92f | 222 | thread4.start(network_thread); |
sayzyas | 9:a4cc7dc2d92f | 223 | |
sayzyas | 9:a4cc7dc2d92f | 224 | //while (true) |
sayzyas | 9:a4cc7dc2d92f | 225 | //{ |
sayzyas | 9:a4cc7dc2d92f | 226 | //led1 = !led1; |
sayzyas | 9:a4cc7dc2d92f | 227 | //Thread::wait(1); |
sayzyas | 9:a4cc7dc2d92f | 228 | //SliderPulse = !SliderPulse; |
sayzyas | 9:a4cc7dc2d92f | 229 | |
sayzyas | 9:a4cc7dc2d92f | 230 | //} |
sayzyas | 9:a4cc7dc2d92f | 231 | } |