publishing trajectory_msgs/jointtrajectory msgs - ros

When i set the position and velocities of the joints in the trajectory msgs i got an error: \
[state_publisher-2] process has died [pid 13362, exit code -11, cmd /home/rob/catkin_ws/devel/lib/r2d2/state_publisher __name:=state_publisher __log:=/home/rob/.ros/log/9980f352-cf74-11e6-8644-d4c9efe8bd37/state_publisher-2.log].
log file: /home/rob/.ros/log/9980f352-cf74-11e6-8644-d4c9efe8bd37/state_publisher-2*.log
My ros node to send geometry_msgs is:
#include <string>
#include <ros/ros.h>
#include <sensor_msgs/JointState.h>
#include <tf/transform_broadcaster.h>
#include <trajectory_msgs/JointTrajectory.h>
#include <vector>
int main(int argc, char** argv) {
ros::init(argc, argv, "state_publisher");
ros::NodeHandle n;
ros::Publisher joint_pub = n.advertise<trajectory_ms
gs::JointTrajectory>("set_joint_trajectory", 1);
ros::Rate loop_rate(30);
const double degree = M_PI/180;
// robot state
double tilt = 0, tinc = degree, swivel=0, angle=0, height=0, hinc=0.005;
// message declarations
trajectory_msgs::JointTrajectory joint_state;
std::vector<trajectory_msgs::JointTrajectoryPoint> points_n(3);
points_n[0].positions[0] = 1; points_n[0].velocities[0]=10;
while (ros::ok()) {
//update joint_state
joint_state.header.stamp = ros::Time::now();
joint_state.joint_names.resize(3);
joint_state.points.resize(3);
joint_state.joint_names[0] ="swivel";
joint_state.points[0] = points_n[0];
joint_state.joint_names[1] ="tilt";
joint_state.points[1] = points_n[1];
joint_state.joint_names[2] ="periscope";
joint_state.points[2] = points_n[2];
joint_pub.publish(joint_state);
// This will adjust as needed per iteration
loop_rate.sleep();
}
return 0;
}
Here when i donot set the position and velocity value it runs without error and when i run rostopic echo /set_joint_trajectory i can clearly see the outputs as all the parameters of points is 0. I also tried below program but it published nothing:
#include <string>
#include <ros/ros.h>
#include <sensor_msgs/JointState.h>
#include <tf/transform_broadcaster.h>
#include <trajectory_msgs/JointTrajectory.h>
#include <vector>
int main(int argc, char** argv) {
ros::init(argc, argv, "state_publisher");
ros::NodeHandle n;
ros::Publisher joint_pub = n.advertise<trajectory_msgs::JointTrajectory>("set_joint_trajectory", 1);
trajectory_msgs::JointTrajectory joint_state;
joint_state.header.stamp = ros::Time::now();
joint_state.header.frame_id = "camera_link";
joint_state.joint_names.resize(3);
joint_state.points.resize(3);
joint_state.joint_names[0] ="swivel";
joint_state.joint_names[1] ="tilt";
joint_state.joint_names[2] ="periscope";
size_t size = 2;
for(size_t i=0;i<=size;i++) {
trajectory_msgs::JointTrajectoryPoint points_n;
int j = i%3;
points_n.positions.push_back(j);
points_n.positions.push_back(j+1);
points_n.positions.push_back(j*2);
joint_state.points.push_back(points_n);
joint_state.points[i].time_from_start = ros::Duration(0.01);
}
joint_pub.publish(joint_state);
ros::spinOnce();
return 0;
}

You are accessing points_n[0].positions[0] and points_n[0].velocities[0] without allocating the memory for positions and velocities. Use
...
// message declarations
trajectory_msgs::JointTrajectory joint_state;
std::vector<trajectory_msgs::JointTrajectoryPoint> points_n(3);
points_n[0].positions.resize(1);
points_n[0].velocities.resize(1);
...
then set the values or use points_n[0].positions.push_back(...) instead. The same applies to points_n[1] and points_n[2].
In your second example it looks like your program terminates before anything is sent. Try to publish repeatedly in a while-loop with
while(ros::ok()){
...
ros::spinOnce();
}

