Mode 3

Dependencies:   C12832_lcd EthernetInterface LM75B MMA7660 libxively mbed-rtos mbed

Fork of Mode_3_MBED by Zain Ijaz

Files at this revision

API Documentation at this revision

Comitter:
bhakti08
Date:
Tue Jun 10 00:24:35 2014 +0000
Parent:
15:53f65089126b
Commit message:
Mode 3 final;

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Tue Jun 10 00:05:34 2014 +0000
+++ b/main.cpp	Tue Jun 10 00:24:35 2014 +0000
@@ -13,12 +13,24 @@
 #include "LM75B.h"
 #include "C12832_lcd.h"
 #define ECHO_SERVER_PORT   7
+#include <string.h>
+ 
+#define FWD 3
+#define REV 4
+#define LEFT 1
+#define RIGHT 2
+#define STOP 0
+#define STRAIGHT_WHEEL 5
+#define servo_1 6
+#define servo_2 7
+
 MMA7660 axl(p28, p27);
 //LM75B tmp(p28, p27);
 AnalogIn tmp(p19);
 C12832_LCD lcd;
 DigitalOut cL(LED1);
 Serial pc(USBTX,USBRX);
+BusOut motor(p5,p6,p7);
 #include "logo.h"
 
 int main() {
@@ -39,106 +51,76 @@
         
         pc.printf("Connection from: %s\n", client.get_address());
         char buffer[256];
-        while (true) {
-            //int n = client.receive(buffer, sizeof(buffer));
-            //if (n <= 0) break;
-            
-            //client.send_all(buffer, n);
-            //if (n <= 0) break;
+            motor = STOP;
+            int n = client.receive(buffer, sizeof(buffer));
+            if (n <= 0) continue;
+            buffer[n] = 0;
+            pc.printf("String is : %s\r\n",buffer);
+ 
+            client.send_all(buffer, n);
+            if (!(strcmp (buffer, "w")))
+                motor = FWD;
+            else if (!(strcmp(buffer,"x")))
+                motor = REV;
+            else if (!(strcmp(buffer,"z")))
+                motor = STRAIGHT_WHEEL;
+            else if (!(strcmp(buffer,"d")))
+                motor = RIGHT;
+            else if (!(strcmp(buffer,"a")))
+                motor = LEFT;
+            else if (!(strcmp(buffer,"s")))
+                motor = STOP;
+            else if (!(strcmp(buffer,"o")))
+                motor = servo_1;
+            else if (!(strcmp(buffer,"p")))
+                motor = servo_2;
               
     pc.printf("Received:%s", buffer);
     
     xi_feed_t feed;
-    memset( &feed, NULL, sizeof( xi_feed_t ) );
-    
-    feed.feed_id = XI_FEED_ID;
-    feed.datastream_count = 4;
-    
-    feed.datastreams[0].datapoint_count = 1;
-    xi_datastream_t* orientation_datastream = &feed.datastreams[0];
-    strcpy( orientation_datastream->datastream_id, "orientation" );
-    xi_datapoint_t* current_orientation = &orientation_datastream->datapoints[0];
-
-    feed.datastreams[1].datapoint_count = 1;
-    xi_datastream_t* side_rotation_datastream = &feed.datastreams[1];
-    strcpy( side_rotation_datastream->datastream_id, "side_rotation" );
-    xi_datapoint_t* current_side_rotation = &side_rotation_datastream->datapoints[0];
+        memset( &feed, NULL, sizeof( xi_feed_t ) );
     
-    feed.datastreams[2].datapoint_count = 1;
-    xi_datastream_t* temperature_datastream = &feed.datastreams[2];
-    strcpy( temperature_datastream->datastream_id, "temperature" );
-    xi_datapoint_t* current_temperature = &temperature_datastream->datapoints[0];
-    
-    feed.datastreams[3].datapoint_count = 1;
-    xi_datastream_t* control_datastream = &feed.datastreams[3];
-    strcpy( control_datastream->datastream_id, "System" );
-    xi_datapoint_t* current_state = &control_datastream->datapoints[0];
-    
-    // create the cosm library context
-    xi_context_t* xi_context = xi_create_context( XI_HTTP, XI_API_KEY, feed.feed_id );
-
-    // check if everything works
-    if( xi_context == NULL )
-    {
-        return -1;
-    }
-
+        feed.feed_id = XI_FEED_ID;
+        feed.datastream_count = 4;
     
-      
-      switch( axl.getSide() ) {
-        case MMA7660::Front:
-          xi_set_value_str( current_side_rotation, "front" );
-          break;
-        case MMA7660::Back:
-          xi_set_value_str( current_side_rotation, "back" );
-          break;
-        default:
-          xi_set_value_str( current_side_rotation, "unknown" );
-          break;
-      }
-      
-      switch( axl.getOrientation() ) {
-        case MMA7660::Down:
-          xi_set_value_str( current_orientation, "down" );
-          break;
-        case MMA7660::Up:
-           xi_set_value_str( current_orientation, "up" );
-           break;
-        case MMA7660::Right:
-          xi_set_value_str( current_orientation, "right" );
-          break;
-        case MMA7660::Left:
-          xi_set_value_str( current_orientation, "left" );
-          break;
-        default: 
-          xi_set_value_str( current_orientation, "unknown" );
-          break;
-      }
-      
-       xi_set_value_f32( current_temperature, tmp.read() );
-       xi_datastream_get( xi_context, feed.feed_id
-                    , control_datastream->datastream_id
-                    , control_datastream->datapoints);      
-                        current_state = &control_datastream->datapoints[0];   
-      int system_status = current_state->value.i32_value;
-      
-      if (system_status == 0)
-       {
-           cL = 0;
+        feed.datastreams[0].datapoint_count = 1;
+        xi_datastream_t* temperature_datastream = &feed.datastreams[0];
+        strcpy( temperature_datastream->datastream_id, "Temperature" );
+        xi_datapoint_t* current_temperature = &temperature_datastream->datapoints[0];
+        
+        feed.datastreams[1].datapoint_count = 1;
+        xi_datastream_t* x_axis_datastream = &feed.datastreams[1];
+        strcpy( x_axis_datastream->datastream_id, "X_axis" );
+        xi_datapoint_t* accel_x = &x_axis_datastream->datapoints[0];
+        
+        feed.datastreams[2].datapoint_count = 1;
+        xi_datastream_t* y_axis_datastream = &feed.datastreams[2];
+        strcpy( y_axis_datastream->datastream_id, "Y_axis" );
+        xi_datapoint_t* accel_y = &y_axis_datastream->datapoints[0];
+        
+        feed.datastreams[3].datapoint_count = 1;
+        xi_datastream_t* z_axis_datastream = &feed.datastreams[3];
+        strcpy( z_axis_datastream->datastream_id, "Z_axis" );
+        xi_datapoint_t* accel_z = &z_axis_datastream->datapoints[0];
+        
+        xi_context_t* xi_context
+            = xi_create_context( XI_HTTP, XI_API_KEY, feed.feed_id );
+        if( xi_context == NULL )
+        {
+            pc.printf("Error in Xi_Context\r\n");
+            exit (0);
         }
-      if (system_status == 1)
-       {
-           cL = 1;
-        }
-        
-      lcd_printf( "update...\n" );
-      xi_feed_update( xi_context, &feed );
-      lcd_printf( "done...\n" );
-      
-      
-      }
-        client.close();
+ 
+        pc.printf("In xively thread\r\n");
+        xi_set_value_f32( current_temperature, tmp.read() ); 
+        xi_set_value_f32( accel_x ,axl.x() );
+        xi_set_value_f32( accel_y ,axl.y() );
+        xi_set_value_f32( accel_z ,axl.z() );
+        pc.printf("Value set\r\n");
+        xi_feed_update( xi_context, &feed );
+        pc.printf("Update\r\n");
+     //client.close();
         
  }
    
-}
+}
\ No newline at end of file