デジタルコンパスを使う

デジタルコンパスを使う



compassHM55B.cs
using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.NetduinoMini;
using BoeBotLib;
/**
 * Class for HM55B compass.
 *
 * このクラスの使い方:
 *
 *
 *  // compass オブジェクトを作る
 *    compassHM55B cp = new compassHM55B();
 *    compassHM55B cp = new compassHM55B(CPU.P0,CPU.P1,CPU.P2);
 *
 *  cp.start()を呼んで計測を開始させる。
 *  次のメソッドがある。
 *  計測が終了したかどうかを調べるには、cp.poll() を呼ぶ。
 *  trueが戻ってきたら、cp.getCompassX(), cp.getCompassY, cp.getCompassAngle()を呼ぶ。
 *  次の計測のために再度cp.start() 呼ぶ。
 *  
 * 例
 *          compassHM55B cp = new compassHM55B();
 *          cp.start(); // Start a measurement
 *          while (true)
 *          {
 *              if (cp.poll())
 *              {
 *                  Console.WriteLine(cp.getCompassAngle());
 *                  cp.start(); // Start a measurement
 *                  CPU.delay(10);
 *              }
 *          }
 *
 *
 */
namespace BoeBotLib
{
    public class compassHM55B
    {
        static readonly ushort RESET = 0x0000;
        static readonly ushort MEASURE = 0x0008;
        static readonly ushort REPORT = 0x000c;
        static readonly ushort READY = 0x000c;
        Cpu.Pin DinDout;
        Cpu.Pin Clk;
        Cpu.Pin En;
        int m_x;       //x-axis value
        int m_y;       //y-axis value

        /**
         * コンストラクタ
         *
         * DinDout:  双方向通信に用いるピン番号
         * Clk   :  シリアルクロック出力に用いるピン番号
         * En   :  チップイネーブルに用いる出力ピン番号
         */
        public compassHM55B()
        {  // デフォールト コンストラクタ
            DinDout = CPU.P0;
            Clk = CPU.P1;
            En = CPU.P2;
            CPU.setOutput(En);
            CPU.setOutput(Clk);
            CPU.writePin(En, true);   //initialize enable pin to high output
            CPU.writePin(Clk, false); //initialize clock pin to low output
            reset();
          
        }

        public compassHM55B(Cpu.Pin DinDout, Cpu.Pin Clk, Cpu.Pin En)
        {
            this.DinDout = DinDout;
            this.Clk = Clk;
            this.En = En; ;
            CPU.setOutput(En);
            CPU.setOutput(Clk);
            CPU.writePin(En, true);   //initialize enable pin to high output
            CPU.writePin(Clk, false); //initialize clock pin to low output
            reset();
        }

        /**
         * X方向の値を得る
         *
         * 戻り: X方向の値
         */
        public int getCompassX()
        {
            return m_x;
        }

        /**
         * Y方向の値を得る
         *
         * 戻り: Y方向の値
         */
        public int getCompassY()
        {
            return m_y;
        }

        /**
         * 方位の角度を得る
         *
         * 戻り: 角度(0-359).
         */
        public int getCompassAngle()
        {
           // if (m_x == 0) return 0;
            return (int)(System.Math.Atan2((double)-m_y, (double)m_x) * 57.29577951308232 + 0.5);
        }

        /**
         * コンパスをリセットする
         */
        void reset()
        {
            CPU.writePin(En, false);
            CPU.shiftOut(DinDout, Clk, 4, CPU.SHIFT_MSB, RESET);
            CPU.writePin(En, true);
        }

        /**
         * コンパスの計測を開始させる
         */
        public void start()
        {
            CPU.writePin(En, false);
            CPU.shiftOut(DinDout, Clk, 4, CPU.SHIFT_MSB, MEASURE);
        }

