firmware for the miniv

Dependencies:   mbed EthernetNetIf JSON

Files at this revision

API Documentation at this revision

Comitter:
hakuturu583
Date:
Sat May 08 14:18:57 2021 +0000
Parent:
2:2eb3f4362288
Commit message:
use ticker to send pwm

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 2eb3f4362288 -r a0cd142687d0 main.cpp
--- a/main.cpp	Thu May 06 13:41:44 2021 +0000
+++ b/main.cpp	Sat May 08 14:18:57 2021 +0000
@@ -38,12 +38,13 @@
 
 // EthernetNetIf eth;
 EthernetNetIf eth(
-  IpAddr(192,168,0,25), //IP Address
+  IpAddr(192,168,10,100), //IP Address
   IpAddr(255,255,255,0), //Network Mask
   IpAddr(192,168,0,1), //Gateway
   IpAddr(192,168,0,1)  //DNS
 );
 
+#define TIMEOUT_SECONDS 5.0
 #define TCP_LISTENING_PORT 12345
 
 TCPSocket ListeningSock;
@@ -52,6 +53,9 @@
 TCPSocketErr err;
 float left_motor_thrust = 0.0;
 float right_motor_thrust = 0.0;
+Timer recieve_timer;
+Ticker pwm_ticker;
+float last_recieved_time = 0.0;
 
 float clamp(const float & value, const float & min = 0.0, const float & max = 1.0)
 {
@@ -98,6 +102,7 @@
                    if(left_value_index > 0 && right_value_index > 0) {
                        if(json.tokenNumberValue(left_value_index, left_motor_thrust) == 0 && 
                           json.tokenNumberValue(right_value_index, right_motor_thrust) == 0) {
+                              last_recieved_time = recieve_timer.read();
                               succees = true;
                        }
                    }
@@ -192,6 +197,18 @@
      };
 }
 
+void sendPwm()
+{
+  if(last_recieved_time - recieve_timer.read() > TIMEOUT_SECONDS) {
+    left_motor_thrust = 0.0;
+    right_motor_thrust = 0.0;
+  }
+  led1 = 0.5 + clamp(left_motor_thrust, -1, 1) * 0.5;
+  led4 = 0.5 + clamp(right_motor_thrust, -1, 1) * 0.5;
+  left_thrust_pin = 0.75 + clamp(left_motor_thrust, -1, 1) * 0.2;
+  right_thrust_pin = 0.75 + clamp(right_motor_thrust, -1, 1) * 0.2;
+}
+
 int main() {
   led1.period(0.002);
   led4.period(0.002);
@@ -219,33 +236,19 @@
   printf("Binding..\r\n");
   if(err)
   {
-   //Deal with that error...
+    //Deal with that error...
     printf("Binding Error\n");
   }
-   err=ListeningSock.listen(); // Starts listening
-   printf("Listening...\r\n");
-   if(err)
-   {
+  err=ListeningSock.listen(); // Starts listening
+  printf("Listening...\r\n");
+  if(err)
+  {
     printf("Listening Error\r\n");
-   }
- 
-  Timer tmr;
-  tmr.start();
-  float timer_count = 0.0;
-
+  }
+  recieve_timer.start();
+  pwm_ticker.attach(&sendPwm, 0.01);
   while(true)
   {
     Net::poll();
-    if(tmr.read() > 0.001) // sec
-    {
-      timer_count = timer_count + 0.001;
-      if(timer_count >= 1.0) {
-          timer_count = 0.0;
-          led1 = 0.5 + clamp(left_motor_thrust, -1, 1) * 0.5;
-          led4 = 0.5 + clamp(right_motor_thrust, -1, 1) * 0.5;
-          left_thrust_pin = 0.75 + clamp(left_motor_thrust, -1, 1) * 0.2;
-          right_thrust_pin = 0.75 + clamp(right_motor_thrust, -1, 1) * 0.2;
-      }
-    }
   }  
 }