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.
Dependencies: mbed
motor.cpp@3:6898c5bd7fef, 2018-08-20 (annotated)
- Committer:
- msimeone234
- Date:
- Mon Aug 20 08:51:01 2018 +0000
- Revision:
- 3:6898c5bd7fef
- Parent:
- 2:ff609fd0c51a
- Child:
- 4:ab3a8a7a0fcf
cleaned up
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| duchung2603 | 0:a8339cee32b7 | 1 | #include "mbed.h" |
| duchung2603 | 0:a8339cee32b7 | 2 | #include "motor.h" |
| duchung2603 | 0:a8339cee32b7 | 3 | |
| duchung2603 | 0:a8339cee32b7 | 4 | |
| duchung2603 | 0:a8339cee32b7 | 5 | // ################################################################ |
| duchung2603 | 0:a8339cee32b7 | 6 | // SPI通信フォーマットに合わせた供用体定義 |
| duchung2603 | 0:a8339cee32b7 | 7 | // bit |15|14|13|12|11|10| 9| 8| 7| 6| 5| 4| 3| 2| 1| 0| |
| duchung2603 | 0:a8339cee32b7 | 8 | // function | KeyBit | ChNumber | Motor Thlotle | |
| duchung2603 | 0:a8339cee32b7 | 9 | // ################################################################ |
| duchung2603 | 0:a8339cee32b7 | 10 | typedef union{ |
| duchung2603 | 0:a8339cee32b7 | 11 | UINT16 spiCmd; |
| duchung2603 | 0:a8339cee32b7 | 12 | struct{ |
| duchung2603 | 0:a8339cee32b7 | 13 | UINT16 val : 8;//bit[ 7: 0] |
| duchung2603 | 0:a8339cee32b7 | 14 | UINT16 ch : 4;//bit[11: 8] |
| duchung2603 | 0:a8339cee32b7 | 15 | UINT16 key : 4;//bit[15:12] |
| duchung2603 | 0:a8339cee32b7 | 16 | }bf; |
| duchung2603 | 0:a8339cee32b7 | 17 | }SPI_CMD; |
| duchung2603 | 0:a8339cee32b7 | 18 | |
| duchung2603 | 0:a8339cee32b7 | 19 | |
| duchung2603 | 0:a8339cee32b7 | 20 | // ############################################################### |
| duchung2603 | 0:a8339cee32b7 | 21 | // グローバル変数 |
| duchung2603 | 0:a8339cee32b7 | 22 | // ############################################################### |
| duchung2603 | 0:a8339cee32b7 | 23 | static SPI spi (p5,p6,p7); // mosi, miso, clk |
| duchung2603 | 0:a8339cee32b7 | 24 | static DigitalOut CS_n(p8); //SPIのチップセレクト |
| duchung2603 | 0:a8339cee32b7 | 25 | static Serial pc (USBTX , USBRX ); |
| duchung2603 | 0:a8339cee32b7 | 26 | |
| duchung2603 | 0:a8339cee32b7 | 27 | MOTOR_THROTTLE mt={0,}; |
| msimeone234 | 3:6898c5bd7fef | 28 | static UCHAR SValue = 0; |
| duchung2603 | 0:a8339cee32b7 | 29 | |
| duchung2603 | 0:a8339cee32b7 | 30 | //================================================================ |
| duchung2603 | 0:a8339cee32b7 | 31 | //モーター制御モジュール初期化 |
| duchung2603 | 0:a8339cee32b7 | 32 | //================================================================ |
| duchung2603 | 0:a8339cee32b7 | 33 | void motorInit(){ |
| duchung2603 | 0:a8339cee32b7 | 34 | spi.format(16,3) ;//SPIのフォーマット指定(bit長、極性) |
| duchung2603 | 0:a8339cee32b7 | 35 | spi.frequency(500000) ;//クロック周波数 |
| duchung2603 | 0:a8339cee32b7 | 36 | //Low有意なのでHighにしておく |
| duchung2603 | 0:a8339cee32b7 | 37 | CS_n =1; |
| duchung2603 | 0:a8339cee32b7 | 38 | } |
| duchung2603 | 0:a8339cee32b7 | 39 | //---------------------------------------------------------------- |
| duchung2603 | 0:a8339cee32b7 | 40 | //SPI送信 |
| duchung2603 | 0:a8339cee32b7 | 41 | //---------------------------------------------------------------- |
| msimeone234 | 3:6898c5bd7fef | 42 | |
| duchung2603 | 0:a8339cee32b7 | 43 | void motorSpiSend |
| duchung2603 | 0:a8339cee32b7 | 44 | (UCHAR ch //チャンネル番号 |
| duchung2603 | 0:a8339cee32b7 | 45 | ,UCHAR val //モータ設定値0~255 |
| duchung2603 | 0:a8339cee32b7 | 46 | ) |
| duchung2603 | 0:a8339cee32b7 | 47 | { |
| duchung2603 | 0:a8339cee32b7 | 48 | SPI_CMD cmd; |
| duchung2603 | 0:a8339cee32b7 | 49 | //引数を送信するコマンドに成形 |
| duchung2603 | 0:a8339cee32b7 | 50 | cmd.bf.key = 0xA; //キーbit :常に0xA |
| duchung2603 | 0:a8339cee32b7 | 51 | cmd.bf.ch = ch; //チャンネル番号 :1~12が有効 |
| duchung2603 | 0:a8339cee32b7 | 52 | cmd.bf.val = val; //モータースロットル :0~255 |
| duchung2603 | 0:a8339cee32b7 | 53 | //チップセレクトアサート |
| duchung2603 | 0:a8339cee32b7 | 54 | CS_n= 0; |
| duchung2603 | 0:a8339cee32b7 | 55 | //データ出力 |
| duchung2603 | 0:a8339cee32b7 | 56 | spi.write(cmd.spiCmd); |
| duchung2603 | 0:a8339cee32b7 | 57 | //チップセレクトネゲート |
| duchung2603 | 0:a8339cee32b7 | 58 | CS_n = 1; |
| duchung2603 | 0:a8339cee32b7 | 59 | |
| duchung2603 | 0:a8339cee32b7 | 60 | pc.printf("ch[%d] %d\r\n" , cmd.bf.ch , cmd.bf.val); |
| duchung2603 | 0:a8339cee32b7 | 61 | } |
| duchung2603 | 0:a8339cee32b7 | 62 | //================================================================ |
| msimeone234 | 3:6898c5bd7fef | 63 | //Motor Tests |
| duchung2603 | 0:a8339cee32b7 | 64 | //================================================================ |
| msimeone234 | 3:6898c5bd7fef | 65 | void motorTestUp(){ |
| msimeone234 | 3:6898c5bd7fef | 66 | int ch,val; |
| msimeone234 | 3:6898c5bd7fef | 67 | for(ch=1; ch<3; ch++){ |
| msimeone234 | 3:6898c5bd7fef | 68 | for(val=0; val<30; val++){ |
| msimeone234 | 3:6898c5bd7fef | 69 | motorSpiSend(ch,val); |
| msimeone234 | 3:6898c5bd7fef | 70 | wait(0.01); |
| msimeone234 | 3:6898c5bd7fef | 71 | } |
| msimeone234 | 3:6898c5bd7fef | 72 | wait(1); |
| duchung2603 | 1:c002cfb55315 | 73 | } |
| duchung2603 | 0:a8339cee32b7 | 74 | for(ch=5; ch<9; ch++){ |
| msimeone234 | 3:6898c5bd7fef | 75 | for(val=0; val<30; val++){ |
| duchung2603 | 0:a8339cee32b7 | 76 | motorSpiSend(ch,val); |
| duchung2603 | 0:a8339cee32b7 | 77 | wait(0.01); |
| duchung2603 | 0:a8339cee32b7 | 78 | } |
| msimeone234 | 3:6898c5bd7fef | 79 | wait(1); |
| duchung2603 | 0:a8339cee32b7 | 80 | } |
| msimeone234 | 3:6898c5bd7fef | 81 | } |
| msimeone234 | 3:6898c5bd7fef | 82 | |
| msimeone234 | 3:6898c5bd7fef | 83 | void motorTestDown(){ |
| msimeone234 | 3:6898c5bd7fef | 84 | int ch,val; |
| msimeone234 | 3:6898c5bd7fef | 85 | for(ch=5; ch<9; ch++){ |
| msimeone234 | 3:6898c5bd7fef | 86 | for(val=30; val>0; val--){ |
| msimeone234 | 3:6898c5bd7fef | 87 | motorSpiSend(ch,val); |
| msimeone234 | 3:6898c5bd7fef | 88 | wait(0.01); |
| msimeone234 | 3:6898c5bd7fef | 89 | } |
| msimeone234 | 3:6898c5bd7fef | 90 | wait(1); |
| msimeone234 | 3:6898c5bd7fef | 91 | } |
| msimeone234 | 3:6898c5bd7fef | 92 | for(ch=1; ch<3; ch++){ |
| msimeone234 | 3:6898c5bd7fef | 93 | for(val=30; val>0; val--){ |
| msimeone234 | 3:6898c5bd7fef | 94 | motorSpiSend(ch,val); |
| msimeone234 | 3:6898c5bd7fef | 95 | wait(0.01); |
| msimeone234 | 3:6898c5bd7fef | 96 | } |
| msimeone234 | 3:6898c5bd7fef | 97 | wait(1); |
| msimeone234 | 3:6898c5bd7fef | 98 | } |
| msimeone234 | 3:6898c5bd7fef | 99 | } |
| msimeone234 | 3:6898c5bd7fef | 100 | |
| msimeone234 | 3:6898c5bd7fef | 101 | void motorUp(){ |
| msimeone234 | 3:6898c5bd7fef | 102 | int ch ,v; |
| msimeone234 | 3:6898c5bd7fef | 103 | for (v=0; v<10; v++){ // for each 1 unit of speed v |
| msimeone234 | 3:6898c5bd7fef | 104 | if (SValue < 0xFF) { // check if speed less than max |
| msimeone234 | 3:6898c5bd7fef | 105 | SValue++; |
| msimeone234 | 3:6898c5bd7fef | 106 | } |
| msimeone234 | 3:6898c5bd7fef | 107 | for(ch=1; ch<3; ch++){ // each channel |
| msimeone234 | 3:6898c5bd7fef | 108 | motorSpiSend(ch,SValue); // increase speed by 1 unit |
| msimeone234 | 3:6898c5bd7fef | 109 | wait(0.01); // 2nd motor increases .01s after 1st motor |
| msimeone234 | 3:6898c5bd7fef | 110 | } |
| msimeone234 | 3:6898c5bd7fef | 111 | } |
| duchung2603 | 0:a8339cee32b7 | 112 | } |
| duchung2603 | 0:a8339cee32b7 | 113 | |
| duchung2603 | 2:ff609fd0c51a | 114 | void motorStop(){ |
| duchung2603 | 0:a8339cee32b7 | 115 | int ch, v; |
| msimeone234 | 3:6898c5bd7fef | 116 | for (v=SValue; v>0; v--){ // for each 1 unit of speed v |
| msimeone234 | 3:6898c5bd7fef | 117 | for(ch=1; ch<3; ch++){ // each channel |
| msimeone234 | 3:6898c5bd7fef | 118 | motorSpiSend(ch,v); // decrease speed by 1 unit |
| msimeone234 | 3:6898c5bd7fef | 119 | wait(0.01); // 2nd motor decreses .01s after 1st motor |
| duchung2603 | 0:a8339cee32b7 | 120 | } |
| msimeone234 | 3:6898c5bd7fef | 121 | } |
| msimeone234 | 3:6898c5bd7fef | 122 | SValue = 0; // after motors have stopped, reset speed value |
| duchung2603 | 0:a8339cee32b7 | 123 | } |
| duchung2603 | 0:a8339cee32b7 | 124 | |
| duchung2603 | 0:a8339cee32b7 | 125 |