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.
Diff: main.cpp
- Revision:
- 4:00ac3d093b24
- Parent:
- 3:814cb36d0f90
- Child:
- 5:62de7a36d3af
diff -r 814cb36d0f90 -r 00ac3d093b24 main.cpp
--- a/main.cpp Thu Sep 22 16:18:08 2016 +0000
+++ b/main.cpp Mon Sep 26 14:01:47 2016 +0000
@@ -1,16 +1,20 @@
#include "mbed.h"
#include "stdio.h"
#include "math.h"
+#include "USBJoystick.h"
Serial pc(USBTX,USBRX);
+USBJoystick joystick;
DigitalInOut pingPin1(p18);
DigitalInOut pingPin2(p19);
Timer tmr1, tmr2,tmr;
-Timeout timeouter;
+Timeout timeouter1,timeouter2;
int flag=0;
-float sum1=0,sum2=0,count1=0,count2=0,timecount1=0,timecount2=0,timesum1=0,timesum2=0;
+float sum1=0,sum2=0,count1=0,count2=0,timecount1=0,timecount2=0,timesum1=0,timesum2=0,v,argv;
+int16_t x=0,y=0,i=2;
+int8_t button = 0;
long microsecondsToCentimeters(long microseconds);
void reset()
@@ -20,9 +24,14 @@
sum1=0,sum2=0,count1=0,count2=0,timecount1=0,timecount2=0,timesum1=0,timesum2=0;
}
+void output()
+{
+ printf("\n|v|: %lf theta: %lf\n",v,argv); //最終的にだすやつ
+}
+
int main()
{
- float avg1,avg2,timeavg1,timeavg2,deltat,a,b,v,argv;
+ float avg1,avg2,timeavg1,timeavg2,deltat,a,b;
pc.baud(115200);
while (1) {
long duration1, duration2,USSDistance1, USSDistance2, cm1, cm2, time1,time2;
@@ -51,11 +60,13 @@
flag=1;
tmr.reset();
tmr.start();
- timeouter.attach(&reset, 5);
+ timeouter2.attach(&output, 1);
+ timeouter1.attach(&reset, 5);
}
if(flag==1) {
- timeouter.detach();
- timeouter.attach(&reset, 5);
+ timeouter1.detach();
+ timeouter2.attach(&output, 1);
+ timeouter1.attach(&reset, 5);
}
time1=tmr.read_ms();
printf("1: %d %d[ms]", cm1,time1);
@@ -65,12 +76,11 @@
sum1=sum1+cm1; //cm1の値を合計する
avg1=sum1/count1; //cm1の平均
- printf(" avg1: %4.2f",avg1);
-
+ printf(" avg1: %f",avg1);
+
timesum1=timesum1+time1; //time1を合計する
timeavg1=timesum1/timecount1; //time1の平均
- printf(" timeavg1: %4.2f[ms]\n",timeavg1);
-
+ printf(" timeavg1: %f[ms]\n",timeavg1);
}
wait_ms(10);
@@ -96,39 +106,37 @@
cm2 = microsecondsToCentimeters(USSDistance2);
if(15<cm2&&cm2<40) {
time2=tmr.read_ms();
- printf(" 2: %d %d[ms]",cm2,time2);
+ printf(" 2: %d %d[ms]",cm2,time2);
count2++;
timecount2++;
-
+
sum2=sum2+cm2; //cm2の値を合計する
avg2=sum2/count2; //cm2の平均
- printf(" avg2: %4.2f",avg2);
-
+ printf(" avg2: %f",avg2);
+
timesum2=timesum2+time2; //time2を合計する
timeavg2=timesum2/timecount2; //time2の平均
- printf(" timeavg2:%4.2f[ms]\n",timeavg2);
-
+ printf(" timeavg2:%f[ms]\n",timeavg2);
+
deltat=timeavg2-timeavg1; //Δtを求める
- printf("\ndeltat: %4.2f\n",deltat);
-
+ printf("\ndeltat: %f\n",deltat);
+
a=pow((avg2-avg1)/deltat,2);
b=pow(10/deltat,2);
v=sqrt(a+b); //|v|を求める
-
+
argv=atan(10/(avg2-avg1)); //θを求める
-
- printf("\n|v|: %lf theta: %lf\n",v,argv);
-
-
}
wait_ms(10);
-
-
}
+ button = (i >> 8) & 0x0F;
+ x = argv;
+ y = v;
+
+ joystick.update(x, y, button);
}
-
long microsecondsToInches(long microseconds)
{
return microseconds / 74 / 2;