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.
Revision 10:1bbe0d41813e, committed 2019-06-27
- Comitter:
- DaichiArai
- Date:
- Thu Jun 27 07:56:29 2019 +0000
- Parent:
- 9:a3a2fb1a3ba7
- Commit message:
- 2019/06/07
Changed in this revision
diff -r a3a2fb1a3ba7 -r 1bbe0d41813e mdc.cpp --- a/mdc.cpp Tue Apr 30 04:54:26 2019 +0000 +++ b/mdc.cpp Thu Jun 27 07:56:29 2019 +0000 @@ -2,20 +2,20 @@ #include "math.h" #include "mdc.h" +extern I2C mdc_i2c; + motor::motor(I2C &i2c, int addr): mdc_i2c(i2c) { address = addr; data[0] = 0; data[1] = 0; - data[2] = 0; - data[3] = 0; data_reset[0] = 0xff; data_reset[1] = 0; val = 1; } int motor::move(float pwm){ - write_pwm = (int)(pwm*125); + write_pwm = (int)(pwm*127); if(pwm == 1000){ md_data[0] = MOTOR_FREE; write_pwm = 0; @@ -42,35 +42,14 @@ mdc_i2c.write(address,data_reset,2); } -//スピードを算出して、スピードの値を返却 -float motor::spd(){ - val = mdc_i2c.read(address,data,10,false); - encoder_spd = (double)data[SPD_H] + (double)data[SPD_M]/100 + (double)data[SPD_L]/10000; - if(data[SPD_MINUS] == 1){ - encoder_spd *= -1; - } - return encoder_spd; -} - -float motor::position(){ - val = mdc_i2c.read(address,data,10,false); - position_angle = ((double)data[ANGLE_H])+((double)data[ANGLE_M]/100)+((double)data[ANGLE_L]/10000); - if(data[ANGLE_MINUS] == 1){ - position_angle *= -1; - position_angle += DPI; - } - return position_angle; -} - //角度を算出して角度を返す(回転数も考慮 float motor::angle(){ - val = mdc_i2c.read(address,data,10,false); - count = ((data[COUNT_MINUS])?(-1):(1))*data[COUNT_SPIN]; + val = mdc_i2c.read(address,data,2,false); + + count = (int)(data[0]) - 128; - float angle_conv = ((double)data[ANGLE_H])+((double)data[ANGLE_M]/100)+((double)data[ANGLE_L]/10000); - if(data[ANGLE_MINUS] == 1){ - angle_conv *= -1; - } + position_data = (int)(data[1])*3.6; + angle_data = (float)count*36 + position_data/10; - return(angle_conv+count*6.28); + return angle_data; }
diff -r a3a2fb1a3ba7 -r 1bbe0d41813e mdc.h --- a/mdc.h Tue Apr 30 04:54:26 2019 +0000 +++ b/mdc.h Thu Jun 27 07:56:29 2019 +0000 @@ -31,19 +31,14 @@ int count; float angle(); - float spd(); - float position(); - //旧作との互換性を確保するためのメソッド------ - float spd_e(); - float angle_e(); - float position_e(); - //-------------------------------------- + float angle_data; + float position_data; //----------------------------------------------------------------------------- + char data[2]; private: char data_reset[2]; char md_data[2]; - char data[10]; void result(); int write_pwm;
diff -r a3a2fb1a3ba7 -r 1bbe0d41813e mdc_old.cpp --- a/mdc_old.cpp Tue Apr 30 04:54:26 2019 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,37 +0,0 @@ -#include "mbed.h" -#include "math.h" -#include "mdc.h" - -//回転回数を数える -//スピードを算出して、スピードの値を返却 -float motor::spd_e(){ - val = mdc_i2c.read(address,data,10,false); - encoder_spd = (double)data[SPD_H] + (double)data[SPD_M]/100 + (double)data[SPD_L]/10000; - if(data[SPD_MINUS] == 1){ - encoder_spd *= -1; - } - return encoder_spd; -} - -float motor::position_e(){ - val = mdc_i2c.read(address,data,10,false); - position_angle = ((double)data[ANGLE_H])+((double)data[ANGLE_M]/100)+((double)data[ANGLE_L]/10000); - if(data[ANGLE_MINUS] == 1){ - position_angle *= -1; - position_angle += DPI; - } - return position_angle; -} - -//角度を算出して角度を返す(回転数も考慮 -float motor::angle_e(){ - val = mdc_i2c.read(address,data,10,false); - count = ((data[COUNT_MINUS])?(-1):(1))*data[COUNT_SPIN]; - - float angle_conv = ((double)data[ANGLE_H])+((double)data[ANGLE_M]/100)+((double)data[ANGLE_L]/10000); - if(data[ANGLE_MINUS] == 1){ - angle_conv *= -1; - } - - return(angle_conv+count*6.28); -}
diff -r a3a2fb1a3ba7 -r 1bbe0d41813e readme.txt --- a/readme.txt Tue Apr 30 04:54:26 2019 +0000 +++ b/readme.txt Thu Jun 27 07:56:29 2019 +0000 @@ -12,8 +12,6 @@ 3.エンコーダの角度と角速度の読み取り(単位はrad基準) float spd(); 角速度を読み取る.返り値:角速度(rad/s) float angle(); 角度を読み取る.オーバーフロー注意.返り値:角度(rad) - float position(); 1周のうちの自己位置を求める。正の値しか返さない。返り値:角度(rad) - int count; angle_e()関数が最後に呼び出された時点での回転数。返り値:回転数 //サンプル---------------------------------------------------------------------- #include "mbed.h" @@ -30,9 +28,7 @@ motor1.move(0.6); motor2.move(); - pc.printf("motor1.spd = %.3f\t",motor1.spd()); pc.printf("motor1.angle = %.3f\t",motor1.angle()); - pc.printf("motor1.position = %.3f\t",motor1.position()); pc.printf("\n"); } } \ No newline at end of file