机器人编程入门教程

服务机器人 2021-10-07 10:51www.robotxin.com女性服务机器人
柏林工大机器人(机械手臂)课程,本课程实验将在VM虚拟机的Ubuntu运行。 本节是编程基础知识。
C++
unix编译和运行程序
g++ helloorld.cpp -o helloorld
./helloorld
debug
g++ -g helloorld.cpp -o helloorld
gdb ./helloorld
gdb备忘单
r                       ==>run运行,遇到error时暂停
break helloorld.cpp:4  ==>到第4行暂停
break 4                 ==>到第4行暂停
s                       ==>step逐步运行
n                       ==>next在一个函数中运行下一步
p var                   ==>打印var的值
c                       ==>continue继续运行程序
bt                      ==>backtrace显示所有的函数调用栈帧的信息,每个帧一行
frame 3                 ==>通过帧编号来选择帧,帧编号可以通过bt来查看。跳到第3帧
list                    ==>当前帧下的代码
延伸
bt
    显示所有的函数调用栈帧的信息,每个帧一行。
bt n
    显示栈定的n个帧信息。
bt -n
    显示栈底的n个帧信息。
bt full
    显示栈中所有帧的完全信息如:函数参数,本地变量
bt full n
    用法同上。
bt full -n
frame n
f n
    通过帧编号来选择帧,帧编号可以通过bt来查看。
f addr
    通过帧地址来选择帧,帧编号可以通过bt来查看。
up n
    在栈中向上移动n个帧。即向着最外层移动n个帧。
don n
    与 up 反方向移动n个帧。
up-silently n
don-silently n
    在栈中移动n个帧,但是不打印信息
frame
f
    打印帧内函数的信息。
info frame
info f
    打印帧的信息。
函数声明和函数定义
Declaration 函数声明指在头文件中的函数名称和类型以及参数; Definition 函数定义指在cpp文件中的具体的函数内容; 编译附属函数
g++ someFunction2.cpp main.cpp -o someFunction2
命名空间
Namespace 定义自己的命名空间
namespace myns{
    int po(int n){
        return n*n;
    }
}
//使用方法
//不含命名空间
myns::po(5);
//使用命名空间
using namespace myns;
po(5);
数组
Arrays
int nums[10]; // 错误用法
int* numbers = ne int[10]; // 正确用法
delate[] numbers;//用完之后删除
g++ -Wall -pedantic arrays_variable.cpp -o arrays.variable
./arrays_variable 20
g++ -pedantic 会严格执行编译过程
Stack 和 Heap
Stack是静态存储,变量的大小在编译时的过程中是已知的, 变量在同一层级中可用(或内层), 通过操作系统管理。
int numbs[10];
Heap是动态存储,变量的大小仅在运行过程中已知,通过用户管理
int size = 10;
int* numbs = ne int[size];
//...
delete[] numbs;
numbs[0] = 10;
numbs[2] = 5;
指针
cout << numbs << endl; //得到numb在heap上的地址
cout << (*numbs) << endl; //得到numbs的第一个元素的值
cout << numbs[2] << endl;
cout << ((*numbs)+2) << endl;
cout << &(*numbs) << endl; //得到numbs在heap上的地址(同numbs第一个元素的地址)
cout << &size << endl; //得到size在stack上的地址
类: 声明
protected中的变量可以在本类和继承于它的类中使用。
construtor结构体与类名一致,用于初始化类,在实例化时会运行。
destructor用于类结束时运行,可以删除heap上所有的成员(member)。
// .h
class IntList{
protected: //private: protected or private member variables
int max_size;
int* members;
int current_size;
public:
    IntList(int max_size_);//constructor
    virtual ~IntList(); // destructor
    //public member functions
    bool add(int number);
};
类: 定义
// .cpp
#include "IntList.h"
IntList::IntList(int max_size):max_size(max_size_), current_size(0){
    members = ne int[max_size];
}
IntList::~IntList(){
    delete[] members;
}
bool IntList::add(int number){
    if(this->current_size+1 >= this->max_size){
        return false;
    }
    members[current_size] = number;
    current_size++;
    return true;
}
类: 实例化
#include <iostream>
#include "IntList.h"
int main(int argc, char* argv[]){
    IntList list(10); // declare and init on stack
    list.add(5);
    list.add(29);
    std::cout << list.elem(0) << std::endl;
    IntList* list2; // declare pointer
    list2 = ne IntList(10); // init on heap
    (*list2).add(5); // dereference pointer
    list->add(29); 
    std::cout << list2->elem[0] << std::endl;
    return 0;
}
//(*pointerToObj).method() = pointerToClass->method()
类: 继承和多态
class A{
public:
    virtual void alpha(){
        cout << "A:alpha" << endl;
    }
};
class B: public A {
public:
    void alpha(){
        cout << "B:alpha" << endl;
    }
};
int main( int argc, char* argv[] ){
    A *class1 = ne A;
    B *class2 = ne B;
    class1->alpha();
    class2->alpha();
    return 0;
}
STL-c++的标准库
#include <iostream>
#include <vector>
#include <string>
int main (int argc, char* argv[] ){
    std::string name1("Klaus");
    std::string name2("Peter");
    std::vector<std::string> names;
    names.push_back(name1);
    names.push_back(name2);
    std::cout << names[0] << std::endl;
}
使用std::string 而不是 char*
使用std::vector 而不是 array
有很多数据结构(containers)和操作
Hash table(std::map) 哈希表
Sorting(#include ) 排序算法
Tuples
...
Containers are templated
std::vector<std::vector<int> > int2d; // 这里两个 >> 中间要有空格,否则在unix系统中有问题
std::cout << int2d[0][1] << std::endl;
 

Copyright © 2016-2025 www.robotxin.com 人工智能机器人网 版权所有 Power by