机械手的控制程序
/* P的范围最好是 按照下面给出的测试范围给值
*/
#001P0800T1000!
#001P1500T1000! //单个控制 (大拇指) P的数值越小弯曲程度越大 2100-600
#001P2100T1000!
#002P0800T1000!
#002P1500T1000! //单个控制 (食指) P的数值越大弯曲程度越大 0800-2050
#002P2050T1000!
#003P0800T1000!
#003P1500T1000! //单个控制 (中指) P的数值越大弯曲程度越大 0800-2100
#003P2100T1000!
#004P0800T1000!
#004P1500T1000! //单个控制 (无名指) P的数值越大弯曲程度越大 0800-2200
#004P2200T1000!
#005P0800T1000!
#005P1500T1000! //单个控制 (小拇指) P的数值越大弯曲程度越大 0800-2100
#005P2100T1000!
/* Arduino Long Range Wireless Communication using HC-12
Example 01
by Dejan Nedelkovski, www.HowToMechatronics.com
*/
#include <SoftwareSerial.h>
SoftwareSerial HC12(10, 11); // HC-12 TX Pin, HC-12 RX Pin
byte analog_pin[5] = {A1, A2, A3, A4, A5}; //定义电位器引脚数组
int sensorValue[6]={0,0,0,0,0,0}; //大拇指读取的模拟量
int sensorMaxValue[6]={0,0,0,0,0,0};
int sensorMinValue[6]={1024,1024,1024,1024,1024,1024};
int FingerBend[6]={0,0,0,0,0,0};
char cmd_return[256];
void setup() {
Serial.begin(115200); // Serial port to computer
HC12.begin(115200); // Serial port to HC12
}
void loop() {
// while (HC12.available()) { // If HC-12 has data
Serial.println(HC12.read()); // Send the data to Serial monitor
// Serial.println("有数据啦");
// }
static u32 systick_ms_bak = 0;
if(millis() - systick_ms_bak > 100) { //25ms为一个执行周期
systick_ms_bak = millis();
// 大拇指
sensorValue[1] = analogRead(A1);
Serial.print(sensorValue[1]); // Send the data to Serial monitor
Serial.print(" max = "); Serial.print(sensorMaxValue[1]);Serial.print(" min = "); Serial.print(sensorMinValue[1]); Serial.print(" ");
sensorMinValue[1] =min(sensorValue[1],sensorMinValue[1]);
sensorMaxValue[1] =max(sensorValue[1],sensorMaxValue[1]);
int temp = map(sensorValue[1],sensorMinValue[1],sensorMaxValue[1],600,2100);//因为模拟读取的是1023,输出却是255,用map函数转换
FingerBend[1] = 2700-temp;
// 食指
// sensorValue[2] = analogRead(A2);
// sensorMinValue[2] =min(sensorValue[2],sensorMinValue[2]);
// sensorMaxValue[2] =max(sensorValue[2],sensorMaxValue[2]);
// FingerBend[2]= map(sensorValue[2],sensorMinValue[2],sensorMaxValue[2],800,2050);//因为模拟读取的是1023,输出却是255,用map函数转换
sprintf(cmd_return,"#001P%04dT0500!",FingerBend[1]);
// sprintf(cmd_return,"{#001P%04dT0050!#002P%04dT0050!#003P%04dT0050!#004P%04dT0050!#005P%04dT0050!}",
// FingerBend[1],FingerBend[2],FingerBend[3],FingerBend[4],FingerBend[5]);
Serial.println(cmd_return); // Send the data to Serial monitor
HC12.write(cmd_return); //这个串口的发送是通过手套的HC-12无线通讯模块发送的
}
}
第四节
// A0—A5,是模拟输入 , 3,4,5,9,10,11是模拟输出口
void analogRead(pin); //读取模拟量电压值,返回一个0到1023的数值(int型)
void anglogWrite(pin,value); //输出到引脚,PWM的信号频率约为490赫兹脚位通常在3,5,6,9,10,11,value变数范围0—255,而0——255对应的是 0—5V电压
int map(value,fromlow,fromHigh,toLow,toHigh),把一个数从一个范围变换到另一个范围就是将value变数依照fromlow与fromHigh范围,对等转化至toLow 与toHigh。
/* Arduino Long Range Wireless Communication using HC-12
Example 01
by Dejan Nedelkovski, www.HowToMechatronics.com
*/
#include <SoftwareSerial.h>
SoftwareSerial HC12(10, 11); // HC-12 TX Pin, HC-12 RX Pin
byte analog_pin[5] = {A1, A2, A3, A4, A5}; //定义电位器引脚数组
int sensorValue[6]={0,0,0,0,0,0}; //手指读取的模拟量
int sensorMaxValue[6]={0,0,0,0,0,0};
int sensorMinValue[6]={1024,1024,1024,1024,1024,1024};
int FingerBend[6]={0,0,0,0,0,0};
char cmd_return[256];
byte readBuf;
void setup() {
Serial.begin(115200); // Serial port to computer
HC12.begin(115200); // Serial port to HC12
}
void loop() {
while (HC12.available()) { // If HC-12 has data
readBuf = HC12.read();
// Serial.println(HC12.read()); // Send the data to Serial monitor
// Serial.println("\n");
Serial.println("有数据啦");
}
static u32 systick_ms_bak = 0;
if(millis() - systick_ms_bak > 100) { //25ms为一个执行周期
systick_ms_bak = millis();
// 大拇指
sensorValue[1] = analogRead(A1);
// Serial.print(" 1max = "); Serial.print(sensorMaxValue[1]); Serial.print(" 1min = "); Serial.print(sensorMinValue[1]); Serial.print(" ");
sensorMinValue[1] =min(sensorValue[1],sensorMinValue[1]);
sensorMaxValue[1] =max(sensorValue[1],sensorMaxValue[1]);
int temp = map(sensorValue[1],sensorMinValue[1],sensorMaxValue[1],600,2100);//因为模拟读取的是1023,输出却是255,用map函数转换
FingerBend[1] = 2700-temp;
// 食指
readFingerBend(2);
readFingerBend(3);
readFingerBend(4);
readFingerBend(5);
for(int i=1;i<=5;i++){
Serial.print(sensorValue[i]); // Send the data to Serial monitor
Serial.print(" ");
}
Serial.print("\n");
//
// sprintf(cmd_return,"#001P%04dT0500!",FingerBend[1]);
// Serial.println(cmd_return);
// HC12.write(cmd_return);
// memset(cmd_return, 0, sizeof(cmd_return));
sprintf(cmd_return,"{#001P%04dT0500!#002P%04dT0500!#003P%04dT0500!#004P%04dT0500!#005P%04dT0500!}",
FingerBend[1],FingerBend[2],FingerBend[3],FingerBend[4],FingerBend[5]);
Serial.println(cmd_return); // Send the data to Serial monitor
HC12.write(cmd_return); //这个串口的发送是通过手套的HC-12无线通讯模块发送的
}
}
void readFingerBend(int index){
// 食指
sensorValue[index] = analogRead(index);
// Serial.print(sensorValue[index]); // Send the data to Serial monitor
// Serial.print(" "); Serial.print(index);Serial.print("max = "); Serial.print(sensorMaxValue[index]);
// Serial.print(index);Serial.print("min = "); Serial.print(sensorMinValue[index]); Serial.print(" ");
sensorMinValue[index] =min(sensorValue[index],sensorMinValue[index]);
sensorMaxValue[index] =max(sensorValue[index],sensorMaxValue[index]);
FingerBend[index]= map(sensorValue[index],sensorMinValue[index],sensorMaxValue[index],800,2050);//因为模拟读取的是1023,输出却是255,用map函数转换
}
输出数据
520 568 329 383 376
519 569 329 383 375
519 568 328 383 375
519 568 328 383 375
520 568 329 384 375
519 568 329 383 375
520 569 328 383 376
520 568 329 383 375
519 568 328 383 376
519 568 328 383 376
519 568 328 383 375
519 568 328 383 375
519 568 328 383 376
519 568 328 383 375
519 568 329 383 375
519 569 329 383 375
519 569 329 383 376
519 568 329 383 375
519 569 328 384 376
520 569 329 383 375
519 568 329 384 375
519 568 329 383 376
519 568 329 383 376
519 568 329 383 375
519 568 329 383 376
519 568 329 383 375
519 568 328 383 376
519 569 329 383 376
519 568 329 383 376
519 568 328 383 376
519 568 328 383 377
519 569 328 383 376
519 568 328 383 375
519 568 328 383 376
519 568 327 383 375
519 568 327 383 375
519 568 328 384 375
519 568 328 383 376
519 568 328 383 375
519 568 328 383 376
519 568 328 383 375
519 569 328 383 376
519 568 328 383 377
519 569 328 384 376
519 568 327 383 376
519 573 328 383 373
517 570 328 383 375
398 521 266 412 365
399 575 247 414 367
466 538 291 400 367
474 550 307 406 367
509 554 309 405 367
582 554 311 404 367
732 560 312 398 368
735 562 313 396 367
745 561 314 397 368
731 561 314 399 368
714 562 315 402 368
395 560 292 389 367
477 559 321 395 369
483 562 324 400 369
482 563 325 399 369
481 563 325 399 368
483 563 325 396 369
477 535 288 389 369
489 573 296 388 367
493 580 30