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.
Dependents: 4thcomp 6th33222_copy
Fork of Locate by
Revision 0:265d8ad19d2a, committed 2016-09-02
- Comitter:
- sakanakuuun
- Date:
- Fri Sep 02 12:40:36 2016 +0000
- Child:
- 1:3513a2fbd81f
- Commit message:
- fs
Changed in this revision
| locate.cpp | Show annotated file Show diff for this revision Revisions of this file |
| locate.h | Show annotated file Show diff for this revision Revisions of this file |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/locate.cpp Fri Sep 02 12:40:36 2016 +0000
@@ -0,0 +1,91 @@
+#include <math.h>
+#include "mbed.h"
+#include "Encoder.h"
+#include "locate.h"
+
+
+/********以下グローバル変数宣言**************/
+
+TIM_Encoder_InitTypeDef encoder1, encoder2; //Encoderライブラリで使う
+TIM_HandleTypeDef timer1, timer2; //Encoderライブラリで使う
+uint16_t count1=0, count2=0; //Encoderライブラリで使う
+int8_t dir1, dir2; //Encoderライブラリで使う
+
+Serial pc(SERIAL_TX, SERIAL_RX); //pcと通信
+DigitalOut enc_v(PC_7); //エンコーダの電源
+
+int r, l; //現在の回転数
+int pr = 0, pl = 0; //前回のステップ数
+short v = 0; //ステップ速度
+float x = 0, y = 0; //xy方向に進んだ距離(m換算なし)
+float theta = 0; //機体角度、x軸正の向きを0とする
+float d_theta = 0; //erase()関数を使って、そのときのthetaをコピー
+
+/*************変数宣言終了******************/
+
+
+void update ()
+//位置情報を更新する。r,lはエンコーダから
+{
+ count1=__HAL_TIM_GET_COUNTER(&timer1);
+ dir1 = __HAL_TIM_IS_TIM_COUNTING_DOWN(&timer1);
+ count2=__HAL_TIM_GET_COUNTER(&timer2);
+ dir2 = __HAL_TIM_IS_TIM_COUNTING_DOWN(&timer2);
+
+ r = -convert_enc_count(count1, dir1);
+ l = convert_enc_count(count2, dir2);
+
+ theta = (r - l) * ROUND - d_theta;
+ v = (r - pr + l - pl);
+
+ x += v * cos(theta);
+ y += v * sin(theta);
+
+ pr = r;
+ pl = l;
+ //pc.printf("count1:%d%s count2:%d%s\r\n", count1, dir1==0 ? "+":"-",count2, dir2==0 ? "+":"-");
+ pc.printf("right:%d left:%d x:%d y:%d t:%f\n\r", r, l, coordinateX(), coordinateY(), coordinateTheta());
+}
+
+void setup()
+{
+ enc_v = 1;
+
+ //counting on both A&B inputs, 4 ticks per cycle, 16-bit count
+ //use PB6 PB7 = Nucleo D7 D8
+ EncoderInit(&encoder1, &timer1, TIM4, 0xffff, TIM_ENCODERMODE_TI12);
+
+ //counting on both A&B inputs, 4 ticks per cycle, 16-bit count
+ //use PA0 PA1 = Nucleo A0 A1
+ EncoderInit(&encoder2, &timer2, TIM2, 0xffff, TIM_ENCODERMODE_TI12);
+}
+
+short coordinateX()
+{
+ return x * LOCATE_STEP / 2;
+}
+
+short coordinateY()
+{
+ return y * LOCATE_STEP / 2;
+}
+
+float coordinateTheta()
+{
+ return theta;
+}
+
+int convert_enc_count(int16_t pulse, int8_t direction)
+{
+ if(direction == 0)
+ pulse = pulse - 0xffff -1;
+
+ return pulse;
+}
+
+void erase()
+{
+ x = 0;
+ y = 0;
+ d_theta = theta;
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/locate.h Fri Sep 02 12:40:36 2016 +0000 @@ -0,0 +1,65 @@ +#ifndef Locate_H +#define Locate_H + +#include <math.h> +#include "Encoder.h" + +/****************定数定義***************************/ + +#define OUTERRING_D 140 //外輪間距離(mm) +#define INNERRING_D 136 //内輪間距離(mm) +#define PI 3.14159 //π +#define RESOLUSION 400 //P/R(分解能) +#define DIAMETER 31.8 //タイヤの直径(mm) +#define ROUND_HOSEI 1.05 //角度のズレを補正 + +// エンコーダの1ステップあたりの距離(mm) +#define LOCATE_STEP (DIAMETER*PI / RESOLUSION) +//タイヤ間距離(mm) +#define TIRE_DISTANCE ((OUTERRING_D + INNERRING_D) / 2) +//機体が1回転するために必要なステップ数の”逆数” +#define ROUND ((PI * DIAMETER / (RESOLUSION * TIRE_DISTANCE)) * ROUND_HOSEI) + +/****************************************************************/ + + + + +/********以下関数宣言*****************/ + +//最初に一回だけ行う +void setup(); + +//位置情報を更新する。r,lはエンコーダから +void update (); + +//xをmm換算して整数値として返す +short coordinateX(); + +//yをmm換算して整数値として返す +short coordinateY(); + +//thetaを返す +float coordinateTheta(); + +//encorderの値を回転数に変換。update内部で使われる +int convert_enc_count(int16_t pulse, int8_t direction); + +//x, y, thetaを0にする +void erase(); + +/**********************************/ +#endif + + + + + +/**************Encoderライブラリで使う定義**************************/ + +#ifdef TARGET_STM32F3 +#define __HAL_TIM_GET_COUNTER(__HANDLE__) ((__HANDLE__)->Instance->CNT) +#define __HAL_TIM_IS_TIM_COUNTING_DOWN(__HANDLE__) (((__HANDLE__)->Instance->CR1 &(TIM_CR1_DIR)) == (TIM_CR1_DIR)) +#endif + +/***************************************************************/
