ROS Construct

C++ for Robotics

Examples mit Maps/Dictionary

#include "rosbot_control/rosbot_class.h"
#include <ros/ros.h>

#include <string>
#include <list>
#include <iostream>
#include <map>
using namespace std;
map<float, double> robot_data;
int main(int argc, char **argv) {
  ros::init(argc, argv, "rosbot_node");

  RosbotClass rosbot;

  float x = rosbot.get_position(1);
  double time = rosbot.get_time();   
  robot_data[time] = x;
  rosbot.move();

  x = rosbot.get_position(1);
  time = rosbot.get_time(); 
  robot_data[time] = x;
  rosbot.move();

  x = rosbot.get_position(1);
  time = rosbot.get_time(); 
  robot_data[time] = x;
  rosbot.move();

  for (auto data : robot_data)
    cout << "Zeit:" << data.first << "Position" << data.second << "\n";
  return 0;
}
#include "rosbot_control/rosbot_class.h"
#include <ros/ros.h>


#include <string>
#include <list>
#include <iostream>
#include <map>
using namespace std;
map<float, double> robot_data;
int main(int argc, char **argv) {
  ros::init(argc, argv, "rosbot_node");

  RosbotClass rosbot;

  float x0 = rosbot.get_position(1);
  double time0 = rosbot.get_time(); 
  rosbot.move();

  float x1 = rosbot.get_position(1);
  double time1 = rosbot.get_time(); 
  rosbot.move();

  float velo =  (x1 - x0)/(time1 - time0); 
  ROS_INFO_STREAM( (velo <1) << endl);


  return 0;
}

Moving the Robot - Chapter 2

#include "rosbot_control/rosbot_class.h"
#include <ros/ros.h>


