#include "Arduino.h" // this #ifndef stops this file // from being included mored than…
- User
#include "Arduino.h" // this #ifndef stops this file // from being included mored than // once by the compiler.
// ALL MEASUREMENTS IN mm AT THE MOMENT!!!
#ifndef _KINEMATICS_H #define _KINEMATICS_H
define POSITION_UPDATE 100 // how often we will update the position, testing will show if 100 milliseconds (0.1 seconds) is a sensible frequency.
include "encoders.h"
include "motors.h"
Motors_c motors;
// Class to track robot position. class Kinematics_c { public: unsigned long kinematics_ts = 0; // for time stamping!
// We need some global class variables to refer to, specifically, X_pos, Y_pos and theta (angle rotation) // all three must start at zero each time the class is initialised as we always want robot starting at origin of reference frame. // all three must be floats as they will not necessarily be whole numbers. max value of float is 3 x 10^38, should be big enough I think... float X_pos = 0.0; // updated by calculating delta_X float Y_pos = 0.0; // updated by calculating delta_Y float Theta = 0.0; // updated by calculating delta_Theta float Theta_Home = 0.0; // angle robot must turn to in order to return to start in a straight line. volatile long previous_count_wheel_left = 0; // we require this for our change in count value, it starts at zero and is updated in updated function volatile long previous_count_wheel_right = 0; // we require this for our change in count value, it starts at zero and is updated in updated function // Constructor, must exist. Kinematics_c() { } // Use this function to update // your kinematics void update() { // Record the time of this execution for coming calucations ( _ts = "time-stamp" ) unsigned long current_ts; current_ts = millis(); // Calculate elapsed time. unsigned long elapsed_t; elapsed_t = current_ts - kinematics_ts; if( elapsed_t > POSITION_UPDATE ) { // declaring my change in x, y, theta variables. this is in reference frame float delta_X; float delta_Y; float delta_Theta; // declaring my dimensional variables, fixed. float r = 16.0; // wheel radius mm. float l = 44.6; // dist from centre of robot to centre of wheel mm. Calculated in book. float counts_per_rev = 358.3; // counts per revolution of each wheel. float circumference = 100.5309649; // wheel circumference is 32pi mm. float dist_per_count = circumference/counts_per_rev; // distance travelled per count. // now get change in counts since last update: long left_change = count_wheel_left - previous_count_wheel_left; long right_change = count_wheel_right - previous_count_wheel_right; // now calculate the change in x position and theta in local frame (change in y local is always zero) // this is essentially the average of the change in counts times by distance per count. float delta_X_local = (0.5)*(left_change + right_change)*dist_per_count; // note, didn't like fraction co-efficient for float. Decimal better.// THIS MINUS MAKES FORWARD X AND Y POSITIVE. delta_Theta = (0.5)*(1/l)*(right_change - left_change)*dist_per_count; // local delta theta is same as reference delta theta as bot starts lined up with x ref as well as x local. // calculte our delta values -> eqns are in lab 6, kinematics section. delta_X = delta_X_local*cos(Theta); delta_Y = delta_X_local*sin(Theta); // we have delta theta already. // Update our refernce frame kinematics. X_pos = X_pos + delta_X; Y_pos = Y_pos + delta_Y; Theta = Theta + delta_Theta; // THIS WORKS!! // condition to prevent theta from exceeding +-180. WILL ONLY WORK IF ROBOT DOES NOT COMPLETE MORE THAN ONE FULL CIRCLE IN TIME OF POSITION UPDATE. if(abs(Theta*180/3.14159) >= 180){ // we want theta in this range for our full circle. see angles A4 page if(Theta > 0){ Theta = Theta - 2*(3.14159); // left is 2pi -> degree equivalent: If theta reads 185, it should be 185 - 365 = -175 } else{ Theta = Theta + 2*(3.14159); // left is 2pi -> degree equivalent: If theta reads -185, it should be (-185) + 360 = 175 } } // printing our counts out - WORKING!! // Serial.println("Right Wheel stats: current, prev, difference "); // Serial.print(count_wheel_right); // Serial.print(", "); // Serial.print(previous_count_wheel_right); // Serial.print(", "); // Serial.print(right_change); // Serial.print("\n"); // Serial.println("Left Wheel stats: current, prev, difference "); // Serial.print(count_wheel_left); // Serial.print(", "); // Serial.print(previous_count_wheel_left); // Serial.print(", "); // Serial.print(left_change); // Serial.print("\n"); // Serial.print("\n"); // Serial.println("x position is: "); // Serial.print(X_pos); // Serial.print("\n"); // Serial.println("y position is: "); // Serial.print(Y_pos); // Serial.print("\n"); // Serial.println("angle from start is: "); // Serial.print(Theta); // // At the end of the update, set the previous counts for when the next update runs. previous_count_wheel_left = count_wheel_left; previous_count_wheel_right = count_wheel_right; // also update our timestamp at the end. kinematics_ts = millis(); } } - Assistant
这段代码定义了一个名为Kinematics_c的类,用于跟踪机器人的位置。该类包含了一些成员变量和成员函数。
成员变量:
- kinematics_ts: 用于时间戳的变量。
- X_pos: 机器人的x坐标。
- Y_pos: 机器人的y坐标。
- Theta: 机器人的角度旋转。
- Theta_Home: 机器人返回出发点的角度旋转。
- previous_count_wheel_left: 左轮的上一个计数值。
- previous_count_wheel_right: 右轮的上一个计数值。
成员函数:
- update(): 用于更新机器人的运动学模型。根据计算的数据,更新机器人的位置和角度。
在update()函数中,首先记录当前时间戳和经过的时间,然后计算左右轮的计数值的变化量。根据变化量和已知的机器人尺寸,计算本地坐标系下的位置变化量和角度变化量。再根据参考坐标系和本地坐标系的转换关系,计算出在参考坐标系下的位置变化量和角度变化量。最后更新机器人在参考坐标系下的位置和角度,并更新之前的计数值和时间戳。
此外,代码中还包含了一些引用的头文件和全局对象的定义。
总结起来,这段代码定义了一个机器人运动学类,并提供了更新机器人位置和角度的功能。
- User
把注释翻译成中文
- Assistant
- User
In file included from C:\Users\曾卓\Desktop\bb\bb.ino:12:0: C:\Users\曾卓\Desktop\bb\fsm.h: In member function 'int FSM_c::update_state(int)': C:\Users\曾卓\Desktop\bb\fsm.h:196:118: warning: integer overflow in expression [-Woverflow] 这是什么意思
- Assistant
这个警告意味着在FSM_c类的update_state()成员函数中,有一个表达式存在整数溢出的风险。
整数溢出是指当一个整数的计算结果超过了它所能表示的范围时发生的情况。在这种情况下,计算结果会被截断或溢出,导致结果不准确。
在警告中,它指出在第196行的代码中存在一个表达式,该表达式可能会导致整数溢出。具体地说,这个表达式中的一个或多个操作数的值超过了所能表示的范围,因此计算结果可能是不准确的。
为了解决这个警告,你可以检查那个表达式的代码,并确保操作数的值处于安全的范围内,或者使用更大范围的数据类型来存储计算结果。如果操作数的值在你的情况下会超过int类型的范围,你可以考虑使用long或其他更大的整数类型来避免溢出。
- User
或运算符是什么
- Assistant
或运算符(|)是一种逻辑运算符,用于对两个操作数进行逻辑或操作。它应用于两个布尔操作数,如果其中一个操作数为真(非零),则结果为真(非零)。以下是或运算符的几个例子:
-
表达式:a | b
- 如果a为真或者b为真,则结果为真。
- 如果a为假且b为假,则结果为假。
-
示例1:
int a = 5; int b = 3; int result = a | b; // result的值为7,因为5的二进制表示为101,3的二进制表示为011,按位或后得到111,即十进制的7。 -
示例2:
bool c = true; bool d = false; bool result = c | d; // result的值为true,因为其中一个操作数为真(非零)。
注意事项:
- 或运算符是按位操作的,即它对操作数的每一个对应位进行逻辑或运算。
- 或运算符的结果将与操作数的数据类型相同。如果操作数为整数,结果也将是整数;如果操作数为布尔值,结果也将是布尔值。
-