        /**
         * 計測終了を調べる
         *
         * 戻り: 計測終了の場合trueそうでない時false
         */
        public bool poll()
        {
            bool result;
            int m_status;
            CPU.writePin(En, true);
            Timer.delay(10);
            CPU.writePin(En, false);
            CPU.shiftOut(DinDout, Clk, 4, CPU.SHIFT_MSB, REPORT);
            m_status = CPU.shiftIn(DinDout, Clk, 4, CPU.POST_CLOCK_MSB);
            result = (m_status == READY);
            if (result)
            {
                m_x = CPU.shiftIn(DinDout, Clk, 11, CPU.POST_CLOCK_MSB);
                m_y = CPU.shiftIn(DinDout, Clk, 11, CPU.POST_CLOCK_MSB);
                CPU.writePin(En, true);
                if ((m_x & 0x0400) != 0)
                {
                    uint x = 0xfffffc00;
                    x |= (uint)m_x;
                    m_x = (int)x;
                }

                if ((m_y & 0x0400) != 0)
                {
                    uint y = 0xfffffc00;
                    y |= (uint)m_y;
                    m_y = (int)y;
                }
            }
            return result;
        }
    }
}
 1 
 2 
 3 
 4 
 5 
 6 
 7 
 8 
 9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
32 
33 
34 
35 
36 
37 
38 
39 
40 
41 
42 
43 
44 
45 
46 
47 
48 
49 
50 
51 
52 
53 
54 
55 
56 
57 
58 
59 
60 
61 
62 
63 
64 
65 
66 
67 
68 
69 
70 
71 
72 
73 
74 
75 
76 
77 
78 
79 
80 
81 
82 
83 
84 
85 
86 
87 
88 
89 
90 
91 
92 
93 
94 
95 
96 
97 
98 
99 
100 
101
102
103 
104 
105 
106 
107 
108 
109 
110 
111 
112 
113 
114 
115 
116 
117 
118 
119 
120 
121 
122 
123 
124 
125 
126 
127 
128 
129 
130 
131 
132 
133 
134 
135 
136 
137 
138 
139 
140 
141 
142 
143 
144 
145 
146 
147 
148 
149 
150 
151 
152 
153 
154 
155 
156 
157 
158 
159 
160 
161 
162 
163 
164 
165 
166 
167 
168 
169 
170 
171 


I2CLCDクラス



I2CLCD.cs
//#define GHIE
#define SecretLabs

using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
#if GHIE
using GHI.OSHW.Hardware;        // FEZCerberus使用のため追加、参照設定にも追加
#elif SecretLabs
using SecretLabs.NETMF.Hardware;
#endif
using BoeBotLib;
namespace BoeBotLib
{
    class I2CLCD
    {
        // Set Constants for Address and Clock Frequency
        const byte LCD_ADDRESS = 0x3E; 
        const int CLOCK_FREQ = 100;
        const int TIMEOUT = 1000;
        int chCount;
        int columnNumber = 16;

        //Write Buffer
        byte[] WriteBuffer = new byte[3];

        //Init I2C Device Config
        I2CDevice.Configuration config =
            new I2CDevice.Configuration(LCD_ADDRESS, CLOCK_FREQ);

        //Init I2C Device & LCD initialize
        I2CDevice i2c;

        public I2CLCD()
        {
            this.columnNumber = 16;
            i2c = new I2CDevice(config);
            init();
        }
        public I2CLCD(int columnNumber)
        {
            this.columnNumber = columnNumber;
            i2c = new I2CDevice(config);
            init();
        }

        public void write(byte control, byte data)
        {
            i2c.Execute(new I2CDevice.I2CTransaction[1] {
                I2CDevice.CreateWriteTransaction(new Byte[] { control, data }) }, TIMEOUT);
            //Thread.Sleep(5);
        }

        void wait_us(int tm)
        {
            BoeBotLib.Timer.delay(tm * 10);
        }
        void wait_ms(int tm)
        {
            BoeBotLib.CPU.delay(tm);
        }
        void init()
        {
            int Wait = 50;
            chCount = 0;
            wait_ms(40);
            // LCD_WRITE= 0x7c;
            write(0x0, 0x38);   //data8bit, numberOfLine=2
            wait_us(Wait);
            write(0x0, 0x39);   //data8bit, numberOfLine=2
            wait_us(Wait);
            write(0x0, 0x14);   //internal OSC frequency
            wait_us(Wait);
            write(0x0, 0x7A);   // contrast
            wait_us(Wait);
            write(0x0, 0x5F);   // power/icon/contrast control
            wait_us(Wait);
            write(0x0, 0x6a);   // follower control
            wait_ms(200);          // 200ms
            write(0x0, 0x0c);   // Display ON
            wait_us(Wait);

            // write(0x0, 0x0E);   // cursor on, blink
            // wait_us(Wait);

            write(0x0, 0x01);   // clear disply
            wait_us(2000);         // 2ms
            write(0x00, 0x06);   // entry mode set
            wait_us(Wait);
        }