#include <string>
#include <list>
#include <iostream>
#include <map>
using namespace std;
map<float, double> robot_data;
int main(int argc, char **argv) {
  ros::init(argc, argv, "rosbot_node");
  float x_limit = 1;
  RosbotClass rosbot;
  rosbot.move_forward(4);

  float x0 = rosbot.get_position(1);
  double time0 = rosbot.get_time(); 
  ROS_INFO_STREAM(x0);
  if (x0>x_limit)
    {
      rosbot.stop_moving();
    } 
  else 
    {
      rosbot.move_backwards(1);
      rosbot.stop_moving();
    }

Chapter 3.1

#include "rosbot_control/rosbot_class.h"
#include <ros/ros.h>
#include <list>

using namespace std;

list<float> move_and_inform(RosbotClass robot, int n_secs)
{
  list<float> coordinates; 
  robot.move_forward(n_secs);
  coordinates = robot.get_position_full();  
  return coordinates; 

}


int main(int argc, char **argv) {
  ros::init(argc, argv, "rosbot_node");
  int secs;
  RosbotClass rosbot;
  list<float> xy =  move_and_inform(rosbot, 2);
  for (float item : xy)
      cout << item << ", ";

  return 0;
}

Chapter 3.2

#include "rosbot_control/rosbot_class.h"
#include <list>
#include <ros/ros.h>
#include <string>

using namespace std;

list<float> move_and_inform(RosbotClass robot, int n_secs) {
  list<float> coordinates;
  robot.move_forward(n_secs);
  coordinates = robot.get_position_full();
  return coordinates;
}

list<float> move_and_turn(RosbotClass robot, int n_secs, string clockwise) {
  list<float> coordinates;
  robot.turn(clockwise, n_secs);
  coordinates = robot.get_position_full();
  return coordinates;
}

int main(int argc, char **argv) {
  ros::init(argc, argv, "rosbot_node");
  int secs;
  RosbotClass rosbot;
  list<float> xy = move_and_inform(rosbot, 2);
  for (float item : xy)
    cout << item << ", ";
  xy = move_and_turn(rosbot, 3, "clockwise");
  xy = move_and_inform(rosbot, 4);

  return 0;
}

Chapter 4.1

#include "rosbot_control/rosbot_class.h"
#include <ros/ros.h>

int main(int argc, char **argv) {
  ros::init(argc, argv, "rosbot_node");
  float laser[2];
  RosbotClass rosbot;
  rosbot.move_forward(1);
  laser[0] = rosbot.get_laser(719);
  laser[1] = rosbot.get_laser(20);

  for (auto items : laser) {
    cout << items << endl;
  }
  return 0;
}
````

Chapter 4.2

```c++
#include <iostream>

using namespace std;

void squared_array(int array[]) {
  int *pointer;
  pointer = array;
  for (int i=0 ; i < 6 ; i++)
  {
    *pointer = *pointer * *pointer;
    cout << *pointer << ", ";
    pointer++;
  } 
}

int main() {
  int array[] = {4, 8, 15, 16, 23, 42};
  squared_array(array);
  return 0;
}

Chapter 4.3a Lösung a

#include "rosbot_control/rosbot_class.h"
#include <ros/ros.h>

int main(int argc, char **argv) {
  ros::init(argc, argv, "rosbot_node");
  RosbotClass rosbot;
  rosbot.move_forward(3);
  float *pointer;

  pointer = rosbot.get_laser_full();
  ROS_INFO_STREAM("Laser values: ");
  for (int i = 0; i < 720; i++) {
    ROS_INFO_STREAM(*pointer);
    pointer++;
  }

  return 0;
}

Lösung b

#include "rosbot_control/rosbot_class.h"
#include <ros/ros.h>

int main(int argc, char **argv) {
  ros::init(argc, argv, "rosbot_node");
  float * laser;
  RosbotClass rosbot;
  rosbot.move_forward(1);
  laser = rosbot.get_laser_full();

  float * stop = laser + 720; 
  float * pointer = laser;
  while (pointer != stop)
  {
    cout << *pointer << "\n";
    ++pointer;
  }
  return 0;
}

Keine Frame_id und kein Timestamp bei Apriltags

a) auf dem einen Melodic-System (NAS) hat rviz trotzdem funktioniert

b) dem Image muss eine frame_id im Header hinzugefügt werden, ebenso eine Timestampf

rosrun tf view_frames ist kaputt

stattdessen sudo apt install ros-noetic-tf2-tools

rosrun tf2_tools view_frames.py

ROS kann aus einer WSL2-VM heraus keine Topics publishen. Dazu müsste man Port-Forwarding einsetzen?

https://github.com/microsoft/WSL/issues/4150

Spaces durch Tabs ersetzen

sed -e 's/    /\t/g' test.py > test.new.py

Erstes Object aus Generator lesen

g= robot.world-visible._objects

first = next(g)

print (g)

Navmap in der interactive Shell der Vector_Anki_SDK

robot.nav_map.init_nav_map_feed()
latest_nav_map=robot.nav_map.latest_nav_map

Navmap einzeln

import anki_vector
with anki_vector.Robot() as robot:
        robot.nav_map.init_nav_map_feed()

        latest_nav_map = robot.nav_map.latest_nav_map
        for x in range(0,64):
           for y in range (0,64):
                 content = latest_nav_map.get_content(x,y)
                  #print(f"Sampling point at x", x, "y:",  y, " found content:",content)
                  #if content > 0:
                  #    print ("Success")
                 if (content < 9):
                          print(str(content),end='')
           print("\r")

ROS .bashrc

export ROS_MASTER_URI=http://192.168.2.120:11311/
export ROS_HOSTNAME=192.168.2.108



function settitle()
{
    if [ $# -eq 0 ]
        then
        eval set -- "\\u@\\h: \\w"
    fi

    case $TERM in
        xterm*) local title="\[\033]0;$@\007\]";;
        *) local title=''
    esac
    local prompt=$(echo "$PS1" | sed -e 's/\\\[\\033\]0;.*\\007\\\]//')
    PS1="${title}${prompt}"
}

printf "[1] ROS1 oder [2] ROS2 oder [0] \n"

read -rsn1 input
if [ "$input" = "1" ]; then
    echo "sourcing ROS1 environment"
    source /opt/ros/noetic/setup.bash
    PS1="\[\033[01;32m\]\u@\h\[\033[0m\]:ROS1:\[\033[01;34m\]\w\[\033[0m\]\$ "
    settitle ROS1
fi
if [ "$input" = "2" ]; then
    echo "sourcing ROS2 environment"
    source /opt/ros/foxy/setup.bash
    PS1="\[\033[01;32m\]\u@\h\[\033[0m\]:ROS2:\[\033[01;34m\]\w\[\033[0m\]\$ "
    settitle ROS2
fi

Wo hat Python seine Pakete installiert?

dab@D4rkph7ber:~$ python3 -m site
sys.path = [
    '/home/dab',
    '/opt/ros/noetic/lib/python3/dist-packages',
    '/usr/lib/python38.zip',
    '/usr/lib/python3.8',
    '/usr/lib/python3.8/lib-dynload',
    '/home/dab/.local/lib/python3.8/site-packages',
    '/usr/local/lib/python3.8/dist-packages',
    '/usr/lib/python3/dist-packages',
]
USER_BASE: '/home/dab/.local' (exists)
USER_SITE: '/home/dab/.local/lib/python3.8/site-packages' (exists)
ENABLE_USER_SITE: True

auf dem Pi wohnt anki_vector unter .local/lib/python3.8/site-packages

im Modul events.py ab Zeile 249

except TypeError:
          self.logger.warning('Unknown Event type')

ersetzen durch

except TypeError:
          pass