Ultrasound timeout demo
Dependencies: F7_Ethernet HCSR04 MQTT mbed
Fork of Nucleo_F746ZG_Ethernet by
main.cpp
00001 /* 00002 STM32 Nucleo MQTT Ultrasound Demonstration 00003 Author : Sam Walsh 00004 Date: 24/10/16 00005 Platform: STM32F746ZG NUCLEO (ARM Cortex M7 @ 216MHz) 00006 00007 Description: This code exercises the ethernet and digital IO on the F7 Nucleo 00008 by connecting to an MQTT broker automatically through the ethernet and then 00009 publishing values from a sensor (ultrasound sensor HCSR04) 00010 00011 Although not fully tested it is publishing values from the sensor to the MQTT broker 00012 (Raspberry Pi 3 connected on a 100Mb LAN) at a rate of about 10Hz, this is limited 00013 by the timeout functionality I have added to the default HCSR04 software - it usually 00014 waits for an echo indefinitely which leads to lags in program, so I added some timeouts (10ms) 00015 to each echo to stop that. These could be tweaked to gain more time I think. 00016 00017 I have benchmarked the Nucleo to be able to send messages to the Pi at a rate of about 16k per second 00018 with a payload of just "0.5", it is the ultrasound limiting the speed of transfer 00019 00020 <samuel.walsh@manchester.ac.uk> 00021 */ 00022 00023 #include "mbed.h" 00024 #include "rtos.h" 00025 #include "EthernetInterface.h" 00026 #include <stdio.h> 00027 #include "hcsr04.h" 00028 00029 #define MQTTCLIENT_QOS2 1 00030 #include "MQTTEthernet.h" 00031 #include "MQTTClient.h" 00032 00033 HCSR04 UltrasonicSensor_FrontLeft(D2,D3); //Trigger,Echo 00034 HCSR04 UltrasonicSensor_FrontCentre(D4,D5); //Trigger,Echo 00035 HCSR04 UltrasonicSensor_FrontRight(D6,D7); //Trigger,Echo 00036 00037 00038 HCSR04 UltrasonicSensor_BackLeft(PG_5,PG_6); //Trigger,Echo 00039 HCSR04 UltrasonicSensor_BackCentre(PE_0,PG_8); //Trigger,Echo 00040 HCSR04 UltrasonicSensor_BackRight(PF_15,PF_11); //Trigger,Echo 00041 Serial pc(USBTX, USBRX); 00042 00043 float distance; 00044 00045 EthernetInterface eth; 00046 00047 DigitalOut led1(LED1); 00048 00049 int arrivedcount = 0; 00050 00051 //Callback function for arriving messages 00052 void messageArrived(MQTT::MessageData& md) 00053 { 00054 MQTT::Message &message = md.message; 00055 printf("\n\rMessage arrived: qos %d, retained %d, dup %d, packetid %d\n", message.qos, message.retained, message.dup, message.id); 00056 printf("\n\rPayload %.*s\n", message.payloadlen, (char*)message.payload); 00057 ++arrivedcount; 00058 } 00059 00060 int main() 00061 { 00062 printf("\n\n*** Ethernet MQTT Demo for STM32F746ZG***\r\n"); 00063 printf("Starting to connect to Ethernet\r\n"); 00064 00065 MQTTEthernet ipstack = MQTTEthernet(); 00066 00067 MQTT::Client<MQTTEthernet, Countdown> client = MQTT::Client<MQTTEthernet, Countdown>(ipstack); 00068 00069 //This is just my MQTT broker, this could be anything// 00070 char* hostname = "192.168.1.2"; 00071 int port = 1883; 00072 printf("Connecting to %s:%d\n", hostname, port); 00073 int rc = ipstack.connect(hostname, port); 00074 if (rc != 0) 00075 printf("rc from TCP connect is %d\n", rc); 00076 00077 MQTTPacket_connectData data = MQTTPacket_connectData_initializer; 00078 00079 data.MQTTVersion = 3; 00080 data.clientID.cstring = "mbed-sample"; 00081 data.username.cstring = "testuser"; 00082 data.password.cstring = "testpassword"; 00083 00084 00085 //Connect to broker 00086 printf("Connecting to MQTT Broker\r\n"); 00087 if ((rc = client.connect(data)) != 0) 00088 printf("rc from MQTT connect is %d\n", rc); 00089 00090 00091 //Start building Message 00092 MQTT::Message message; 00093 // QoS 0 00094 char buf[100]; 00095 message.qos = MQTT::QOS0; 00096 message.retained = false; 00097 message.dup = false; 00098 00099 //Publish ultrasonic distance 00100 while(1) 00101 { 00102 //I have not fully benchmarked this loop but by eye it seems to be publishing sensor data about 10Hz TODO:Speed it up as much as possible and properly characterise sensor update rates 00103 distance = UltrasonicSensor_FrontLeft.distance(); 00104 wait_ms(10); 00105 if(distance == 17182){ 00106 //this is just 999999 error code ran through distance calc, i should change this but 00107 printf("\n\rOut of range -> echo timed out"); 00108 distance = 99999; 00109 } 00110 else{ 00111 printf("\n\rDistance is: %0.2f", distance); 00112 } 00113 00114 sprintf(buf, "%d", int(distance)); 00115 message.payload = (void*)buf; 00116 message.payloadlen = strlen(buf)+1; 00117 rc = client.publish("RobotSensors/Ultrasound/FrontLeft", message); 00118 00119 //Read Front Centre 00120 distance = UltrasonicSensor_FrontCentre.distance(); 00121 wait_ms(10); 00122 if(distance == 17182){ 00123 //this is just 999999 error code ran through distance calc, i should change this but 00124 printf("\n\rOut of range -> echo timed out"); 00125 distance = 99999; 00126 } 00127 else{ 00128 printf("\n\rDistance is: %0.2f", distance); 00129 } 00130 00131 sprintf(buf, "%d", int(distance)); 00132 message.payload = (void*)buf; 00133 message.payloadlen = strlen(buf)+1; 00134 rc = client.publish("RobotSensors/Ultrasound/FrontCentre", message); 00135 00136 //Read Front Right 00137 distance = UltrasonicSensor_FrontRight.distance(); 00138 wait_ms(10); 00139 if(distance == 17182){ 00140 //this is just 999999 error code ran through distance calc, i should change this but 00141 printf("\n\rOut of range -> echo timed out"); 00142 distance = 99999; 00143 } 00144 else{ 00145 printf("\n\rDistance is: %0.2f", distance); 00146 } 00147 00148 sprintf(buf, "%d", int(distance)); 00149 message.payload = (void*)buf; 00150 message.payloadlen = strlen(buf)+1; 00151 rc = client.publish("RobotSensors/Ultrasound/FrontRight", message); 00152 00153 00154 00155 distance = UltrasonicSensor_BackLeft.distance(); 00156 wait_ms(10); 00157 if(distance == 17182){ 00158 //this is just 999999 error code ran through distance calc, i should change this but 00159 printf("\n\rOut of range -> echo timed out"); 00160 distance = 99999; 00161 } 00162 else{ 00163 printf("\n\rDistance is: %0.2f", distance); 00164 } 00165 00166 sprintf(buf, "%d", int(distance)); 00167 message.payload = (void*)buf; 00168 message.payloadlen = strlen(buf)+1; 00169 rc = client.publish("RobotSensors/Ultrasound/BackLeft", message); 00170 00171 //Read Front Centre 00172 distance = UltrasonicSensor_BackCentre.distance(); 00173 wait_ms(10); 00174 if(distance == 17182){ 00175 //this is just 999999 error code ran through distance calc, i should change this but 00176 printf("\n\rOut of range -> echo timed out"); 00177 distance = 99999; 00178 } 00179 else{ 00180 printf("\n\rDistance is: %0.2f", distance); 00181 } 00182 00183 sprintf(buf, "%d", int(distance)); 00184 message.payload = (void*)buf; 00185 message.payloadlen = strlen(buf)+1; 00186 rc = client.publish("RobotSensors/Ultrasound/BackCentre", message); 00187 00188 //Read Front Right 00189 distance = UltrasonicSensor_BackRight.distance(); 00190 wait_ms(10); 00191 if(distance == 17182){ 00192 //this is just 999999 error code ran through distance calc, i should change this but 00193 printf("\n\rOut of range -> echo timed out"); 00194 distance = 99999; 00195 } 00196 else{ 00197 printf("\n\rDistance is: %0.2f", distance); 00198 } 00199 00200 sprintf(buf, "%d", int(distance)); 00201 message.payload = (void*)buf; 00202 message.payloadlen = strlen(buf)+1; 00203 rc = client.publish("RobotSensors/Ultrasound/BackRight", message); 00204 } 00205 //client.yield(100); 00206 // if ((rc = client.unsubscribe(topic)) != 0) 00207 // printf("rc from unsubscribe was %d\n", rc); 00208 // 00209 // if ((rc = client.disconnect()) != 0) 00210 // printf("rc from disconnect was %d\n", rc); 00211 // 00212 // ipstack.disconnect(); 00213 00214 00215 }
Generated on Tue Aug 2 2022 04:13:39 by 1.7.2