code

Dependencies:   mbed ros_lib_kinetic

Files at this revision

API Documentation at this revision

Comitter:
BenRJG
Date:
Thu Dec 12 19:51:42 2019 +0000
Parent:
8:639e4bfb65b0
Commit message:
Ben's Guidelines for code modification

Changed in this revision

TOFPublisher.cpp Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
publisherExample.cpp Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TOFPublisher.cpp	Thu Dec 12 19:51:42 2019 +0000
@@ -0,0 +1,86 @@
+#include "mbed.h"
+#include <std_msgs/Empty.h>
+#include <ros.h>
+#include <std_msgs/UInt8.h>
+
+//#include "VL6180.h"
+#include "timeofflight.hpp"
+///////////////////////////////////////////////////////////////////
+// Beginning of code
+///////////////////////////////////////////////////////////////////
+Serial pc(USBTX,USBRX);
+//VL6180 distance_sensor (SDA,SCL);
+//Serial pc(SERIAL_TX, SERIAL_RX);// set-up serial to pc
+            // I²C address of VL6180 shifted by 1 bit
+
+VL6180 tof1Data(0x52);
+/* VL6180 tof2Data(0x53);
+VL6180 tof3Data(0x54);
+VL6180 tof4Data(0x55);
+*/
+
+DigitalOut myled(LED1);
+
+ros::NodeHandle  nh;
+
+std_msgs::UInt8 Tof1;
+ros::Publisher ("tof1", &Tof1);
+
+std_msgs::UInt8 Tof2;
+ros::Publisher tof2("tof2", &Tof2);
+
+std_msgs::UInt8 Tof3;
+ros::Publisher tof3("tof3", &Tof3);
+
+std_msgs::UInt8 Tof4;
+ros::Publisher tof4("tof4", &Tof4);
+
+//!!This needs to be a subscriber not publisher!!//
+//std_msgs::UInt8 received_speed;
+//ros::Publisher speed_publisher("speed_publisher", &received_speed);
+
+std_msgs::UInt8 the_motor_speed;
+
+void messageCb(const std_msgs::UInt8& speed){
+    the_motor_speed = speed;
+}
+ros::Subscriber<std_msgs::UInt8> sub("motor_speed", &messageCb);
+
+//void messageCb(const std_msgs::Empty& toggle_msg){
+//    myled = !myled;   // blink the led
+//}
+//ros::Subscriber<std_msgs::Empty> sub("toggle_led", &messageCb);
+
+int main() {
+////2222 -----------------------------------2222////
+    nh.initNode();
+    nh.advertise(tof1);
+    nh.advertise(tof2);
+    nh.advertise(tof3);
+    nh.advertise(tof4);
+    //!!nh.advertise(speed_publisher);
+    
+    //!!This is the subscriber, so setup the speed_publisher as speed_subscriber here!!//
+    nh.subscribe(sub);
+    
+    tof1Data.init();
+////2222 -----------------------------------2222////
+   
+    while (1) {
+////1111 put this in the matching spot in the main file 1111////
+        Tof1.data = tof1Data.Get_Range();
+        tof1.publish( &Tof1 );
+        Tof2.data = 6;
+        tof2.publish( &Tof2 );
+        Tof3.data = 7;
+        tof3.publish( &Tof3);
+        Tof4.data = 8;
+        tof4.publish( &Tof4);
+        //!! speed_publisher.publish(&the_motor_speed);
+////1111 ---------------------------------------------- 1111////
+        nh.spinOnce();
+        wait_ms(1000);
+    }
+}
+
+
--- a/main.cpp	Tue Oct 22 10:21:34 2019 +0000
+++ b/main.cpp	Thu Dec 12 19:51:42 2019 +0000
@@ -1,49 +1,42 @@
-// Changed text
-
-///////////////////////////////////////////////////////////////////
-// Beginning of code
-///////////////////////////////////////////////////////////////////
-#include "timeofflight.hpp"
-
-Serial pc(SERIAL_TX, SERIAL_RX);// set-up serial to pc
-#define addr (0x52)             // I²C address of VL6180 shifted by 1 bit
-
-VL6180 tof1(addr);
+#include "ros/ros.h"
+#include "std_msgs/String.h"
 
-#include <ros.h>
-#include <std_msgs/String.h>
-
-ros::NodeHandle  node_chatter;
+class dumbotControl
+{
+public:
+    dumbotControl()
+    {
+        //Node handle here
+        
+        //Subscriber setup here
+        //Publihser setup here
+        ////2222 -----------------------------------2222////
+    }
 
-std_msgs::String str_msg;
-ros::Publisher chatter("chatter", &str_msg);
-
-char data[13];
-
-
+    void chatterCallback(const sensor_msgs::Joy::ConstPtr& input) // Change this variable for inputs
+    {
+        //Code to run initially when subscriber recieves data
+        ////1111 ---------------------------------------------- 1111////
+    }
 
-///////////////////////////////////////////////////////////////////
-// Main Program loop
-///////////////////////////////////////////////////////////////////
-int main()
-{
-    node_chatter.initNode();
-    node_chatter.advertise(chatter);
+protected:
+    //Declarations go here
+    
     
-    pc.printf("test\r\n");
-    int range;
+    /* example
+    ros::Publisher controller_pub;
+    ros::Subscriber button_sub;
+    sensor_msgs::Joy joy;
+    */
+};
 
-    // load settings onto VL6180X
-    tof1.Init();
-    while (1){
-        range = tof1.Get_Range();
+int main(int argc, char** argv)
+{
+    ros::init(argc, argv, "main");
+    
+    dumbotControl dumbot;
 
-        // send range to pc by serial
-        sprintf(data, "Distance: %dmm", range);
-        
-        str_msg.data = data;
-        chatter.publish( &str_msg );
-        node_chatter.spinOnce();
-        wait_ms(1000);
-    }
+    ros::spin();
+
+    return 0;
 }
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/publisherExample.cpp	Thu Dec 12 19:51:42 2019 +0000
@@ -0,0 +1,49 @@
+// Changed text
+
+///////////////////////////////////////////////////////////////////
+// Beginning of code
+///////////////////////////////////////////////////////////////////
+#include "timeofflight.hpp"
+
+Serial pc(SERIAL_TX, SERIAL_RX);// set-up serial to pc
+#define addr (0x52)             // I²C address of VL6180 shifted by 1 bit
+
+VL6180 tof1(addr);
+
+#include <ros.h>
+#include <std_msgs/String.h>
+
+ros::NodeHandle  node_chatter;
+
+std_msgs::String str_msg;
+ros::Publisher chatter("chatter", &str_msg);
+
+char data[13];
+
+
+
+///////////////////////////////////////////////////////////////////
+// Main Program loop
+///////////////////////////////////////////////////////////////////
+int main()
+{
+    node_chatter.initNode();
+    node_chatter.advertise(chatter);
+    
+    pc.printf("test\r\n");
+    int range;
+
+    // load settings onto VL6180X
+    tof1.Init();
+    while (1){
+        range = tof1.Get_Range();
+
+        // send range to pc by serial
+        sprintf(data, "Distance: %dmm", range);
+        
+        str_msg.data = data;
+        chatter.publish( &str_msg );
+        node_chatter.spinOnce();
+        wait_ms(1000);
+    }
+}
\ No newline at end of file