Difference between revisions of "ROS - Andrej Zemanovič"
(→Zdrojové kódy) |
|||
Line 41: | Line 41: | ||
==Zdrojové kódy== | ==Zdrojové kódy== | ||
+ | Vyhýhanie prekážkam: | ||
+ | <code> | ||
+ | #include "ros/ros.h" | ||
+ | #include "sensor_msgs/LaserScan.h" | ||
+ | #include <geometry_msgs/Twist.h> | ||
+ | #include <stdio.h> | ||
+ | |||
+ | |||
+ | ros::Publisher publisher; | ||
+ | geometry_msgs::Twist msg; | ||
+ | float min_vzd_uhol, min_vzdial; | ||
+ | |||
+ | void chatterCallback(const sensor_msgs::LaserScan::ConstPtr& msg) | ||
+ | { | ||
+ | min_vzdial= msg->ranges[0]; | ||
+ | min_vzd_uhol= 0; | ||
+ | for(int j=0; j <= 360; j++) | ||
+ | { | ||
+ | if(msg->ranges[j]<min_vzdial) | ||
+ | { | ||
+ | min_vzdial= msg->ranges[j]; | ||
+ | min_vzd_uhol= j/2; | ||
+ | } | ||
+ | } | ||
+ | if(min_vzdial<=1.1) | ||
+ | { | ||
+ | if(min_vzd_uhol >= 90) { | ||
+ | msg.linear.x = 0; | ||
+ | msg.angular.z= -1.0; | ||
+ | printf("zatacam doprava\n"); | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | msg.linear.x = 0; | ||
+ | msg.angular.z = 1.0; | ||
+ | printf("zatacam dolava\n"); | ||
+ | } | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | msg.linear.x= 1.0; | ||
+ | msg.angular.z= 0; | ||
+ | printf("idem rovno\n"); | ||
+ | } | ||
+ | |||
+ | publisher.publish(msg); | ||
+ | |||
+ | } | ||
+ | |||
+ | int main(int argc, char **argv) | ||
+ | { | ||
+ | |||
+ | ros::init(argc, argv, "laser_messages"); | ||
+ | ros::NodeHandle n1; | ||
+ | ros::NodeHandle n2; | ||
+ | |||
+ | publisher = n2.advertise<geometry_msgs::Twist>("base_controller/command",1); | ||
+ | msg.linear.x= 0; | ||
+ | msg.linear.y= 0; | ||
+ | msg.linear.z= 0; | ||
+ | msg.angular.x= 0; | ||
+ | msg.angular.y= 0; | ||
+ | msg.angular.z= 0; | ||
+ | |||
+ | ros::Subscriber subscriber = n1.subscribe("base_scan", 1, chatterCallback); | ||
+ | ros::spin(); | ||
+ | |||
+ | return 0; | ||
+ | } | ||
+ | |||
+ | </code> |
Revision as of 11:54, 3 June 2017
Contents
Cieľ projektu
Cieľom projektu bolo nainštalovať, nastaviť a spustiť framework Robot Operating System(ROS) - http://www.ros.org/ , zoznámiť sa s konceptom ROS, vyskúšať funkcionality a skúsiť spustiť simuláciu robota a naprogramovať niektoré akcie.
Inštalácia
Na stránke ROS http://www.ros.org/ som stiahol verziu Kinetic Kame pre Ubuntu 16.04, nastavil som aby repozitáre pre Ubuntu povoľovali "restricted", "universe", "multiverse".
Potom som nakonfiguroval, aby Ubuntu akceptovalo balíčky zo zdroja packages.ros.org.
sudo sh -c 'echo "deb http://packages.ros.org/ros/ubuntu $(lsb_release -sc) main" > /etc/apt/sources.list.d/ros-latest.list'
Nastavil prístupové kľúče sudo apt-key adv --keyserver hkp://ha.pool.sks-keyservers.net:80 --recv-key 421C365BD9FF1F717815A3895523BAEEB01FA116
Nainštaloval inštalačný balíček sudo apt-get install ros-kinetic-desktop-full
Inicializoval ROS závislosti sudo rosdep init
rosdep update
Nastavil prostredie, aby ROS premenné boli pri každom spustené terminalu (bashu) prístupné.
echo "source /opt/ros/kinetic/setup.bash" >> ~/.bashrc
source ~/.bashrc
Následne som nainštaloval rosinstall, čo je command-line tool, pre jednoduchšie získanie ROS balíčkov. Spúšťa sa príkazom sudo apt-get install python-rosinstall
Vytvorenie ROS workspace
Vytvoril som si ROS workspace mkdir -p ~/catkin_ws/src
.
Vybuildovanie prostredia, vojdeme do priečinku vytvoreného workspaceu catkin_ws a zavoláme catkin_make
ROS Node
ROS Topic
Vytvorenie a použitie Service
Posielanie, prijímanie správ
Simulácie
Zber dát
Zdrojové kódy
Vyhýhanie prekážkam:
- include "ros/ros.h"
- include "sensor_msgs/LaserScan.h"
- include <geometry_msgs/Twist.h>
- include <stdio.h>
ros::Publisher publisher;
geometry_msgs::Twist msg;
float min_vzd_uhol, min_vzdial;
void chatterCallback(const sensor_msgs::LaserScan::ConstPtr& msg) { min_vzdial= msg->ranges[0]; min_vzd_uhol= 0; for(int j=0; j <= 360; j++) { if(msg->ranges[j]<min_vzdial) { min_vzdial= msg->ranges[j]; min_vzd_uhol= j/2; } } if(min_vzdial<=1.1) {
if(min_vzd_uhol >= 90) {
msg.linear.x = 0;
msg.angular.z= -1.0;
printf("zatacam doprava\n");
}
else {
msg.linear.x = 0;
msg.angular.z = 1.0; printf("zatacam dolava\n"); } } else { msg.linear.x= 1.0; msg.angular.z= 0; printf("idem rovno\n"); }
publisher.publish(msg);
}
int main(int argc, char **argv) {
ros::init(argc, argv, "laser_messages"); ros::NodeHandle n1; ros::NodeHandle n2;
publisher = n2.advertise<geometry_msgs::Twist>("base_controller/command",1); msg.linear.x= 0; msg.linear.y= 0; msg.linear.z= 0; msg.angular.x= 0; msg.angular.y= 0; msg.angular.z= 0;
ros::Subscriber subscriber = n1.subscribe("base_scan", 1, chatterCallback); ros::spin();
return 0;
}