        public void write(byte ch)
        {
            int x;
            //int cn2 = columnNumber / 2;
            if (ch == 0x0a)
            {
                x = chCount % (columnNumber * 2);
                if (x < columnNumber)
                {
                    write(0x00, 0xc0);
                    chCount = columnNumber;
                }
                else
                {
                    write(0x00, 0x01); // clear display
                    chCount = 0;
                    wait_ms(2);
                }
            }
            else if (ch == 0x0d)
            {
                if (chCount < columnNumber)
                {
                    home();
                    //write(0x00, 0x80);
                    chCount = 0;
                }
                else
                {
                    write(0x00, 0xC0);
                    chCount = columnNumber;
                }
                wait_ms(2);
            }
            else
            {
                write(0x40, ch);
                x = ++chCount % (columnNumber * 2);
                if (x == columnNumber) write(0x00, 0xc0);
                else if (x == 0) home();
            }
        }

        public void write(String str)
        {
            foreach (byte ch in str) write(ch);
        }

        public void write(byte[] str, int n)
        {
            for (int i = 0; i < n; i++) write(str[i]);
        }

        public void clear()
        {
            write(0x00, 0x01); // clear display
            chCount = 0;
            wait_ms(2);
        }

        public void home()
        {
            write(0x00, 0x02); // return to home
            chCount = 0;
            wait_ms(2);
        }

        public void write(int x)
        {
            byte[] num = new byte[5];
            int n = 0;
            if (x < 0)
            {     // マイナスの値の場合
                write((byte)'-');
                x *= -1;
            }
            do
            {
                num[n++] = (byte)(x % 10 + (int)'0');
                x /= 10;
            } while (x != 0);
            while (--n >= 0) write(num[n]);
        }

        public void write(int x, int column, bool zeroPad)
        {
            byte[] Buffer = new byte[10];
            byte[] num = new byte[5];
            int i, j, n = 0;
            bool signFlag = false;
            if (x < 0)
            {     // マイナスの値の場合
                signFlag = true;
                x *= -1;
            }
            do
            {
                num[n++] = (byte)(x % 10);
                x /= 10;
            } while (x != 0);
            i = column - n;
            if (i > 0)
            {
                for (i = 0; i < 10; i++) Buffer[i] = (byte)' ';
                if (zeroPad) for (i = 0; i < column - n; i++) Buffer[i] = (byte)'0';
                Buffer[9] = 0;
                for (i = column - n, j = n - 1; i < column; i++, j--) 
                    Buffer[i] = (byte)(num[j] | '0');
                Buffer[i] = 0;
                if (signFlag) Buffer[column - n - 1] = (byte)'-';
            }
            else
            {
                j = 0;
                if (signFlag) Buffer[j++] = (byte)'-';
                for (i = --n; i >= 0; i--) Buffer[j++] = (byte)(num[i] | '0');
                Buffer[j] = 0;
            }
            write(Buffer, column);
        }

        public void setCG(bool mode, byte[] cgData)
        {
            int i, j;
            write(0x00, 0x38);     // Function set IS= 0
            write(0x00, 0x40);     // CGRAM set address= 0
            if (mode)
            {            // StringModeで呼ばれた時はデフォールトにセット
                for (i = 0; i < 8; i++)
                {
                    for (j = 0; j < 8; j++)
                    {
                        if (j < (7 - i)) write(0x40, 0x00);
                        else write(0x40, 0x1f);     // 横棒をセット
                    }
                }
            }
            else
            {
                for (i = 0; i < 64; i++) write(0x40, cgData[i]);
            }
        }