Related

pthread error code 3025 (ENOENT) on the as400/IBM i?

When I have the following C source code, which is running on an IBM i Midrange, then I get a non-zero result from pthread_create, specifically 3025, which is ENOENT (No such path or directory), which doesn't make any sense to me. Anyone have any thoughts on what the error actually means in this context.
#define _MULTI_THREADED
#define _XOPEN_SOURCE 520
#include <pthread.h>
#include <stdio.h>
#include <errno.h>
void* workerThread(void* parm) {
// Do some work here
pthread_exit(NULL);
}
int main(int argc, char* argv[]) {
pthread_t t;
int rc;
rc = pthread_create(&t, NULL, workerThread, NULL);
if (rc != 0) {
char *msg = strerror(errno);
perror("pthread_create failed");
}
// Other code here
return 0;
}
pthread_create doesn't set errno. You should be checking strerror of rc.
http://pubs.opengroup.org/onlinepubs/7908799/xsh/pthread_create.html
char *msg = strerror(rc);

OpenCV play 2 videos same time

I would like to present two videos at the same time. The following code is working, but somehow it has an error occurs when the videos are finished. If I only play one video, the error does not occur. Any help?
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main()
{
string filename1 = "C:/test_videos/toy_plane_liftoff.avi";
string filename2 = "C:/test_videos/toy_plane_liftoff_stab.avi";
VideoCapture capture1(filename1);
VideoCapture capture2(filename2);
Mat frame1, frame2;
if (!capture1.isOpened())
throw "Error1";
if (!capture2.isOpened())
throw "Error2";
for (int i = 0; i <= min(capture1.get(CV_CAP_PROP_FRAME_COUNT), capture2.get(CV_CAP_PROP_FRAME_COUNT)); i++)
{
capture1 >> frame1;
capture2 >> frame2;
imshow("1", frame1);
imshow("2", frame2);
if (waitKey(30) >= 0)
break;
}
return 0;
}
You get rid of the error in the following way
if( !frame1.empty())
imshow("1", frame1);
if( !frame2.empty())
imshow("2", frame2);

openCV:capture images from webcam but only greysreen

Can anyone tell me what's wrong with my code? I can use my webcam by other code, so there is nothing do with the supported problem. In my code below, I must set the camera index into a loop so that i can motivate my camera(the led inductor is on, simply set "CvCapture* camera=cvCaptureFromCAM(0)" can not run! that's wierd!).However, I just can obtain a greysreen,why?
#include "highgui.h"
#include "cv.h"
int main(int grac, char** grav)
{
CvCapture* camera;
int index;
for (index = -1; index <1; index++)
{
camera = cvCaptureFromCAM(index);
if (camera)
{
printf("%d\n", index);
IplImage* f;
cvNamedWindow("camera", CV_WINDOW_AUTOSIZE);
while (1)
{
f = cvQueryFrame(camera);
cvShowImage("camera", f);
char c = cvWaitKey(33);
if (c == 27)break;
}
}
}
cvReleaseCapture(&camera);
cvDestroyAllWindows;
}

Setsockopt() returning error number 10042

