NHK2018Bチームの手動ロボットのプログラムです。(製作中)

Dependencies:   HCSR04 PID PS3viaSBDBT QEI mbed

Committer:
tektomo
Date:
Tue Oct 02 13:17:01 2018 +0000
Revision:
5:79cc2a126504
Parent:
4:58a2afff53a4
Child:
6:eaeaaf374263
????????????????

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tektomo 0:1ebf4907639c 1 /* ------------------------------------------------------------------- */
tektomo 0:1ebf4907639c 2 /* NHKロボコン2018-Bチーム手動ロボット(設計者: 4S 関) */
tektomo 0:1ebf4907639c 3 /* ------------------------------------------------------------------- */
tektomo 0:1ebf4907639c 4 /* このプログラムは上記のロボット専用の制御プログラムである。 */
tektomo 0:1ebf4907639c 5 /* ------------------------------------------------------------------- */
tektomo 0:1ebf4907639c 6 /* 対応機種: NUCLEO-F446RE */
tektomo 0:1ebf4907639c 7 /* ------------------------------------------------------------------- */
tektomo 0:1ebf4907639c 8 /* 製作者: 1-3 武井 智大, mail: taobmcoe@outlook.com */
tektomo 0:1ebf4907639c 9 /* ------------------------------------------------------------------- */
tektomo 0:1ebf4907639c 10 /* 使用センサ:ロータリーエンコーダ: 2個, 超音波センサ: 1個*/
tektomo 0:1ebf4907639c 11 /* ------------------------------------------------------------------- */
tektomo 0:1ebf4907639c 12 #include "mbed.h"
tektomo 0:1ebf4907639c 13 #include "PS3_S.h"
tektomo 0:1ebf4907639c 14 #include "hcsr04.h"
tektomo 0:1ebf4907639c 15 #include "QEI.h"
tektomo 0:1ebf4907639c 16 #include "PID.h"
tektomo 0:1ebf4907639c 17
tektomo 0:1ebf4907639c 18 #define ps3data ps3s.condata
tektomo 0:1ebf4907639c 19 #define PI 3.14159265359
tektomo 3:f462b1244d77 20 #define roller_Kp 1.5
tektomo 5:79cc2a126504 21 #define froller_Ki 0.5
tektomo 5:79cc2a126504 22 #define broller_Ki 0.5
tektomo 5:79cc2a126504 23 #define roller_Kd 0.0
tektomo 0:1ebf4907639c 24
tektomo 0:1ebf4907639c 25 //定義
tektomo 0:1ebf4907639c 26
tektomo 0:1ebf4907639c 27 /*I2C定義
tektomo 0:1ebf4907639c 28 0前輪左:0x10
tektomo 0:1ebf4907639c 29 1前輪右:0x12
tektomo 0:1ebf4907639c 30 2後輪左:0x14
tektomo 0:1ebf4907639c 31 3後輪右:0x16
tektomo 0:1ebf4907639c 32 4投射前:0x18
tektomo 0:1ebf4907639c 33 5投射後ろ:0x20
tektomo 0:1ebf4907639c 34 6エアシリンダー:0x40
tektomo 0:1ebf4907639c 35 正転:0x84~0xFF
tektomo 0:1ebf4907639c 36 逆転:0x00~0x7C
tektomo 0:1ebf4907639c 37 ショートブレーキ:0x7D~0x83
tektomo 0:1ebf4907639c 38 */
tektomo 0:1ebf4907639c 39 I2C i2c(PB_9, PB_8);
tektomo 0:1ebf4907639c 40 //PS3コン定義
tektomo 0:1ebf4907639c 41 PS3s ps3s(PC_10, PC_11);
tektomo 0:1ebf4907639c 42 //PC定義
tektomo 0:1ebf4907639c 43 Serial pc(USBTX,USBRX);
tektomo 0:1ebf4907639c 44 //ロリコン定義
tektomo 0:1ebf4907639c 45 QEI front_roller_wheel(PC_8, PC_6, NC, 624);
tektomo 0:1ebf4907639c 46 QEI back_roller_wheel(PC_5, PA_12, NC, 624);
tektomo 0:1ebf4907639c 47 //PID定義
tektomo 0:1ebf4907639c 48 PID front_roller(roller_Kp, froller_Ki, roller_Kd, 0.001);
tektomo 0:1ebf4907639c 49 PID back_roller(roller_Kp, broller_Ki, roller_Kd, 0.001);
tektomo 0:1ebf4907639c 50 //超音波センサ定義
tektomo 0:1ebf4907639c 51 HCSR04 topsonic(PB_2,PB_1);
tektomo 0:1ebf4907639c 52 //HCSR04 undersonic(PB_15,PB_14);
tektomo 0:1ebf4907639c 53 //LED定義
tektomo 0:1ebf4907639c 54 DigitalOut Power(PC_12);//Green
tektomo 0:1ebf4907639c 55 DigitalOut i2ccheck(PB_7);//Blue
tektomo 0:1ebf4907639c 56 DigitalOut sncled(PC_2);//Yellow
tektomo 0:1ebf4907639c 57 DigitalOut Powemer(PC_3);//Red
tektomo 0:1ebf4907639c 58 DigitalOut myled(LED1);
tektomo 0:1ebf4907639c 59 //緊急停止定義
tektomo 0:1ebf4907639c 60 DigitalOut emersig(PC_0);
tektomo 0:1ebf4907639c 61 //タイマー定義
tektomo 0:1ebf4907639c 62 Ticker get_rps;//ロリコンからRPMの読み取り
tektomo 0:1ebf4907639c 63 InterruptIn but(USER_BUTTON);
tektomo 0:1ebf4907639c 64
tektomo 0:1ebf4907639c 65 //変数定義
tektomo 0:1ebf4907639c 66 //データ配列
tektomo 0:1ebf4907639c 67 char send_data[4][1];//足回り用データ配列
tektomo 0:1ebf4907639c 68 char send_data2[1];//エアシリンダー用データ配列
tektomo 0:1ebf4907639c 69 int address[4] = {0x10, 0x12, 0x14, 0x16};//i2cアドレス
tektomo 0:1ebf4907639c 70 int err[4] = {1, 1, 1, 1};//I2Cリザルト
tektomo 0:1ebf4907639c 71
tektomo 0:1ebf4907639c 72 int front_roller_rpm;//RPM変数
tektomo 0:1ebf4907639c 73 int back_roller_rpm;
tektomo 0:1ebf4907639c 74 int front_roller_pulse[10];//ロリコンパルスバッファ
tektomo 0:1ebf4907639c 75 int back_roller_pulse[10];
tektomo 0:1ebf4907639c 76 int sum_front_roller_pulse;//ロリコンパルス
tektomo 0:1ebf4907639c 77 int sum_back_roller_pulse;
tektomo 0:1ebf4907639c 78 int ave_front_roller_pulse;//ロリコンパルス平均
tektomo 0:1ebf4907639c 79 int ave_back_roller_pulse;
tektomo 0:1ebf4907639c 80 char front_roller_data[1];
tektomo 0:1ebf4907639c 81 char back_roller_data[1];
tektomo 0:1ebf4907639c 82 int dmove_val;
tektomo 0:1ebf4907639c 83 static int j = 0;
tektomo 0:1ebf4907639c 84 int sonicval;
tektomo 0:1ebf4907639c 85 int mode = true;
tektomo 3:f462b1244d77 86 int table;
tektomo 0:1ebf4907639c 87
tektomo 0:1ebf4907639c 88 //プロトタイプ宣言
tektomo 0:1ebf4907639c 89 //メイン制御関数
tektomo 0:1ebf4907639c 90 void ctrl();
tektomo 0:1ebf4907639c 91 void semiauto();
tektomo 0:1ebf4907639c 92 //移動関数
tektomo 0:1ebf4907639c 93 void amove_R();
tektomo 0:1ebf4907639c 94 void amave_L();
tektomo 0:1ebf4907639c 95 void dmove();
tektomo 0:1ebf4907639c 96 //void shortb(int i);
tektomo 0:1ebf4907639c 97 void backfif();
tektomo 0:1ebf4907639c 98 void sendi2c();
tektomo 0:1ebf4907639c 99 //回転数取得関数
tektomo 0:1ebf4907639c 100 void flip();
tektomo 0:1ebf4907639c 101 //ローラーPID
tektomo 0:1ebf4907639c 102 int roller_PID(int front, int back);
tektomo 0:1ebf4907639c 103 //発射関数
tektomo 0:1ebf4907639c 104 void shoot();
tektomo 0:1ebf4907639c 105 //その他諸関数
tektomo 0:1ebf4907639c 106 void changemode();
tektomo 0:1ebf4907639c 107 void printdata();
tektomo 0:1ebf4907639c 108 void outemergency();
tektomo 0:1ebf4907639c 109 void emergency();
tektomo 0:1ebf4907639c 110
tektomo 0:1ebf4907639c 111 //実装部
tektomo 0:1ebf4907639c 112
tektomo 0:1ebf4907639c 113 //メイン関数
tektomo 0:1ebf4907639c 114 int main()
tektomo 0:1ebf4907639c 115 {
tektomo 0:1ebf4907639c 116 pc.baud(460800);
tektomo 0:1ebf4907639c 117 emersig = 0;
tektomo 0:1ebf4907639c 118 Power = 1;
tektomo 0:1ebf4907639c 119 i2ccheck = 0;
tektomo 0:1ebf4907639c 120 get_rps.attach_us(&flip, 40000);
tektomo 0:1ebf4907639c 121 but.rise(shoot);
tektomo 3:f462b1244d77 122 pc.printf("\x1b[0m");
tektomo 5:79cc2a126504 123 send_data2[0] = 0x80;
tektomo 5:79cc2a126504 124 i2c.write(0x18, send_data2, 1);
tektomo 5:79cc2a126504 125 i2c.write(0x20, send_data2, 1);
tektomo 0:1ebf4907639c 126 while(true) {
tektomo 0:1ebf4907639c 127 topsonic.start();
tektomo 0:1ebf4907639c 128 sonicval = topsonic.get_dist_cm();
tektomo 4:58a2afff53a4 129 ps3s.getdata();
tektomo 4:58a2afff53a4 130 ps3s.checkdata();
tektomo 5:79cc2a126504 131 if(ps3data[6] == 1){
tektomo 5:79cc2a126504 132 shoot();
tektomo 5:79cc2a126504 133 }
tektomo 4:58a2afff53a4 134 ctrl();
tektomo 0:1ebf4907639c 135 }
tektomo 0:1ebf4907639c 136 }
tektomo 0:1ebf4907639c 137
tektomo 0:1ebf4907639c 138 //メイン制御関数
tektomo 0:1ebf4907639c 139 void ctrl()
tektomo 0:1ebf4907639c 140 {
tektomo 0:1ebf4907639c 141
tektomo 5:79cc2a126504 142 if(ps3data[12]==1) {//Select
tektomo 0:1ebf4907639c 143 emergency();//緊急停止
tektomo 5:79cc2a126504 144 } else if(ps3data[13]==1) {//Start
tektomo 0:1ebf4907639c 145 outemergency();//緊急停止解除
tektomo 0:1ebf4907639c 146 }
tektomo 0:1ebf4907639c 147
tektomo 0:1ebf4907639c 148 if(emersig == 0) {
tektomo 0:1ebf4907639c 149 for(int i = 0; i < 4; i++) {
tektomo 0:1ebf4907639c 150 send_data[i][0] = 0x83;//ショートブレーキ
tektomo 0:1ebf4907639c 151 }
tektomo 0:1ebf4907639c 152
tektomo 5:79cc2a126504 153 if(ps3data[0]==1) {
tektomo 0:1ebf4907639c 154 dmove_val += 1;//上
tektomo 5:79cc2a126504 155 } else if(ps3data[1]==1) {
tektomo 0:1ebf4907639c 156 dmove_val += 2;//下
tektomo 5:79cc2a126504 157 } else if(ps3data[2]==1) {
tektomo 0:1ebf4907639c 158 dmove_val += 4;//右
tektomo 5:79cc2a126504 159 } else if(ps3data[3]==1) {
tektomo 0:1ebf4907639c 160 dmove_val += 8;//左
tektomo 5:79cc2a126504 161 } else if(ps3data[8]==1) {//L1
tektomo 0:1ebf4907639c 162 dmove_val += 32;//左回転
tektomo 5:79cc2a126504 163 } else if(ps3data[10]==1) {//R1
tektomo 0:1ebf4907639c 164 dmove_val += 16;//右回転
tektomo 0:1ebf4907639c 165 }
tektomo 5:79cc2a126504 166 if (dmove_val == 1) {
tektomo 0:1ebf4907639c 167 dmove();//方向キー+L1R1キー
tektomo 0:1ebf4907639c 168 }
tektomo 0:1ebf4907639c 169
tektomo 0:1ebf4907639c 170 if(((ps3data[16] <= 51) || (ps3data[16] >= 77)) || ((ps3data[17] <= 51) || (ps3data[17] >= 77))) {
tektomo 0:1ebf4907639c 171 amove_R();//アナログパッド
tektomo 0:1ebf4907639c 172 }
tektomo 0:1ebf4907639c 173
tektomo 5:79cc2a126504 174 if(ps3data[7]==1) {//しかく
tektomo 3:f462b1244d77 175 //backfif();50cm後退
tektomo 5:79cc2a126504 176 } else if(ps3data[4]==1) { //さんかく
tektomo 0:1ebf4907639c 177 semiauto();
tektomo 5:79cc2a126504 178 } else if(ps3data[5]==1) { //しかく
tektomo 0:1ebf4907639c 179 changemode();
tektomo 0:1ebf4907639c 180 }
tektomo 5:79cc2a126504 181 //sendi2c();
tektomo 0:1ebf4907639c 182 } else {
tektomo 0:1ebf4907639c 183 }
tektomo 0:1ebf4907639c 184 }
tektomo 0:1ebf4907639c 185
tektomo 0:1ebf4907639c 186 //緊急停止
tektomo 0:1ebf4907639c 187 void emergency()
tektomo 0:1ebf4907639c 188 {
tektomo 0:1ebf4907639c 189 emersig = 1;
tektomo 0:1ebf4907639c 190 Powemer = 1;
tektomo 0:1ebf4907639c 191 }
tektomo 0:1ebf4907639c 192
tektomo 0:1ebf4907639c 193 //緊急停止解除
tektomo 0:1ebf4907639c 194 void outemergency()
tektomo 0:1ebf4907639c 195 {
tektomo 0:1ebf4907639c 196 emersig = 0;
tektomo 0:1ebf4907639c 197 Powemer = 0;
tektomo 0:1ebf4907639c 198 }
tektomo 0:1ebf4907639c 199
tektomo 0:1ebf4907639c 200 //右アナログパッド
tektomo 0:1ebf4907639c 201 void amove_R()
tektomo 0:1ebf4907639c 202 {
tektomo 0:1ebf4907639c 203 int RXdata = ps3data[16];
tektomo 0:1ebf4907639c 204 int RYdata = ps3data[17];
tektomo 0:1ebf4907639c 205
tektomo 0:1ebf4907639c 206 if(RXdata == 64 && RYdata < 64) {
tektomo 0:1ebf4907639c 207 //垂直上
tektomo 0:1ebf4907639c 208 send_data[0][0] = abs(RYdata - 53) + 202 - RYdata - 8;
tektomo 0:1ebf4907639c 209 send_data[1][0] = abs(RYdata - 53) + 202 - RYdata;
tektomo 0:1ebf4907639c 210 send_data[2][0] = abs(RYdata - 53) + 202 - RYdata - 8;
tektomo 0:1ebf4907639c 211 send_data[3][0] = abs(RYdata - 53) + 202 - RYdata;
tektomo 0:1ebf4907639c 212
tektomo 0:1ebf4907639c 213 } else if(RXdata == 64 && RYdata > 64) {
tektomo 0:1ebf4907639c 214 //垂直下
tektomo 0:1ebf4907639c 215 send_data[0][0] = abs(RYdata - 127) * 2;
tektomo 0:1ebf4907639c 216 send_data[1][0] = abs(RYdata - 127) * 2 + 8;
tektomo 0:1ebf4907639c 217 send_data[2][0] = abs(RYdata - 127) * 2;
tektomo 0:1ebf4907639c 218 send_data[3][0] = abs(RYdata - 127) * 2 + 8;
tektomo 0:1ebf4907639c 219
tektomo 0:1ebf4907639c 220 } else if(RXdata > 64 && RYdata == 64) {
tektomo 0:1ebf4907639c 221 //垂直右
tektomo 0:1ebf4907639c 222 send_data[0][0] = (RXdata - 75) + 203 - abs(RXdata - 127) - 8;
tektomo 0:1ebf4907639c 223 send_data[3][0] = (RXdata - 75) + 203 - abs(RXdata - 127) - 8;
tektomo 0:1ebf4907639c 224 send_data[1][0] = abs(RXdata - 127) * 2;
tektomo 0:1ebf4907639c 225 send_data[2][0] = abs(RXdata - 127) * 2;
tektomo 0:1ebf4907639c 226
tektomo 0:1ebf4907639c 227 } else if(RXdata < 64 && RYdata == 64) {
tektomo 0:1ebf4907639c 228 //垂直左
tektomo 0:1ebf4907639c 229 send_data[0][0] = RXdata * 2;
tektomo 0:1ebf4907639c 230 send_data[3][0] = RXdata * 2;
tektomo 0:1ebf4907639c 231 send_data[1][0] = abs(RXdata - 53) + 202 - RXdata - 8;
tektomo 0:1ebf4907639c 232 send_data[2][0] = abs(RXdata - 53) + 202 - RXdata - 8;
tektomo 0:1ebf4907639c 233
tektomo 0:1ebf4907639c 234 } else if (RXdata > 64 && RYdata < 64) {
tektomo 0:1ebf4907639c 235 //右上
tektomo 0:1ebf4907639c 236 send_data[0][0] = (RXdata - 74) + 202 - abs(RXdata - 127);
tektomo 0:1ebf4907639c 237 send_data[3][0] = (RXdata - 74) + 202 - abs(RXdata - 127);
tektomo 0:1ebf4907639c 238 } else if(RXdata < 64 && RYdata < 64) {
tektomo 0:1ebf4907639c 239 //左上
tektomo 0:1ebf4907639c 240 send_data[1][0] = abs(RXdata - 54) + 201 - RXdata;
tektomo 0:1ebf4907639c 241 send_data[2][0] = abs(RXdata - 54) + 201 - RXdata;
tektomo 0:1ebf4907639c 242 } else if(RXdata < 64 && RYdata > 64) {
tektomo 0:1ebf4907639c 243 //左下
tektomo 0:1ebf4907639c 244 send_data[0][0] = RXdata * 2;
tektomo 0:1ebf4907639c 245 send_data[3][0] = RXdata * 2;
tektomo 0:1ebf4907639c 246 } else if(RXdata > 64 && RYdata > 64) {
tektomo 0:1ebf4907639c 247 //右下
tektomo 0:1ebf4907639c 248 send_data[1][0] = abs(RXdata - 127) * 2;
tektomo 0:1ebf4907639c 249 send_data[2][0] = abs(RXdata - 127) * 2;
tektomo 0:1ebf4907639c 250 }
tektomo 0:1ebf4907639c 251 }
tektomo 0:1ebf4907639c 252
tektomo 0:1ebf4907639c 253 void dmove()
tektomo 0:1ebf4907639c 254 {
tektomo 0:1ebf4907639c 255 int k = 32;
tektomo 0:1ebf4907639c 256 while(true) {
tektomo 0:1ebf4907639c 257 if (dmove_val == 0) {
tektomo 0:1ebf4907639c 258 break;
tektomo 0:1ebf4907639c 259 } else {
tektomo 0:1ebf4907639c 260 if (dmove_val - k >= 0) {
tektomo 0:1ebf4907639c 261 sncled = 1;
tektomo 0:1ebf4907639c 262 switch (k) {
tektomo 0:1ebf4907639c 263 case 1:
tektomo 0:1ebf4907639c 264 send_data[0][0] = 0xF7;
tektomo 0:1ebf4907639c 265 send_data[1][0] = 0xFF;
tektomo 0:1ebf4907639c 266 send_data[2][0] = 0xF7;
tektomo 0:1ebf4907639c 267 send_data[3][0] = 0xFF;
tektomo 0:1ebf4907639c 268 break;
tektomo 0:1ebf4907639c 269 case 2:
tektomo 0:1ebf4907639c 270 send_data[0][0] = 0x00;
tektomo 0:1ebf4907639c 271 send_data[1][0] = 0x0A;
tektomo 0:1ebf4907639c 272 send_data[2][0] = 0x00;
tektomo 0:1ebf4907639c 273 send_data[3][0] = 0x0A;
tektomo 0:1ebf4907639c 274 break;
tektomo 0:1ebf4907639c 275 case 4:
tektomo 0:1ebf4907639c 276 send_data[0][0] = 0xF5;
tektomo 0:1ebf4907639c 277 send_data[3][0] = 0xFF;
tektomo 0:1ebf4907639c 278 send_data[1][0] = 0x09;
tektomo 0:1ebf4907639c 279 send_data[2][0] = 0x00;
tektomo 0:1ebf4907639c 280 break;
tektomo 0:1ebf4907639c 281 case 8:
tektomo 0:1ebf4907639c 282 send_data[0][0] = 0x00;
tektomo 0:1ebf4907639c 283 send_data[3][0] = 0x09;
tektomo 0:1ebf4907639c 284 send_data[1][0] = 0xFF;
tektomo 0:1ebf4907639c 285 send_data[2][0] = 0xF5;
tektomo 0:1ebf4907639c 286 break;
tektomo 0:1ebf4907639c 287 case 16:
tektomo 0:1ebf4907639c 288 send_data[1][0] = 0x00;
tektomo 0:1ebf4907639c 289 send_data[3][0] = 0x00;
tektomo 0:1ebf4907639c 290 send_data[0][0] = 0xFF;
tektomo 0:1ebf4907639c 291 send_data[2][0] = 0xFF;
tektomo 0:1ebf4907639c 292 break;
tektomo 0:1ebf4907639c 293 case 32:
tektomo 0:1ebf4907639c 294 send_data[0][0] = 0x00;
tektomo 0:1ebf4907639c 295 send_data[2][0] = 0x00;
tektomo 0:1ebf4907639c 296 send_data[1][0] = 0xFF;
tektomo 0:1ebf4907639c 297 send_data[3][0] = 0xFF;
tektomo 0:1ebf4907639c 298 break;
tektomo 0:1ebf4907639c 299 default:
tektomo 0:1ebf4907639c 300 break;
tektomo 0:1ebf4907639c 301 }
tektomo 0:1ebf4907639c 302 sncled = 0;
tektomo 0:1ebf4907639c 303 dmove_val -= k;
tektomo 0:1ebf4907639c 304 } else {
tektomo 0:1ebf4907639c 305 k /= 2;
tektomo 0:1ebf4907639c 306 }
tektomo 0:1ebf4907639c 307 }
tektomo 0:1ebf4907639c 308 }
tektomo 0:1ebf4907639c 309 }
tektomo 0:1ebf4907639c 310
tektomo 0:1ebf4907639c 311 //ショートブレーキ
tektomo 0:1ebf4907639c 312 void shortb(int i)
tektomo 0:1ebf4907639c 313 {
tektomo 0:1ebf4907639c 314 i2ccheck = 0;
tektomo 0:1ebf4907639c 315 send_data[i][0] = 0x83;
tektomo 0:1ebf4907639c 316 }
tektomo 0:1ebf4907639c 317
tektomo 3:f462b1244d77 318 /*50cm後退
tektomo 0:1ebf4907639c 319 void backfif()
tektomo 0:1ebf4907639c 320 {
tektomo 0:1ebf4907639c 321 int snc1;
tektomo 0:1ebf4907639c 322 //int snc2;
tektomo 0:1ebf4907639c 323 while(true) {
tektomo 0:1ebf4907639c 324 topsonic.start();
tektomo 0:1ebf4907639c 325 //undersonic.start();
tektomo 0:1ebf4907639c 326 snc1 = topsonic.get_dist_cm();
tektomo 0:1ebf4907639c 327 //snc2 = undersonic.get_dist_cm();
tektomo 0:1ebf4907639c 328 if (snc1 != 0) { // && (snc2 != 0)) {
tektomo 0:1ebf4907639c 329 break;
tektomo 0:1ebf4907639c 330 }
tektomo 0:1ebf4907639c 331 }
tektomo 0:1ebf4907639c 332
tektomo 0:1ebf4907639c 333 int snc1buffer;
tektomo 0:1ebf4907639c 334 //int snc2buffer;
tektomo 0:1ebf4907639c 335 while(true) {
tektomo 0:1ebf4907639c 336 topsonic.start();
tektomo 0:1ebf4907639c 337 //undersonic.start();
tektomo 0:1ebf4907639c 338 snc1buffer = topsonic.get_dist_cm();
tektomo 0:1ebf4907639c 339 //snc2buffer = undersonic.get_dist_cm();
tektomo 0:1ebf4907639c 340 if (snc1buffer == 0) { // || snc2buffer == 0) {
tektomo 0:1ebf4907639c 341 continue;
tektomo 0:1ebf4907639c 342 }
tektomo 0:1ebf4907639c 343 if(snc1buffer >= snc1 + 50) {//|| (snc2buffer >= snc2 + 50)) {
tektomo 0:1ebf4907639c 344 sncled = 1;
tektomo 0:1ebf4907639c 345 for(int i = 0; i < 4; i++) {
tektomo 0:1ebf4907639c 346 send_data[i][0] = 0x83;
tektomo 0:1ebf4907639c 347 }
tektomo 0:1ebf4907639c 348 break;
tektomo 0:1ebf4907639c 349 } else {
tektomo 0:1ebf4907639c 350 for(int i = 0; i < 4; i++) {
tektomo 0:1ebf4907639c 351 send_data[i][0] = 0x10;
tektomo 0:1ebf4907639c 352 i2c.write(address[i], send_data[i], 1);
tektomo 0:1ebf4907639c 353 pc.printf("Goal:%dcm,Now:%dcm\n\r", snc1 + 50, snc1buffer);
tektomo 0:1ebf4907639c 354 }
tektomo 0:1ebf4907639c 355 }
tektomo 0:1ebf4907639c 356 }
tektomo 3:f462b1244d77 357 }*/
tektomo 0:1ebf4907639c 358
tektomo 0:1ebf4907639c 359 //I2C送信
tektomo 0:1ebf4907639c 360 void sendi2c()
tektomo 0:1ebf4907639c 361 {
tektomo 0:1ebf4907639c 362 err[0] = i2c.write(address[0], send_data[0], 1);
tektomo 0:1ebf4907639c 363 err[1] = i2c.write(address[1], send_data[1], 1);
tektomo 0:1ebf4907639c 364 err[2] = i2c.write(address[2], send_data[2], 1);
tektomo 0:1ebf4907639c 365 err[3] = i2c.write(address[3], send_data[3], 1);
tektomo 0:1ebf4907639c 366 if (err[0]!=0 || err[1]!=0 || err[2]!=0 || err[3]!=0) {
tektomo 0:1ebf4907639c 367 while(10) {
tektomo 0:1ebf4907639c 368 i2ccheck = 1;
tektomo 0:1ebf4907639c 369 wait(0.1);
tektomo 0:1ebf4907639c 370
tektomo 0:1ebf4907639c 371 i2ccheck = 0;
tektomo 0:1ebf4907639c 372 }
tektomo 0:1ebf4907639c 373 } else {
tektomo 0:1ebf4907639c 374 i2ccheck = 1;
tektomo 0:1ebf4907639c 375 }
tektomo 0:1ebf4907639c 376 }
tektomo 0:1ebf4907639c 377
tektomo 0:1ebf4907639c 378 //移動平均をPID
tektomo 0:1ebf4907639c 379 void flip()
tektomo 0:1ebf4907639c 380 {
tektomo 0:1ebf4907639c 381 //前回のi番目のデータを消して新たにそこにデータを書き込む(キューのような考え?)
tektomo 0:1ebf4907639c 382 sum_front_roller_pulse -= front_roller_pulse[j];
tektomo 0:1ebf4907639c 383 sum_back_roller_pulse -= back_roller_pulse[j];
tektomo 0:1ebf4907639c 384
tektomo 0:1ebf4907639c 385 //配列のi番目の箱に取得パルスを代入
tektomo 0:1ebf4907639c 386 front_roller_pulse[j] = front_roller_wheel.getPulses();
tektomo 0:1ebf4907639c 387 back_roller_pulse[j] = back_roller_wheel.getPulses();
tektomo 0:1ebf4907639c 388
tektomo 0:1ebf4907639c 389 front_roller_wheel.reset();
tektomo 0:1ebf4907639c 390 back_roller_wheel.reset();
tektomo 0:1ebf4907639c 391
tektomo 0:1ebf4907639c 392 //i[0]~i[9]までの合計値を代入
tektomo 0:1ebf4907639c 393 sum_front_roller_pulse += front_roller_pulse[j];
tektomo 0:1ebf4907639c 394 sum_back_roller_pulse += back_roller_pulse[j];
tektomo 0:1ebf4907639c 395
tektomo 0:1ebf4907639c 396 //インクリメント
tektomo 0:1ebf4907639c 397 j++;
tektomo 0:1ebf4907639c 398 //iが最大値(9)になったらリセット
tektomo 0:1ebf4907639c 399 if(j > 9) {
tektomo 0:1ebf4907639c 400 j = 0;
tektomo 0:1ebf4907639c 401 }
tektomo 0:1ebf4907639c 402 //10回分の合計値を10で割り、取得パルスの平均を出す
tektomo 0:1ebf4907639c 403 ave_front_roller_pulse = sum_front_roller_pulse / 10;
tektomo 0:1ebf4907639c 404 ave_back_roller_pulse = sum_back_roller_pulse / 10;
tektomo 0:1ebf4907639c 405
tektomo 0:1ebf4907639c 406 //平均値をRPMへ変換
tektomo 0:1ebf4907639c 407 front_roller_rpm = ave_front_roller_pulse * 25 * 60 / 1200;
tektomo 0:1ebf4907639c 408 back_roller_rpm = ave_back_roller_pulse * 25 * 60 / 1200;
tektomo 5:79cc2a126504 409 pc.printf("%3d,%3d,%d\n\r",abs(front_roller_rpm), abs(back_roller_rpm),sonicval);
tektomo 0:1ebf4907639c 410 }
tektomo 0:1ebf4907639c 411
tektomo 0:1ebf4907639c 412 //ローラー
tektomo 0:1ebf4907639c 413 int roller_PID(int front, int back)
tektomo 0:1ebf4907639c 414 {
tektomo 0:1ebf4907639c 415 front_roller.setInputLimits(-2000, 2000);
tektomo 0:1ebf4907639c 416 back_roller.setInputLimits(-2000, 2000);
tektomo 0:1ebf4907639c 417
tektomo 0:1ebf4907639c 418 front_roller.setOutputLimits(0x84, 0xFF);
tektomo 0:1ebf4907639c 419 back_roller.setOutputLimits(0x84, 0xFF);
tektomo 0:1ebf4907639c 420
tektomo 0:1ebf4907639c 421 front_roller.setMode(AUTO_MODE);
tektomo 0:1ebf4907639c 422 back_roller.setMode(AUTO_MODE);
tektomo 0:1ebf4907639c 423
tektomo 0:1ebf4907639c 424 front_roller.setSetPoint(front);
tektomo 0:1ebf4907639c 425 back_roller.setSetPoint(back);
tektomo 0:1ebf4907639c 426
tektomo 0:1ebf4907639c 427 front_roller.setProcessValue(abs(front_roller_rpm));
tektomo 0:1ebf4907639c 428 back_roller.setProcessValue(abs(back_roller_rpm));
tektomo 0:1ebf4907639c 429
tektomo 0:1ebf4907639c 430 front_roller_data[0] = front_roller.compute();
tektomo 0:1ebf4907639c 431 back_roller_data[0] = back_roller.compute();
tektomo 0:1ebf4907639c 432
tektomo 0:1ebf4907639c 433 return 0;
tektomo 0:1ebf4907639c 434 }
tektomo 0:1ebf4907639c 435
tektomo 0:1ebf4907639c 436 //投擲動作
tektomo 0:1ebf4907639c 437 void shoot()
tektomo 0:1ebf4907639c 438 {
tektomo 3:f462b1244d77 439 while(true) {
tektomo 4:58a2afff53a4 440 roller_PID(1000,1000);
tektomo 5:79cc2a126504 441 i2c.write(0x18, front_roller_data, 1, false);
tektomo 5:79cc2a126504 442 i2c.write(0x20, back_roller_data, 1, false);
tektomo 3:f462b1244d77 443 }
tektomo 0:1ebf4907639c 444 }
tektomo 0:1ebf4907639c 445
tektomo 0:1ebf4907639c 446
tektomo 4:58a2afff53a4 447
tektomo 0:1ebf4907639c 448 //セミオートプレイテスト
tektomo 0:1ebf4907639c 449
tektomo 0:1ebf4907639c 450 void semiauto()
tektomo 0:1ebf4907639c 451 {
tektomo 0:1ebf4907639c 452 while(true) {
tektomo 3:f462b1244d77 453 looptime = mainloop.read();
tektomo 3:f462b1244d77 454 pc.printf("%f,%d\n\r", looptime, sonicval);
tektomo 3:f462b1244d77 455 mainloop.reset();
tektomo 3:f462b1244d77 456 mainloop.start();
tektomo 3:f462b1244d77 457 send_data[0][0] = 0xE1;
tektomo 3:f462b1244d77 458 send_data[3][0] = 0xEC;
tektomo 3:f462b1244d77 459 send_data[1][0] = 0x1D;
tektomo 3:f462b1244d77 460 send_data[2][0] = 0x14;
tektomo 0:1ebf4907639c 461 topsonic.start();
tektomo 0:1ebf4907639c 462 wait(0.01);
tektomo 0:1ebf4907639c 463 sonicval = topsonic.get_dist_cm();
tektomo 3:f462b1244d77 464 if(sonicval < 100) {
tektomo 3:f462b1244d77 465 send_data[0][0] = 0xF7;
tektomo 3:f462b1244d77 466 send_data[1][0] = 0xFF;
tektomo 3:f462b1244d77 467 send_data[2][0] = 0xF7;
tektomo 3:f462b1244d77 468 send_data[3][0] = 0xFF;
tektomo 3:f462b1244d77 469 if(sonicval == 75) {
tektomo 3:f462b1244d77 470 send_data[0][0] = 0x80;
tektomo 3:f462b1244d77 471 send_data[1][0] = 0x80;
tektomo 3:f462b1244d77 472 send_data[2][0] = 0x80;
tektomo 3:f462b1244d77 473 send_data[3][0] = 0x80;
tektomo 3:f462b1244d77 474 }
tektomo 0:1ebf4907639c 475 }
tektomo 3:f462b1244d77 476 sendi2c();
tektomo 3:f462b1244d77 477 if(sonicval == 75) {
tektomo 3:f462b1244d77 478 table = 0;
tektomo 3:f462b1244d77 479 shoot();
tektomo 0:1ebf4907639c 480 }
tektomo 3:f462b1244d77 481 mainloop.stop();
tektomo 0:1ebf4907639c 482 }
tektomo 0:1ebf4907639c 483 }
tektomo 0:1ebf4907639c 484
tektomo 3:f462b1244d77 485 void changemode()
tektomo 3:f462b1244d77 486 {
tektomo 3:f462b1244d77 487 mode = !mode;
tektomo 3:f462b1244d77 488 if(mode) {
tektomo 3:f462b1244d77 489 pc.printf("\x1b[41m");
tektomo 3:f462b1244d77 490 } else {
tektomo 3:f462b1244d77 491 pc.printf("\x1b[44m");
tektomo 0:1ebf4907639c 492 }
tektomo 3:f462b1244d77 493 }
tektomo 0:1ebf4907639c 494
tektomo 0:1ebf4907639c 495
tektomo 0:1ebf4907639c 496 //デバック用データ出力
tektomo 3:f462b1244d77 497 void printdata()
tektomo 3:f462b1244d77 498 {
tektomo 3:f462b1244d77 499 for (int i = 0; i < 7; i++) {
tektomo 3:f462b1244d77 500 pc.printf("%d,",ps3s.Rdata[i]);
tektomo 3:f462b1244d77 501 }
tektomo 3:f462b1244d77 502 pc.printf("%d\n\r",ps3s.Rdata[7]);
tektomo 3:f462b1244d77 503 }