bma180-reader.cpp
Go to the documentation of this file.
00001 /* -------------------------------------------------------------------------- */
00002 // BMA180 Spi Communication Node
00003 //
00004 //    - Reads from Shared Memory buffer
00005 //    - Publishes message with accel data
00006 //
00007 //  Nikhil Deshpande, 7-Jul-2011 | Robert Bosch RTC
00008 /* -------------------------------------------------------------------------- */
00009 
00010 // Includes
00011 /* -------------------------------------------------------------------------- */
00012 #include <sstream>
00013 #include <iostream>
00014 #include <stdint.h>
00015 #include <stdio.h>
00016 #include <stdlib.h>
00017 #include <fcntl.h>
00018 #include <unistd.h>
00019 #include <sys/ioctl.h>
00020 #include <sys/time.h>
00021 #include <sys/ipc.h>
00022 #include <sys/types.h>
00023 #include <sys/shm.h>
00024 #include <linux/spi/spidev.h>
00025 #include <ros/ros.h>
00026 #include <ros/time.h>
00027 #include <std_msgs/String.h>
00028 #include <gumstix_memread_bma180/bma180meas.h>
00029 #include <gumstix_memwrite_bma180/writermsg.h>
00030 #include <errno.h>
00031 
00032 using namespace std;
00033 
00034 #define SHMSZ   30720000
00035 #define xSHMSZ  0x1D4C000  //hex converted
00036 #define NUM_SENSORS     8       
00037 bool readit=false;
00038 
00039 void msgCallback(const gumstix_memwrite_bma180::writermsg::ConstPtr & msg){
00040         if(msg->written==true){
00041                 readit=true;
00042         } else{
00043                 readit=false;
00044         }
00045 }
00046 
00047 // Main
00048 
00049 int main(int argc, char** argv) {
00050         // Ros
00051         ros::init(argc, argv, "gumstix_memread_bma180");
00052         ros::NodeHandle n;
00053         ros::Publisher pub = n.advertise<gumstix_memread_bma180::bma180meas> ("bma180meas", 1000);
00054         ros::Subscriber sub = n.subscribe("/writermsg", 100, msgCallback);
00055 
00056         // Initialize ROS Message
00057         gumstix_memread_bma180::bma180meas msg;
00058 
00059         // Rate object
00060         double rate_Hz;
00061         if(n.getParam("/gumstix_memread_bma180/rate_Hz", rate_Hz)==false){
00062                 rate_Hz = 50;
00063         }       
00064         cout << "Publishing rate: " << rate_Hz << " Hz" << endl;        
00065         // Initialize Shared Memory
00066         int16_t *shm, *s, shmid;
00067         key_t key = 42;
00068         if ((shmid = shmget(key, SHMSZ, 0666)) < 0) {
00069             cerr << "shmget did not work - " << errno << endl;
00070             if(errno == EINVAL)
00071                 printf("Invalid segment size specified\n");
00072             else if(errno == EEXIST)
00073                 printf("Segment exists, cannot create it\n");
00074             else if(errno == EIDRM)
00075                 printf("Segment is marked for deletion or was removed\n");
00076             else if(errno == ENOENT)
00077                 printf("Segment does not exist\n");
00078             else if(errno == EACCES)
00079                 printf("Permission denied\n");
00080             else if(errno == ENOMEM)
00081                 printf("Not enough memory to create segment\n");
00082         }
00083         if ((shm = (int16_t *)shmat(shmid, NULL, 0)) == (int16_t *) -1) {
00084                 cerr << "shmat did not work!" << endl;  
00085         }
00086 
00087         // J counts measurements per Message (index in msg array)
00088         int16_t i=0, j=0, k=0, val=0;
00089         long m=0, l=0;
00090         s = shm;
00091         s++;
00092         cout << "Begin memory <reader> : " << s << endl;
00093         ros::Rate loop1(1);
00094         while(ros::ok()){
00095                 if(readit==true){
00096                         readit = false;
00097                         break;
00098                 }
00099                 ros::spinOnce();
00100                 loop1.sleep();
00101         }
00102         cout << "Publishing to topic /bma180meas... " << endl;
00103         ros::Rate loop_rate(rate_Hz);
00104         // Main loop
00105         while (ros::ok()) {
00106                 // Transfer/receive messages
00107                 int num_msgs = 6*10*NUM_SENSORS;
00108                 for (j=0; j<(num_msgs); j++) {
00109                         i  = (int16_t)*s;
00110                         msg.vals.push_back(i);
00111                         //cout << "value: " << msg.vals.at(j) << endl;
00112                         s++; m++;
00113                         if(s>=(shm+xSHMSZ)){
00114                                 s = shm;
00115                                 s++;
00116                         }
00117                         if(i==0) l++;
00118                         if(l>=num_msgs){
00119                                 k=1;
00120                                 break;
00121                         }
00122                 }
00123                 l=0;
00124                 pub.publish(msg);
00125                 msg.vals.clear();
00126                 if(k==1){
00127                         break;
00128                 }
00129                 // Ros rate
00130                 ros::spinOnce();
00131                 loop_rate.sleep();      
00132         }
00133         cout << "Done publishing..." << endl;
00134         cout << "Values published: " << m << endl; 
00135 //      *shm = m+1;
00136         cout << "End memory <reader>: " << s << endl;
00137         exit(0);
00138 
00139 }


gumstix_memread_bma180
Author(s): Nikhil Deshpande (Maintained by Philip Roan)
autogenerated on Mon Oct 6 2014 10:10:10