So I am getting started with SCTP and have written the basics of the SCTP server application(which I intend to modify to make it a peer-to-peer app). The code is incomplete but I compiled and ran it to test the socket options and the first setsockopt returns error 10042(protocol not supported). I have determined that it's the first call of setsockopt() that returns an error. So here is the incomplete code:
#include "stdafx.h"
#include <iostream>
#include <iostream>
#include <string.h>
#include <stdlib.h>
#ifndef UNICODE
#define UNICODE
#endif
#define WIN32_LEAN_AND_MEAN
#include <WinSock2.h>
#include <WS2tcpip.h>
#include <WS2spi.h>
#include <ws2sctp.h>
#include <wsipv6ok.h>
#include <if.h>
#include "ws2isatap.h"
#include "if_tunnel.h"
#pragma comment(lib, "ws2_32.lib")
#pragma comment(lib, "sctpsp.lib")
using namespace std;
using namespace System;
static int LISTENQ = 5;
void isatap_enable(void);
int main(int argc, char* argv[]){
WSADATA wsaData;
int iResult;
int optv = 10;
u_long imode = 1;
bool connected = false;
char *optval = (char*)&optv;
int optlen = sizeof(optval);
sockaddr_in6 servAddr;
sctp_sndrcvinfo sr;
sctp_event_subscribe evnts;
sctp_initmsg init;
memset(&sr,0,sizeof(sr));
memset(&evnts,0,sizeof(evnts));
memset(&init,0,sizeof(init));
memset(&servAddr,0,sizeof(servAddr));
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != NO_ERROR) {
wprintf(L"WSAStartup function failed with error: %d\n", iResult);
return 1;
}
SOCKET servSock = socket(AF_INET6,SOCK_STREAM,IPPROTO_SCTP);
if(servSock==INVALID_SOCKET){
printf("Socket function failed with error: %d\n",GetLastError());
return 1;
}
if(setsockopt(servSock,IPPROTO_IPV6,IPV6_PROTECTION_LEVEL,optval,sizeof(optval))<0){
printf("setsockopt function failed with error: %d\n", GetLastError());
return 1;
}
u_int servPort = 5000;
servAddr.sin6_family = AF_INET6;
servAddr.sin6_addr = in6addr_any;
servAddr.sin6_port = htons(servPort);
if(setsockopt(servSock,IPPROTO_SCTP,SCTP_EVENTS,(const char*)&evnts,sizeof(evnts)) < 0){
printf("setsockopt function failed with error: %d\n", GetLastError());
return 1;
}
ioctlsocket(servSock,FIONBIO, &imode);
if(bind(servSock,(struct sockaddr*)&servAddr,sizeof(servAddr))<0){
printf("Bind function failed with error: %d\n", GetLastError());
return 1;
}
evnts.sctp_data_io_event = 1;
evnts.sctp_association_event = *(u_char*)optval;
for(;;){
if(listen(servSock,LISTENQ) < 0){
printf("Listen function failed with error: %d/n",GetLastError());
return 1;
}else{
printf("Listening on port %d\n",servPort);
}
}
}
If it's related to python packages nameko/kombu/amqp, then it's reported here Issue with 2.1.4 version.
Temporary soln: Roll-back the version to 2.1.3 till it's fixed.
OK guys, I fixed it(for now lol). What I did is substitute the IPPROTO_IPV6 with IPPROTO_SCTP and it seems to work.

CodeBook background subtraction with moving objects

