Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
- Committer:
- takaoha
- Date:
- 2019-12-24
- Revision:
- 2:33681dfc2aa5
- Parent:
- 1:fa3a640a55e0
- Child:
- 3:e356b3e7ecfd
File content as of revision 2:33681dfc2aa5:
#include "mbed.h"
#include "math.h"
AnalogIn fsr(PC_0);
DigitalInOut name(PC_7);//EN_IN(HIGHでICS機器に送信するモード、LOWでICS機器から受信するモードにするピン)と繋いでいる
Serial master(PA_9, PA_10);//シリアル通信を担当するピン
Serial pc(USBTX,USBRX);
bool setpos(int deg,int id);
void getID();
void getpos(int id);
int txlen=3;
int rxlen=3;
int _id;
int main() {
pc.printf("hello\r\n");
//master.baud(1250000);//クロックレートの設定
master.baud(112500);
master.format(8, Serial::Even, 1);//通信方式の設定
name.output();//p8を出力モードに
int deg=0;
char degpart[3];
int id;
char a;
while(1){
pc.printf("choose1 or 2 or3\r\n");
a=pc.getc();
switch(a){
case '1':
pc.printf("degree\r\n");
for(int i=0;i<3;i++){
degpart[i]=pc.getc()-48;
printf("%d",degpart[i]);
}
printf("\r\n");
deg=100*degpart[0]+10*degpart[1]+degpart[2];
//pc.scanf("%d\r\n",deg);
pc.printf("%d\r\n",deg);
pc.printf("id\r\n");
id=pc.getc()-48;
//id+=48;
pc.printf("%d\r\n",id);
setpos(deg,id);
//getID();
wait(1.0);
getpos(id);
break;
case '2':
double i;
i=fsr.read();
deg=i*270-135;
pc.printf("%d\r\n",deg);
setpos(deg,5);
break;
case '3':
pc.printf("id\r\n");
id=pc.getc()-48;
//id+=48;
pc.printf("%d\r\n",id);
getpos(id);
break;
default:
break;
}
}
}
bool setpos(int deg,int id){
name=1;
int txdata[txlen];
int rxdata[rxlen];
if(deg>135 || deg<-135){
pc.printf("failed\r\n");
return 0;
}
int pos=deg*29.633;
pos=pos+7500;
//pc.printf("%d\r\n",pos);
txdata[0]=0x80+id;
txdata[1]=((pos >> 7) & 0x007F);
txdata[2]=(pos & 0x007F);
for(int i=0;i<txlen;i++){
master.putc(txdata[i]);
}
//wait(0.00002);
wait(0.0002);
name=0;
for(int i=0;i<rxlen;i++){
rxdata[i]=master.getc();
}
int degnow;
degnow=((rxdata[1] << 7) & 0x3F80) + (rxdata[2] & 0x007F);
degnow=(degnow-7500)/29.6;
pc.printf("%d\r\n",rxdata[0]);
pc.printf("%d\r\n",rxdata[1]);
pc.printf("%d\r\n",rxdata[2]);
pc.printf("角度%d\r\n",degnow);
}
void getpos(int id){
name=1;
unsigned char txdata[txlen-1];
unsigned char rxdata[rxlen+1];
int degnow;
txdata[0]=0xA0+id;
txdata[1]=0x05;
for(int i=0;i<txlen-1;i++){
master.putc(txdata[i]);
}
wait(0.0002);
name=0;
for(int i=0;i<rxlen+1;i++){
rxdata[i]=master.getc();
}
pc.printf("%d\r\n",rxdata[0]);
degnow=((rxdata[2] << 7) & 0x3F80) + (rxdata[3] & 0x007F);
degnow=(degnow-7500)/29.6;
pc.printf("%d\r\n",degnow);
}
void getID()
{
int id;
name=1;
master.putc(0xFF);
wait(0.00001);
master.putc(0x0);
wait(0.00001);
master.putc(0x0);
wait(0.00001);
master.putc(0x0);
wait(0.00002);
name=0;
for(int i=0;i<5;i++)
{
id = (master.getc() & 0x1f);
}
//id=master.getc();
printf("%d\r\n",id);
wait_ms(500);
}