display.cpp
Go to the documentation of this file.
00001 #include "ros/ros.h"
00002 #include <stdio.h>
00003 #include <stdlib.h>
00004 #include <string.h>
00005 #include <errno.h>
00006 #include <math.h>
00007 #include <sstream>
00008 #include <serial.h>
00009 
00010 #include "std_msgs/String.h"
00011 #include "std_msgs/MultiArrayLayout.h"
00012 #include "std_msgs/MultiArrayDimension.h"
00013 #include "std_msgs/Int32MultiArray.h"
00014 #include "std_msgs/Int32.h"
00015 #include "std_msgs/Float32.h"
00016 #include "std_msgs/Bool.h"
00017 #include "mrp2_display/gpio.h"
00018 
00019 ros::Subscriber bumpers_sub;
00020 ros::Subscriber battery_volt_sub;
00021 ros::Subscriber estop_btn_sub;
00022 ros::Subscriber estop_sub;
00023 
00024 ros::Publisher inputs_pub;
00025 
00026 double last_volt = 0.0;
00027 bool last_fr = false;
00028 bool last_fl = false;
00029 bool last_rr = false;
00030 bool last_rl = false;
00031 
00032 bool last_estop_btn = false;
00033 bool last_estop     = false;
00034 
00035 // Set-reset GPIO output pins
00036 bool gpio(mrp2_display::gpio::Request &req, mrp2_display::gpio::Response &res){
00037 
00038         sendCmd(GPIO_SET, (char)req.pin_number, (char)req.pin_state);
00039 
00040         res.status = true;
00041         return true;
00042 }
00043 
00044 // Plays beep sound
00045 void beep(void)
00046 {
00047         sendCmd(BEEP, 0, 0);
00048 }
00049 
00050 // Send request to read input pins and publish data
00051 void analog_read()
00052 {
00053         portFlush();
00054         sendCmd(READ_ANALOG, 0, 0);
00055         lockSerial(true);
00056 
00057         usleep(100000);
00058 
00059         if(portDataAvail() == 10){
00060 
00061                 std_msgs::Int32MultiArray array;
00062                 int i, an;
00063                 int indata[10];
00064 
00065                 array.data.clear();
00066 
00067                 for(i = 0; i < 10; i++){
00068                         indata[i] = portGetchar();
00069                 }
00070 
00071                 an = indata[0] << 8;
00072                 an += indata[1];
00073                 array.data.push_back(an);
00074 
00075                 an = indata[2] << 8;
00076                 an += indata[3];
00077                 array.data.push_back(an);
00078 
00079                 an = indata[4] << 8;
00080                 an += indata[5];
00081                 array.data.push_back(an);
00082 
00083                 an = indata[6];
00084                 array.data.push_back(an);
00085 
00086                 an = indata[7] << 8;
00087                 an += indata[8];
00088                 array.data.push_back(an);
00089 
00090                 an = indata[9];
00091                 array.data.push_back(an);
00092 
00093                 inputs_pub.publish(array);
00094         }
00095         lockSerial(false);
00096 }
00097 
00098 // Changes e-stop button led on the screen
00099 void estopBtnCallback(const std_msgs::Bool::ConstPtr& stat)
00100 {
00101         if(last_estop_btn != stat->data){
00102                 last_estop_btn = stat->data;
00103                 sendCmd(ESTOP_BTN, last_estop_btn, 0);
00104         }
00105 }
00106 
00107 // Shows estop string on screen
00108 void estopCallback(const std_msgs::Bool::ConstPtr& stat)
00109 {
00110         if(last_estop == false){
00111                 if(last_estop != stat->data){
00112                         last_estop = stat->data;
00113                         sendCmd(ESTOP, 0, 0);
00114                 }
00115         }else{
00116                 if(last_estop != stat->data){
00117                         last_estop = stat->data;
00118                 }
00119         }
00120         
00121 }
00122 
00123 // Sets battery voltage
00124 void batteryVoltCallback(const std_msgs::Float32::ConstPtr& volt)
00125 {
00126         if(last_volt != volt->data){
00127                 last_volt = volt->data;
00128                 uint16_t send = (uint16_t)(last_volt * 10 );
00129                 if(last_estop == false)
00130                         sendCmd(BATTERY, (uint8_t)(send >> 8), (uint8_t)send);
00131         }
00132 }
00133 
00134 // Changes status of bumper indicators 
00135 void bumpersCallback(const std_msgs::Int32MultiArray::ConstPtr& bumpers)
00136 {
00137         uint8_t fl = bumpers->data[3];
00138         uint8_t fr = bumpers->data[2];
00139         uint8_t rl = bumpers->data[1];
00140         uint8_t rr = bumpers->data[0];
00141 
00142         if(last_fl != fl){
00143                 last_fl = fl;
00144                 sendCmd(BUMPER_FL, fl, 0);
00145                 usleep(100000);
00146                 
00147                 if(fl == 1){
00148                         beep();
00149                 }               
00150         }
00151 
00152         if(last_fr != fr){
00153                 last_fr = fr;
00154                 sendCmd(BUMPER_FR, fr, 0);
00155                 usleep(100000);
00156 
00157                 if(fr == 1){
00158                 beep();
00159                 }       
00160         }
00161 
00162         if(last_rl != rl){
00163                 last_rl = rl;
00164                 sendCmd(BUMPER_RL, rl, 0);
00165                 usleep(100000);
00166 
00167                 if(rl == 1){
00168                 beep();
00169                 }       
00170         }
00171 
00172         if(last_rr != rr){
00173                 last_rr = rr;
00174                 sendCmd(BUMPER_RR, rr, 0);
00175                 usleep(100000);
00176 
00177                 if(rr == 1){
00178                 beep();
00179                 }
00180         }
00181 }
00182 
00183 int main(int argc, char **argv)
00184 {
00185 
00186         ros::init(argc, argv, "mrp2_display");
00187 
00188         ros::NodeHandle n;
00189 
00190         ros::Rate loop_rate(10);
00191 
00192         std::string port;
00193         ros::param::param<std::string>("~port", port, "/dev/ttyS1");
00194 
00195         // Serial port setup
00196         printf("Starting display at port: %s \n", port.c_str());
00197         if (portOpen(const_cast<char*>(port.c_str()), 57600) < 0)
00198         {
00199                 fprintf (stderr, "rgb: Can't initialise Display: %s\n", strerror (errno)) ;
00200                 return 1 ;
00201         }
00202 
00203         bumpers_sub = n.subscribe<std_msgs::Int32MultiArray>("bumpers", 1, &bumpersCallback);
00204         battery_volt_sub = n.subscribe<std_msgs::Float32>("/batt_volt", 1, &batteryVoltCallback);
00205         estop_btn_sub = n.subscribe<std_msgs::Bool>("/estop_btn", 1, &estopBtnCallback);
00206         estop_sub = n.subscribe<std_msgs::Bool>("/estop", 1, &estopCallback);
00207 
00208         inputs_pub = n.advertise<std_msgs::Int32MultiArray>("panel_inputs", 1);
00209         ros::ServiceServer service = n.advertiseService("/panel_outputs/gpio", gpio);
00210 
00211         int i = 0;
00212 
00213         while (ros::ok())
00214         {
00215                 i++;
00216                 if(i == 3)
00217                 {
00218                         analog_read();
00219                         i = 0;
00220                 }
00221                 
00222                 ros::spinOnce();
00223         loop_rate.sleep();      
00224         }
00225         return 0 ;
00226 }
00227 


mrp2_display
Author(s): Bolkar Altuntas
autogenerated on Thu Jun 6 2019 21:43:35