I am trying to use CodeBook method for OpenCV to subtract the background. So far so good, but I am not sure if I can update the codebook for moving objects after some time span, say 5 minutes, I need to update the codebook after which I get lots of moving objects. How do I make sure that after 5 minutes I have a background that needs to be updated?
Thanks for the tips!
kim's algorithm supposedly has a cache layer, i did not dig deep enough into opencv implementation to know how to have a workable version that works with exposure problem.
notes
opencv book(which i havent read) should have the info you need if it's there at all
this is only 1 channel and not in yuv although it can be extended
needs more work on speed/testing (you can of course turn on optimization in compiler)
background subtraction is a buzzy word. try "change detection"
bgfg_cb.h
#ifndef __bgfg_cb_h__
#define __bgfg_cb_h__
//http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.148.9778&rep=rep1&type=pdf
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
#include <time.h>
using namespace cv;
using namespace std;
struct codeword {
float min;
float max;
float f;
float l;
int first;
int last;
bool isStale;
};
extern int alpha ;
extern float beta ;
extern int Tdel ,Tadd , Th;
void initializeCodebook(int w,int h);
void update_cb(Mat& frame);
void fg_cb(Mat& frame,Mat& fg);
#endif
bgfg_cb.cpp
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
#include <time.h>
#include "bgfg_cb.h"
using namespace cv;
using namespace std;
vector<codeword> **cbMain;
vector<codeword> **cbCache;
int t=0;
int alpha = 10;//knob
float beta =1;
int Tdel = 200,Tadd = 150, Th= 200;//knobs
void initializeCodebook(int w,int h)
{
cbMain = new vector<codeword>*[w];
for(int i = 0; i < w; ++i)
cbMain[i] = new vector<codeword>[h];
cbCache = new vector<codeword>*[w];
for(int i = 0; i < w; ++i)
cbCache[i] = new vector<codeword>[h];
}
void update_cb(Mat& frame)
{
if(t>10) return;
for(int i=0;i<frame.rows;i++)
{
for(int j=0;j<frame.cols;j++)
{
int pix = frame.at<uchar>(i,j);
vector<codeword>& cm =cbMain[i][j];
bool found = false;
for(int k=0;k<cm.size();k++)
{
if(cm[k].min<=pix && pix<=cm[k].max && !found)
{
found=true;
cm[k].min = ((pix-alpha)+(cm[k].f*cm[k].min))/(cm[k].f+1);
cm[k].max = ((pix+alpha)+(cm[k].f*cm[k].max))/(cm[k].f+1);
cm[k].l=0;
cm[k].last=t;
cm[k].f++;
}else
{
cm[k].l++;
}
}
if(!found)
{
codeword n={};
n.min=max(0,pix-alpha);
n.max=min(255,pix+alpha);
n.f=1;
n.l=0;
n.first=t;
n.last=t;
cm.push_back(n);
}
}
}
t++;
}
void fg_cb(Mat& frame,Mat& fg)
{
fg=Mat::zeros(frame.size(),CV_8UC1);
if(cbMain==0) initializeCodebook(frame.rows,frame.cols);
if(t<10)
{
update_cb(frame);
return;
}
for(int i=0;i<frame.rows;i++)
{
for(int j=0;j<frame.cols;j++)
{
int pix = frame.at<uchar>(i,j);
vector<codeword>& cm = cbMain[i][j];
bool found = false;
for(int k=0;k<cm.size();k++)
{
if(cm[k].min<=pix && pix<=cm[k].max && !found)
{
cm[k].min = ((1-beta)*(pix-alpha)) + (beta*cm[k].min);
cm[k].max = ((1-beta)*(pix+alpha)) + (beta*cm[k].max);
cm[k].l=0;
cm[k].first=t;
cm[k].f++;
found=true;
}else
{
cm[k].l++;
}
}
cm.erase( remove_if(cm.begin(), cm.end(), [](codeword& c) { return c.l>=Tdel;} ), cm.end() );
fg.at<uchar>(i,j) = found?0:255;
if(found) continue;
found = false;
vector<codeword>& cc = cbCache[i][j];
for(int k=0;k<cc.size();k++)
{
if(cc[k].min<=pix && pix<=cc[k].max && !found)
{
cc[k].min = ((1-beta)*(pix-alpha)) + (beta*cc[k].min);
cc[k].max = ((1-beta)*(pix+alpha)) + (beta*cc[k].max);
cc[k].l=0;
cc[k].first=t;
cc[k].f++;
found=true;
}else
{
cc[k].l++;
}
}
if(!found)
{
codeword n={};
n.min=max(0,pix-alpha);
n.max=min(255,pix+alpha);
n.f=1;
n.l=0;
n.first=t;
n.last=t;
cc.push_back(n);
}
cc.erase( remove_if(cc.begin(), cc.end(), [](codeword& c) { return c.l>=Th;} ), cc.end() );
for(vector<codeword>::iterator it=cc.begin();it!=cc.end();it++)
{
if(it->f>Tadd)
{
cm.push_back(*it);
}
}
cc.erase( remove_if(cc.begin(), cc.end(), [](codeword& c) { return c.f>Tadd;} ), cc.end() );
}
}
}
main.cpp
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "bgfg_cb.h"
#include <iostream>
using namespace cv;
using namespace std;
void proc()
{
Mat frame,fg,gray;
VideoCapture cap("C:/Downloads/S2_L1.tar/S2_L1/Crowd_PETS09/S2/L1/Time_12-34/View_001/frame_%04d.jpg");
cap >> frame;
initializeCodebook(frame.rows,frame.cols);
for(;;)
{
cap>>frame;
cvtColor(frame,gray,CV_BGR2GRAY);
fg_cb(gray,fg);
Mat cc;
imshow("fg",fg);
waitKey(1);
}
}
int main(int argc, char ** argv)
{
proc();
cin.ignore(1);
return 0;
}

Resources