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.
Fork of mbed_AS5047P_driver by
Revision 1:d68c51a0a706, committed 2016-09-28
- Comitter:
- dhaldane
- Date:
- Wed Sep 28 16:59:46 2016 +0000
- Parent:
- 0:00c56a9c6dee
- Commit message:
- Initial commit;
Changed in this revision
--- a/enc.cpp Sat Mar 05 00:09:45 2016 +0000
+++ b/enc.cpp Wed Sep 28 16:59:46 2016 +0000
@@ -1,27 +1,17 @@
#include "enc.h"
enc::enc(PinName mosi, PinName miso, PinName sck, PinName cs) : m_spi(mosi, miso, sck), csl(cs){
- ushort zpos = 2228; // est. 8/6/2015 1773
- ushort zhold;
- m_spi.format(16, 1);
- m_spi.frequency(50000);
+ set_offset(2134); // est. 9/13/2016
csl=1;
+ m_spi.format(8, 1);
+ m_spi.frequency(1000000);
wait(0.05);
-
write_spi(0x0019, 0); //SETTINGS2
write_spi(0x001D, 0);
write_spi(0x0018, 0); //SETTINGS1
write_spi(0x0018, 0);
-
- zhold = zpos >> 6; //8MSB of zero position
- write_spi(0x0016, 0);
- write_spi(zhold, 0);
-
- zhold = zpos & 0x003F; //6 LSB of zero position
- write_spi(0x0017, 0);
- write_spi(zhold, 0);
-
+
enc_pos.pos = 0;
enc_pos.oticks = 0;
enc_pos.offset = 0;
@@ -36,18 +26,30 @@
return enc_pos.pos;
}
-void enc::set_offset(){
- enc_pos.offset = ams_read();
+void enc::set_offset(ushort zpos){
+ ushort zhold;
+ zhold = zpos >> 6; //8MSB of zero position
+ write_spi(0x0016, 0);
+ write_spi(zhold, 0);
+
+ zhold = zpos & 0x003F; //6 LSB of zero position
+ write_spi(0x0017, 0);
+ write_spi(zhold, 0);
}
ushort enc::write_spi(ushort reg, u8 rw){
ushort data;
reg = reg | rw << 14;
reg = reg | par(reg);
+ char d1 = reg >> 8;
+ char d2 = reg & 0x00FF;
csl=0;
- data = m_spi.write(reg);
+ d1 = m_spi.write(d1);
+ d2 = m_spi.write(d2);
+
csl=1;
- wait(0.00001);
+ wait(0.0001);
+ data = (d1 << 8) | (d2 & 0xFF);
return data;
}
@@ -102,4 +104,4 @@
read_spi(0x0001);
return enc_data & 0x3FFF;
-}
\ No newline at end of file
+}
--- a/enc.h Sat Mar 05 00:09:45 2016 +0000
+++ b/enc.h Wed Sep 28 16:59:46 2016 +0000
@@ -20,7 +20,7 @@
~enc();
unsigned int ams_read();
void update_pos();
- void set_offset();
+ void set_offset(ushort zpos);
unsigned int cal_state(); //Calibrated stat
static ushort par(ushort value);
ushort write_spi(ushort reg, u8 rw);
--- a/main.cpp Sat Mar 05 00:09:45 2016 +0000
+++ b/main.cpp Wed Sep 28 16:59:46 2016 +0000
@@ -1,23 +1,38 @@
#include "mbed.h"
#include "enc.h"
-Ticker RTI;
-DigitalOut myled(LED1);
-Serial pc(USBTX, USBRX); // tx, rx
-enc crankPos(p5,p6,p7,p8);
+//Ticker RTI;
+Serial imProc(PTD7,PTD6);
+DigitalOut led_1(PTC2);
+DigitalOut led_2(PTC3);
+DigitalOut led_3(PTA18);
+enc motPos(PTC6,PTC7,PTC5,PTC4);
+PwmOut EN(PTA4);
+DigitalOut DR(PTA2);
+DigitalOut BRAKE(PTA1);
+AnalogIn motCurrent(PTB0);
+AnalogIn temp(PTE30);
void get_state(){
- crankPos.update_pos();
+ motPos.update_pos();
}
-int main() {
- RTI.attach(&get_state, 0.01);
+int main() {
+ EN.period_us(50);
+ EN.write(0.9f);
+ DR.write(0);
+ BRAKE.write(1);
+ motPos.set_offset(2160);
+ led_1 = 1;
+// RTI.attach(&get_state, 0.01f);
while(1) {
- myled = 1;
- wait(0.05);
- pc.printf("Encoder: %u \r\n", crankPos.cal_state());
- myled = 0;
- wait(0.05);
+
+ led_2 = 1;
+ led_3 = 0;
+ wait(0.5);
+ led_2 = 0;
+ led_3 = 1;
+ wait(0.5);
}
}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-src.lib Wed Sep 28 16:59:46 2016 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed-src/#a11c0372f0ba
--- a/mbed.bld Sat Mar 05 00:09:45 2016 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -http://mbed.org/users/mbed_official/code/mbed/builds/7e07b6fb45cf \ No newline at end of file
