teamALI / Mbed 2 deprecated RubyHwTest2

Dependencies:   mbed

Committer:
msimeone234
Date:
Tue Aug 21 01:04:17 2018 +0000
Revision:
5:94b196231ecd
Parent:
4:ab3a8a7a0fcf
now clean, with small motors included

Who changed what in which revision?

UserRevisionLine numberNew 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 5:94b196231ecd 103 for (v=0; v<10; v++) // for each 1 unit of speed v (until 10)
msimeone234 5:94b196231ecd 104 {
msimeone234 5:94b196231ecd 105 if (SValue < 0xFF) // check if speed less than max
msimeone234 5:94b196231ecd 106 {
msimeone234 3:6898c5bd7fef 107 SValue++;
msimeone234 4:ab3a8a7a0fcf 108 }
msimeone234 5:94b196231ecd 109 for(ch=1; ch<3; ch++) // each big motor
msimeone234 5:94b196231ecd 110 {
msimeone234 3:6898c5bd7fef 111 motorSpiSend(ch,SValue); // increase speed by 1 unit
msimeone234 3:6898c5bd7fef 112 wait(0.01); // 2nd motor increases .01s after 1st motor
msimeone234 3:6898c5bd7fef 113 }
msimeone234 5:94b196231ecd 114 for(ch=5; ch<9; ch++)// each small motor
msimeone234 5:94b196231ecd 115 {
msimeone234 4:ab3a8a7a0fcf 116 motorSpiSend(ch,SValue); // increase speed by 1 unit
msimeone234 4:ab3a8a7a0fcf 117 wait(0.01); // 2nd motor increases .01s after 1st motor
msimeone234 5:94b196231ecd 118 }
msimeone234 3:6898c5bd7fef 119 }
duchung2603 0:a8339cee32b7 120 }
duchung2603 0:a8339cee32b7 121
duchung2603 2:ff609fd0c51a 122 void motorStop(){
duchung2603 0:a8339cee32b7 123 int ch, v;
msimeone234 5:94b196231ecd 124 for (v=SValue; v>0; v--) // for each 1 unit of speed v
msimeone234 5:94b196231ecd 125 {
msimeone234 5:94b196231ecd 126 for(ch=1; ch<3; ch++) // each big motor
msimeone234 5:94b196231ecd 127 {
msimeone234 4:ab3a8a7a0fcf 128 motorSpiSend(ch,v); // decrease speed by 1 unit
msimeone234 4:ab3a8a7a0fcf 129 wait(0.01); // 2nd motor decreses .01s after 1st motor
msimeone234 4:ab3a8a7a0fcf 130 }
msimeone234 5:94b196231ecd 131 for(ch=5; ch<9; ch++)// each small motor
msimeone234 5:94b196231ecd 132 {
msimeone234 3:6898c5bd7fef 133 motorSpiSend(ch,v); // decrease speed by 1 unit
msimeone234 3:6898c5bd7fef 134 wait(0.01); // 2nd motor decreses .01s after 1st motor
duchung2603 0:a8339cee32b7 135 }
msimeone234 3:6898c5bd7fef 136 }
msimeone234 3:6898c5bd7fef 137 SValue = 0; // after motors have stopped, reset speed value
duchung2603 0:a8339cee32b7 138 }
duchung2603 0:a8339cee32b7 139
duchung2603 0:a8339cee32b7 140