        public void setIcon(int x)
        {
            write(0x00, 0x39);    // Function set
            write(0x00, 0x40 | 0x0f);   // flower icon
            if ((x & 1)!=0) write(0x40, 0x10);
            else write(0x40, 0x00);

            write(0x00, 0x40 | 0x0d);   // Battery icon
            write(0x40, 0);
            if ((x & 0x0e)!=0)
            {
                write(0x00, 0x40 | 0x0d);   // Battery icon
                write(0x40, (byte)(((x & 0x0e) << 1) | 2));
            }

            write(0x00, 0x40 | 0x0b);   // no sound icon
            if ((x & 0x10) != 0) write(0x40, 0x10);
            else write(0x40, 0);

            write(0x00, 0x40 | 0x09);   // Key icon
            if ((x & 0x20) != 0) write(0x40, 0x10);
            else write(0x40, 0);

            write(0x00, 0x40 | 0x07);   // 矢印 icon
            write(0x40, 0);
            if ((x & 0xC0) != 0)
            {
                write(0x00, 0x40 | 0x07);   // 矢印 icon
                write(0x40, (byte)((x & 0xC0) >> 3));
            }

            write(0x00, 0x40 | 0x06);   // enter icon
            if ((x & 0x100) != 0) write(0x40, 0x10);
            else write(0x40, 0);
            write(0x00, 0x40 | 0x04);   // ring icon
            if ((x & 0x200) != 0) write(0x40, 0x10);
            else write(0x40, 0);
            write(0x00, 0x40 | 0x02);   // 電話 icon
            if ((x & 0x400) != 0) write(0x40, 0x10);
            else write(0x40, 0);
            write(0x00, 0x40);    // antena icon
            if ((x & 0x800) != 0) write(0x40, 0x10);
            else write(0x40, 0);
            write(0x00, 0x38);    // Function set
        }
    }
}
 1 
 2 
 3 
 4 
 5 
 6 
 7 
 8 
 9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
32 
33 
34 
35 
36 
37 
38 
39 
40 
41 
42 
43 
44 
45 
46 
47 
48 
49 
50 
51 
52 
53 
54 
55 
56 
57 
58 
59 
60 
61 
62 
63 
64 
65 
66 
67 
68 
69 
70 
71 
72 
73 
74 
75 
76 
77 
78 
79 
80 
81 
82 
83 
84 
85 
86 
87 
88 
89 
90 
91 
92 
93 
94 
95 
96 
97 
98 
99 
100 
101
102
103 
104 
105 
106 
107 
108 
109 
110 
111 
112 
113 
114 
115 
116 
117 
118 
119 
120 
121 
122 
123 
124 
125 
126 
127 
128 
129 
130 
131 
132 
133 
134 
135 
136 
137 
138 
139 
140 
141 
142 
143 
144 
145 
146 
147 
148 
149 
150 
151
152
153 
154 
155 
156 
157 
158 
159 
160 
161 
162 
163 
164 
165 
166 
167 
168 
169 
170 
171 
172 
173 
174 
175 
176 
177 
178 
179 
180 
181 
182 
183 
184 
185 
186 
187 
188 
189 
190 
191 
192 
193 
194 
195 
196 
197 
198 
199 
200 
201
202
203 
204 
205 
206 
207 
208 
209 
210 
211 
212 
213 
214 
215 
216 
217 
218 
219 
220 
221 
222 
223 
224 
225 
226 
227 
228 
229 
230 
231 
232 
233 
234 
235 
236 
237 
238 
239 
240 
241 
242 
243 
244 
245 
246 
247 
248 
249 
250 
261
262
263 
264 
265 
266 
267 
268 
269 
270 
271 
272 
273 
274 
275 
276 
277 
278 
279 
280 
281 
282 
283 
284 
285 
286 
287 
288 
289 
290 
291 
292 
293 


メインプログラム



メインプログラム
using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.NetduinoMini;
using BoeBotLib;

namespace compass
{
    public class Program
    {
        public static void Main()
        {
            new Program().main();
        }

        public void main()
        {
            String str = "Mizuno\n";
            I2CLCD lcd = new I2CLCD(8);
            lcd.write(str);
            compassHM55B cp = new compassHM55B(CPU.P7,CPU.P6,CPU.P8);
            cp.start(); // Start a measurement
            while (true)
            {
                if (cp.poll())
                {
                    //Console.WriteLine(cp.getCompassAngle());
                    lcd.write(cp.getCompassAngle(),4,false);
                    cp.start(); // Start a measurement
                    CPU.delay(10);
                    lcd.write((byte)0x0d);
                }
            }
        }
    }
}
 1 
 2 
 3 
 4 
 5 
 6 
 7 
 8 
 9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 小型LCDを利用する
22 
23 
24 
25 
26 
27 
28 
29 VisualStudioに出力する場合
30 
31 
32 
33 
34 
35 
36 
37 
38