4年工学基礎研究(前期)で作成したペンプロッタの制御プログラム

Dependencies:   SPM_simple mbed trapezoidalMotionCal

Committer:
Akito914
Date:
Fri Aug 04 04:08:02 2017 +0000
Revision:
4:76146d1e248a
Parent:
2:80eb19e9091e
Child:
5:0abb848bb2f9
???????????????; ???pc?Hello ???????

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Akito914 0:0eddebdc2c58 1 #include "mbed.h"
Akito914 0:0eddebdc2c58 2 #include "SPM.h"
Akito914 0:0eddebdc2c58 3 #include "trapezoidalMotionCal.h"
Akito914 0:0eddebdc2c58 4
Akito914 1:3663d0f20136 5 #define PEN_SAVEMODE 120
Akito914 1:3663d0f20136 6 #define PEN_UP 90
Akito914 1:3663d0f20136 7 #define PEN_DOWN 70
Akito914 1:3663d0f20136 8
Akito914 1:3663d0f20136 9
Akito914 2:80eb19e9091e 10 //stepMotor x_axis(PC_4 ,PB_13 ,PB_14 ,PB_15);
Akito914 0:0eddebdc2c58 11 stepMotor x_axis(PB_15 ,PB_14 ,PB_13 ,PC_4);
Akito914 2:80eb19e9091e 12 //stepMotor y_axis(PB_1 ,PB_2 ,PB_12 ,PA_11);
Akito914 2:80eb19e9091e 13 stepMotor y_axis(PA_11 ,PB_12 ,PB_2 ,PB_1);
Akito914 0:0eddebdc2c58 14
Akito914 2:80eb19e9091e 15 trapezoidalMotionCal motionCal(50, 50, 4000, 10000, -10000);
Akito914 0:0eddebdc2c58 16
Akito914 0:0eddebdc2c58 17 PwmOut servo(PC_6);
Akito914 1:3663d0f20136 18 PwmOut speaker(/*PC_8*/PA_15);
Akito914 0:0eddebdc2c58 19
Akito914 0:0eddebdc2c58 20 DigitalIn x_originSW(PA_0);
Akito914 0:0eddebdc2c58 21 DigitalIn y_originSW(PA_1);
Akito914 0:0eddebdc2c58 22
Akito914 0:0eddebdc2c58 23 DigitalOut myled(LED1);
Akito914 0:0eddebdc2c58 24 Serial pc(USBTX,USBRX);
Akito914 2:80eb19e9091e 25 Serial debugPort(PA_9,PA_10);
Akito914 0:0eddebdc2c58 26
Akito914 0:0eddebdc2c58 27 typedef struct {
Akito914 0:0eddebdc2c58 28 int32_t x;
Akito914 0:0eddebdc2c58 29 int32_t y;
Akito914 0:0eddebdc2c58 30 } coordinate_t;
Akito914 0:0eddebdc2c58 31
Akito914 0:0eddebdc2c58 32 int task = 0;
Akito914 0:0eddebdc2c58 33 int32_t pulse = 0;
Akito914 0:0eddebdc2c58 34
Akito914 1:3663d0f20136 35 int job = 0;
Akito914 1:3663d0f20136 36 int nextMode = 0;
Akito914 1:3663d0f20136 37 int penMotion = 0; // 0: up 1: down
Akito914 1:3663d0f20136 38 coordinate_t nextPosition = {0};
Akito914 0:0eddebdc2c58 39
Akito914 1:3663d0f20136 40 coordinate_t currentPosition = {0};
Akito914 1:3663d0f20136 41
Akito914 1:3663d0f20136 42 void pcIntr();
Akito914 1:3663d0f20136 43 void rcdataProcessing(int32_t *RXData);
Akito914 1:3663d0f20136 44 void sendRequest();
Akito914 1:3663d0f20136 45
Akito914 0:0eddebdc2c58 46 void driveServo(int deg);
Akito914 0:0eddebdc2c58 47 void linearInterpolation(coordinate_t cStart, coordinate_t cTarg);
Akito914 0:0eddebdc2c58 48 void trapezoid(int32_t targSteps);
Akito914 0:0eddebdc2c58 49
Akito914 1:3663d0f20136 50 void beep(int T_us,int t_ms);
Akito914 1:3663d0f20136 51 void pi(int times);
Akito914 1:3663d0f20136 52 void q_sound(void);
Akito914 1:3663d0f20136 53 void ans_sound(void);
Akito914 1:3663d0f20136 54 void piroro(void);
Akito914 0:0eddebdc2c58 55
Akito914 1:3663d0f20136 56 // 受信割込みハンドラ
Akito914 1:3663d0f20136 57 void pcIntr()
Akito914 1:3663d0f20136 58 {
Akito914 1:3663d0f20136 59 static int32_t RXByte[8] = {0};
Akito914 1:3663d0f20136 60 static int32_t RXCount = 0;
Akito914 1:3663d0f20136 61
Akito914 1:3663d0f20136 62 char temp = pc.getc();
Akito914 1:3663d0f20136 63
Akito914 1:3663d0f20136 64 if(((temp >> 5) & 0x07) != RXCount){
Akito914 1:3663d0f20136 65 RXCount = 0;
Akito914 1:3663d0f20136 66 }
Akito914 1:3663d0f20136 67 else{
Akito914 1:3663d0f20136 68 RXByte[RXCount] = temp;
Akito914 1:3663d0f20136 69
Akito914 1:3663d0f20136 70 if(RXCount < 7){
Akito914 1:3663d0f20136 71 RXCount += 1;
Akito914 1:3663d0f20136 72 }
Akito914 1:3663d0f20136 73 else{ // all data received
Akito914 1:3663d0f20136 74 RXCount = 0;
Akito914 1:3663d0f20136 75 rcdataProcessing(RXByte);
Akito914 1:3663d0f20136 76 }
Akito914 1:3663d0f20136 77 }
Akito914 1:3663d0f20136 78 }
Akito914 0:0eddebdc2c58 79
Akito914 1:3663d0f20136 80
Akito914 1:3663d0f20136 81 void rcdataProcessing(int32_t *RXData){
Akito914 1:3663d0f20136 82
Akito914 1:3663d0f20136 83 if((RXData[0] & 0x08) != 0){ // move mode
Akito914 1:3663d0f20136 84 nextPosition.x = (RXData[1] & 0x0f) | ((RXData[2] & 0x0f) << 4) | ((RXData[3] & 0x0f) << 8);
Akito914 1:3663d0f20136 85 nextPosition.y = (RXData[4] & 0x0f) | ((RXData[5] & 0x0f) << 4) | ((RXData[6] & 0x0f) << 8);
Akito914 1:3663d0f20136 86 nextMode = 1;
Akito914 0:0eddebdc2c58 87 }
Akito914 1:3663d0f20136 88 else{ // pen mode
Akito914 1:3663d0f20136 89 nextMode = 0;
Akito914 1:3663d0f20136 90 penMotion = RXData[0] & 0x01;
Akito914 1:3663d0f20136 91 }
Akito914 1:3663d0f20136 92 job = 1;
Akito914 1:3663d0f20136 93 }
Akito914 1:3663d0f20136 94
Akito914 1:3663d0f20136 95
Akito914 1:3663d0f20136 96 void sendRequest(){
Akito914 1:3663d0f20136 97 pc.putc(0x01);
Akito914 0:0eddebdc2c58 98 }
Akito914 0:0eddebdc2c58 99
Akito914 0:0eddebdc2c58 100 void driveServo(int deg){
Akito914 0:0eddebdc2c58 101 const static int servo_offset =830;
Akito914 0:0eddebdc2c58 102 const static float servo_gain =10.0;
Akito914 0:0eddebdc2c58 103
Akito914 0:0eddebdc2c58 104 deg=deg*servo_gain+servo_offset;
Akito914 0:0eddebdc2c58 105 servo.pulsewidth_us(deg);
Akito914 0:0eddebdc2c58 106
Akito914 0:0eddebdc2c58 107 return;
Akito914 0:0eddebdc2c58 108 }
Akito914 0:0eddebdc2c58 109
Akito914 0:0eddebdc2c58 110 int main()
Akito914 0:0eddebdc2c58 111 {
Akito914 2:80eb19e9091e 112 coordinate_t runningPosition = {0};
Akito914 2:80eb19e9091e 113 int runningPenMotion = 0;
Akito914 1:3663d0f20136 114
Akito914 1:3663d0f20136 115 pc.baud(115200);
Akito914 2:80eb19e9091e 116 debugPort.baud(115200);
Akito914 1:3663d0f20136 117
Akito914 1:3663d0f20136 118 pc.attach(pcIntr, Serial::RxIrq);
Akito914 2:80eb19e9091e 119
Akito914 2:80eb19e9091e 120 wait_ms(1000);
Akito914 2:80eb19e9091e 121
Akito914 2:80eb19e9091e 122 debugPort.printf("Hello Debug Port\r\n");
Akito914 2:80eb19e9091e 123
Akito914 0:0eddebdc2c58 124 x_axis.lock();
Akito914 0:0eddebdc2c58 125 y_axis.lock();
Akito914 2:80eb19e9091e 126
Akito914 1:3663d0f20136 127 driveServo(PEN_UP);
Akito914 1:3663d0f20136 128
Akito914 1:3663d0f20136 129 sendRequest();
Akito914 0:0eddebdc2c58 130
Akito914 0:0eddebdc2c58 131 while(1) {
Akito914 1:3663d0f20136 132
Akito914 1:3663d0f20136 133 while(job == 0);
Akito914 1:3663d0f20136 134
Akito914 1:3663d0f20136 135 if(nextMode != 0){ // move mode
Akito914 2:80eb19e9091e 136 runningPosition = nextPosition;
Akito914 1:3663d0f20136 137 job = 0;
Akito914 1:3663d0f20136 138 sendRequest();
Akito914 2:80eb19e9091e 139 linearInterpolation(currentPosition, runningPosition);
Akito914 2:80eb19e9091e 140 currentPosition = runningPosition;
Akito914 2:80eb19e9091e 141 wait_ms(10);
Akito914 0:0eddebdc2c58 142 }
Akito914 1:3663d0f20136 143 else{ // pen mode
Akito914 2:80eb19e9091e 144 runningPenMotion = penMotion;
Akito914 2:80eb19e9091e 145 job = 0;
Akito914 2:80eb19e9091e 146 sendRequest();
Akito914 2:80eb19e9091e 147 if(runningPenMotion != 0){
Akito914 1:3663d0f20136 148 driveServo(PEN_DOWN);
Akito914 2:80eb19e9091e 149 wait_ms(100);
Akito914 1:3663d0f20136 150 }
Akito914 1:3663d0f20136 151 else{
Akito914 1:3663d0f20136 152 driveServo(PEN_UP);
Akito914 2:80eb19e9091e 153 wait_ms(100);
Akito914 1:3663d0f20136 154 }
Akito914 1:3663d0f20136 155 }
Akito914 1:3663d0f20136 156
Akito914 0:0eddebdc2c58 157 }
Akito914 0:0eddebdc2c58 158 }
Akito914 0:0eddebdc2c58 159
Akito914 0:0eddebdc2c58 160 void linearInterpolation(coordinate_t cStart, coordinate_t cTarg)
Akito914 0:0eddebdc2c58 161 {
Akito914 0:0eddebdc2c58 162 coordinate_t delta;
Akito914 0:0eddebdc2c58 163 int32_t error = 0;
Akito914 0:0eddebdc2c58 164 bool x_inv = false;
Akito914 0:0eddebdc2c58 165 bool y_inv = false;
Akito914 0:0eddebdc2c58 166 coordinate_t cCurrent = {cStart.x, cStart.y};
Akito914 0:0eddebdc2c58 167 coordinate_t inc = {0, 0};
Akito914 0:0eddebdc2c58 168 uint32_t period_us = 0;
Akito914 0:0eddebdc2c58 169
Akito914 0:0eddebdc2c58 170 if(cStart.x > cTarg.x) {
Akito914 0:0eddebdc2c58 171 x_inv = true;
Akito914 0:0eddebdc2c58 172 cTarg.x = cStart.x * 2 - cTarg.x;
Akito914 0:0eddebdc2c58 173 }
Akito914 0:0eddebdc2c58 174 if(cStart.y > cTarg.y) {
Akito914 0:0eddebdc2c58 175 y_inv = true;
Akito914 0:0eddebdc2c58 176 cTarg.y = cStart.y * 2 - cTarg.y;
Akito914 0:0eddebdc2c58 177 }
Akito914 1:3663d0f20136 178
Akito914 0:0eddebdc2c58 179 delta.x = cTarg.x - cStart.x;
Akito914 0:0eddebdc2c58 180 delta.y = cTarg.y - cStart.y;
Akito914 0:0eddebdc2c58 181
Akito914 2:80eb19e9091e 182 if((delta.x == 0)&&(delta.y == 0)){
Akito914 2:80eb19e9091e 183 return;
Akito914 2:80eb19e9091e 184 }
Akito914 2:80eb19e9091e 185
Akito914 0:0eddebdc2c58 186 if(delta.x < delta.y){
Akito914 0:0eddebdc2c58 187 motionCal.setTarg(delta.y);
Akito914 0:0eddebdc2c58 188 }
Akito914 0:0eddebdc2c58 189 else{
Akito914 0:0eddebdc2c58 190 motionCal.setTarg(delta.x);
Akito914 0:0eddebdc2c58 191 }
Akito914 0:0eddebdc2c58 192
Akito914 0:0eddebdc2c58 193 while(true) {
Akito914 0:0eddebdc2c58 194 inc.x = 0;
Akito914 0:0eddebdc2c58 195 inc.y = 0;
Akito914 1:3663d0f20136 196 if(delta.x == 0){
Akito914 2:80eb19e9091e 197 if(cCurrent.y < cTarg.y){
Akito914 2:80eb19e9091e 198 cCurrent.y++;
Akito914 2:80eb19e9091e 199 inc.y = 1;
Akito914 2:80eb19e9091e 200 }
Akito914 2:80eb19e9091e 201 else{
Akito914 2:80eb19e9091e 202 debugPort.printf("%d, %d : %d\r\n", cCurrent.x, cCurrent.y, error);
Akito914 1:3663d0f20136 203 break;
Akito914 1:3663d0f20136 204 }
Akito914 1:3663d0f20136 205 period_us = motionCal.calStepDelay(cCurrent.y - cStart.y);
Akito914 1:3663d0f20136 206 }
Akito914 1:3663d0f20136 207 else if(delta.y == 0){
Akito914 2:80eb19e9091e 208 if(cCurrent.x < cTarg.x){
Akito914 2:80eb19e9091e 209 cCurrent.x++;
Akito914 2:80eb19e9091e 210 inc.x = 1;
Akito914 2:80eb19e9091e 211 }
Akito914 2:80eb19e9091e 212 else{
Akito914 2:80eb19e9091e 213 debugPort.printf("%d, %d : %d\r\n", cCurrent.x, cCurrent.y, error);
Akito914 1:3663d0f20136 214 break;
Akito914 1:3663d0f20136 215 }
Akito914 1:3663d0f20136 216 period_us = motionCal.calStepDelay(cCurrent.x - cStart.x);
Akito914 1:3663d0f20136 217 }
Akito914 2:80eb19e9091e 218 else if(delta.y > delta.x) {
Akito914 2:80eb19e9091e 219 if(cCurrent.y < cTarg.y){
Akito914 2:80eb19e9091e 220 cCurrent.y++;
Akito914 2:80eb19e9091e 221 inc.y = 1;
Akito914 2:80eb19e9091e 222 }
Akito914 2:80eb19e9091e 223 else{
Akito914 2:80eb19e9091e 224 debugPort.printf("%d, %d : %d\r\n", cCurrent.x, cCurrent.y, error);
Akito914 2:80eb19e9091e 225 break;
Akito914 2:80eb19e9091e 226 }
Akito914 0:0eddebdc2c58 227 error += delta.x;
Akito914 2:80eb19e9091e 228 if(error >= delta.y) {
Akito914 0:0eddebdc2c58 229 error -= delta.y;
Akito914 0:0eddebdc2c58 230 cCurrent.x++;
Akito914 0:0eddebdc2c58 231 inc.x = 1;
Akito914 0:0eddebdc2c58 232 }
Akito914 2:80eb19e9091e 233 period_us = motionCal.calStepDelay(cCurrent.y - cStart.y);
Akito914 2:80eb19e9091e 234 } else {
Akito914 2:80eb19e9091e 235 if(cCurrent.x < cTarg.x){
Akito914 2:80eb19e9091e 236 cCurrent.x++;
Akito914 2:80eb19e9091e 237 inc.x = 1;
Akito914 2:80eb19e9091e 238 }
Akito914 2:80eb19e9091e 239 else{
Akito914 2:80eb19e9091e 240 debugPort.printf("%d, %d : %d\r\n", cCurrent.x, cCurrent.y, error);
Akito914 1:3663d0f20136 241 break;
Akito914 1:3663d0f20136 242 }
Akito914 0:0eddebdc2c58 243 error += delta.y;
Akito914 2:80eb19e9091e 244 if(error >= delta.x) {
Akito914 0:0eddebdc2c58 245 error -= delta.x;
Akito914 0:0eddebdc2c58 246 cCurrent.y++;
Akito914 0:0eddebdc2c58 247 inc.y = 1;
Akito914 0:0eddebdc2c58 248 }
Akito914 0:0eddebdc2c58 249 period_us = motionCal.calStepDelay(cCurrent.x - cStart.x);
Akito914 0:0eddebdc2c58 250 }
Akito914 0:0eddebdc2c58 251
Akito914 0:0eddebdc2c58 252 if(inc.x != 0)x_axis.oneStep(x_inv == 0);
Akito914 0:0eddebdc2c58 253 if(inc.y != 0)y_axis.oneStep(y_inv == 0);
Akito914 0:0eddebdc2c58 254
Akito914 0:0eddebdc2c58 255 wait_us(period_us);
Akito914 1:3663d0f20136 256
Akito914 0:0eddebdc2c58 257 }
Akito914 0:0eddebdc2c58 258 }
Akito914 0:0eddebdc2c58 259
Akito914 0:0eddebdc2c58 260 void trapezoid(int32_t targSteps)
Akito914 0:0eddebdc2c58 261 {
Akito914 0:0eddebdc2c58 262 int revMode = 0;
Akito914 0:0eddebdc2c58 263
Akito914 0:0eddebdc2c58 264 uint32_t period_us = 0;
Akito914 0:0eddebdc2c58 265 uint32_t steps = 0;
Akito914 0:0eddebdc2c58 266
Akito914 0:0eddebdc2c58 267 if(targSteps < 0) {
Akito914 0:0eddebdc2c58 268 revMode = 1;
Akito914 0:0eddebdc2c58 269 targSteps *= -1;
Akito914 0:0eddebdc2c58 270 }
Akito914 0:0eddebdc2c58 271
Akito914 0:0eddebdc2c58 272 motionCal.setTarg(targSteps);
Akito914 0:0eddebdc2c58 273
Akito914 0:0eddebdc2c58 274 while (steps <= targSteps) {
Akito914 0:0eddebdc2c58 275
Akito914 0:0eddebdc2c58 276 period_us = motionCal.calStepDelay(steps);
Akito914 0:0eddebdc2c58 277
Akito914 0:0eddebdc2c58 278 steps += 1;
Akito914 0:0eddebdc2c58 279
Akito914 0:0eddebdc2c58 280 x_axis.oneStep(revMode != 1);
Akito914 0:0eddebdc2c58 281
Akito914 0:0eddebdc2c58 282 wait_us(period_us);
Akito914 0:0eddebdc2c58 283
Akito914 0:0eddebdc2c58 284 }
Akito914 1:3663d0f20136 285 }
Akito914 0:0eddebdc2c58 286
Akito914 1:3663d0f20136 287
Akito914 1:3663d0f20136 288 void beep(int T_us,int t_ms){
Akito914 1:3663d0f20136 289
Akito914 1:3663d0f20136 290 if(T_us==0 || t_ms==0)return;
Akito914 1:3663d0f20136 291
Akito914 1:3663d0f20136 292 speaker.period_us(T_us);
Akito914 1:3663d0f20136 293 speaker.write(0.10);
Akito914 1:3663d0f20136 294
Akito914 1:3663d0f20136 295 wait_ms(t_ms);
Akito914 1:3663d0f20136 296
Akito914 1:3663d0f20136 297 speaker.write(0.0);
Akito914 1:3663d0f20136 298 speaker.period_us(100);
Akito914 1:3663d0f20136 299
Akito914 1:3663d0f20136 300 return;
Akito914 0:0eddebdc2c58 301 }
Akito914 1:3663d0f20136 302
Akito914 1:3663d0f20136 303 void pi(int times){
Akito914 1:3663d0f20136 304 int count=0;
Akito914 1:3663d0f20136 305
Akito914 1:3663d0f20136 306 for(count=0;count<times;count++){
Akito914 1:3663d0f20136 307 beep(379,50);
Akito914 1:3663d0f20136 308 wait_ms(50);
Akito914 1:3663d0f20136 309 }
Akito914 1:3663d0f20136 310 wait_ms(300);
Akito914 1:3663d0f20136 311 }
Akito914 1:3663d0f20136 312
Akito914 1:3663d0f20136 313 void q_sound(void){
Akito914 1:3663d0f20136 314 beep(478,100);
Akito914 1:3663d0f20136 315 beep(379,100);
Akito914 1:3663d0f20136 316 }
Akito914 1:3663d0f20136 317
Akito914 1:3663d0f20136 318 void ans_sound(void){
Akito914 1:3663d0f20136 319 beep(379,100);
Akito914 1:3663d0f20136 320 beep(478,100);
Akito914 1:3663d0f20136 321 }
Akito914 1:3663d0f20136 322
Akito914 1:3663d0f20136 323 void piroro(void){
Akito914 1:3663d0f20136 324 beep(379,100);
Akito914 1:3663d0f20136 325 beep(426,100);
Akito914 1:3663d0f20136 326 beep(478,100);
Akito914 1:3663d0f20136 327 }
Akito914 1:3663d0f20136 328
Akito914 1:3663d0f20136 329
Akito914 1:3663d0f20136 330
Akito914 1:3663d0f20136 331
Akito914 1:3663d0f20136 332