[X-NUCLEO-53L4A3 TOF Evaluation Board] Gesture Control Servo
[Copy link]
[X-NUCLEO-53L4A3 TOF Evaluation Board] Gesture Recognition https://en.eeworld.com/bbs/thread-1299302-1-1.html
[X-NUCLEO-53L4A3 TOF Evaluation Board] TouchGFX Distance Scale - Sensor - Electronic Engineering World - Forum
[X-NUCLEO-53L4A3 TOF Evaluation Board] Unboxing Experience and Conventional Distance Measurement - Sensor - Electronic Engineering World - Forum
In the previous three articles, I implemented gesture recognition. This article will share gesture control of servos.
【Servo control implementation】
The direction of the servo is controlled by a 20ms period PWM waveform. Here I first use TIM2 to output a 50Hz PWM waveform through PA1.
First, configure TIM2 CH2 to output a 50Hz PWM waveform. The configuration is shown in the figure below:
By checking the bus clock of the bus TIM, we configure it to 84MHz clock, then configure it to 84000 000/8400-1, which is 10Khz, and divide it by Period 200 to get a period of 50Hz. After generating the code, we also need to manually add code to set the startup of TIM2, that is:
HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_2); //开启定时器2的通道2
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_2, 200/20); //设置他的占空比为 1ms.
In the gesture recognition function, we set different direction gestures, modify the duty cycle, and finally realize real-time control of the servo:
ret = isApproximatelyLinear(timeData, distanceData, DATA_POINTS);
if (ret <100)
{
printf("物体静止或近似静止。\n");
}
else if( ret >=100 && ret <5000)
{
double sum_t = 0, sum_d = 0, sum_tt = 0, sum_td = 0;
for (int i = 0; i < DATA_POINTS; i++) {
sum_t += timeData[i];
sum_d += distanceData[i];
sum_tt += timeData[i] * timeData[i];
sum_td += timeData[i] * distanceData[i];
}
double denominator = DATA_POINTS * sum_tt - sum_t * sum_t;
double a = (DATA_POINTS * sum_td - sum_t * sum_d) / denominator;
if (a > 0) {
printf("物体向前运动。\n");
pwmvalue --;
if(pwmvalue <5)
pwmvalue = 5;
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_2, pwmvalue); //修改舵机运行位置
} else if (a < 0) {
printf("物体向后运动。\n");
pwmvalue ++;
if(pwmvalue >25)
pwmvalue = 25;
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_2, pwmvalue); //修改舵机运行位置
} else {
printf("物体静止或近似静止。\n");
}
} else {
printf("数据点不近似在一条直线上,无法简单判断运动方向。\n");
}
Through the schematic diagram, we find the position of A1 and connect A1 to the signal input position of the servo:
【Effect】
After downloading the program, you can control the servo by gestures. See the video for details:
20241117115525
|