Telescope motorised focuser based on the moonlite protocol, indilib's indi_monlite_focuser compliant. WIP

Files at this revision

API Documentation at this revision

Comitter:
rakware
Date:
Sun Aug 05 09:19:46 2018 +0000
Parent:
7:30dcdf917330
Commit message:
using Ticker to set motor speed

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed-os.lib Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Wed Aug 01 18:26:56 2018 +0000
+++ b/main.cpp	Sun Aug 05 09:19:46 2018 +0000
@@ -8,20 +8,21 @@
 uint16_t gp = 0; //current position
 uint16_t temperature = 255; //hardcoded for now //TODO
 
-bool is_moving, move = false;
+bool move = false;
 
 uint16_t step_delay = 256;
 
 //minimum motor speed that will get doubled 5 times to result maximum motor speed
-//eg: for a value of 100 there will be 5 speeds as follows 100*1,100*2,100*4,100*8,100*16
-//eg: for a value of 100 there will be 5 speeds as follows 100,200,400,800,1600
-uint8_t min_motor_pps = 75;
+//eg: for a value of 16 there will be 5 speeds as follows 16*1,16*2,16*4,16*8,16*16
+//eg: for a value of 16 there will be 5 speeds as follows 16,32,64,128,256
+uint8_t min_motor_pps = 16; //TODO use max pps since it might be more intuitive?
 
 
 char ml_in[256];
 uint8_t idx = 0;
 
-Thread led_thread, motor_thread;
+Thread serial_thread, motor_thread;
+Ticker m_speed;
 
 void led_blink()
 {
@@ -42,7 +43,7 @@
 DigitalOut slp(D4); //pull high to enable operation
 DigitalOut i2(D5); //set low to enable pwm power control
 PwmOut i1(D6); //MP6500 power limit control
-DigitalOut ms2(D7); 
+DigitalOut ms2(D7); //D7 and D8 are PC_14 and PC_15 N/C oscillator pins, hence this is full step only //TODO
 DigitalOut ms1(D8); //set low for FULL or high for HALF, only use MS1 to be moonlite compliant ?
 DigitalOut en(D9); //pulled low by the motor driver, default enables operation
 
@@ -57,38 +58,34 @@
     slp = 1;
     i2 = 0;
     i1.period(0.001f); //1kHz period
-    i1.write(pw);
+    i1.write(1.0f); //start with lowest power since we're idle
     ms2 = 0;
     ms1 = 1;
     en = 0;
 }
 
 
-void motor()
+void m_step()
 {
 
-    while (true) {
+    //while (true) {
 
         if(move) {
             
             i1.write(pw); //set defined current motor while moving
             
-            is_moving = true;
-
             if(gp > gn) {
 
                 dir = 0;
                 wait_us(1);
-//__disable_irq();    // Disable Interrupts because serial irq makes motor move funky with indi_moonlite_focuser polling
-//__enable_irq();     // Enable Interrupts
                 //led = !led;
                 step = !step;
                 wait_us(1); //mp6500
                 //led = !led;
                 step = !step;
                 wait_us(1);
-                //https://os.mbed.com/docs/v5.9/mbed-os-api-doxy/mbed__wait__api_8h_source.html use Thread::Wait()?
-                wait((step_delay/1.024)/min_motor_pps/250);
+                //https://os.mbed.com/docs/v5.9/mbed-os-api-doxy/mbed__wait__api_8h_source.html use Thread::wait()?
+                //wait((step_delay/1.024)/min_motor_pps/250);
                 gp--;
 
             } else if (gp < gn) {
@@ -101,7 +98,7 @@
                 //led = !led;
                 step = !step;
                 wait_us(1);
-                wait((step_delay/1.024)/min_motor_pps/250);
+                //wait((step_delay/1.024)/min_motor_pps/250);
                 gp++;
 
             } else {
@@ -111,14 +108,13 @@
 
                 i1.write(1.0f); //set minimum controller power while holding, keeps motor cool and still provides a nice holding torque, ~120ma
 
-                is_moving = false;
                 move = false;
 
             }
 
         }
 
-    }
+    //}
 
 }
 
@@ -127,8 +123,8 @@
 //http://indilib.org/media/kunena/attachments/1/HighResSteppermotor107.pdf
 void read_serial()
 {
-
-    while(pc.readable()) {
+while(true){
+    if(pc.readable()) {
         char c = pc.getc();
 
         switch (c) {
@@ -166,7 +162,7 @@
                                 pc.printf("%02X#", ms1 * 255);
                                 break;
                             case 'I': //00# OR 01# Returns "00#" if the focus motor is not moving, otherwise return "01#"
-                                pc.printf("%02X#", is_moving);
+                                pc.printf("%02X#", move);
                                 break;
                             case 'N': //YYYY# Returns the new position previously set by a ":SNYYYY#" command where YYYY is a four-digit unsigned hex number
                                 pc.printf("%04X#", gn); //TODO use this to move the motor
@@ -192,6 +188,7 @@
                                 break; //TODO
                             case 'D': //N/A Set the new stepping delay where XX is a two-digit, unsigned hex number. Valid values to send are 02, 04, 08, 10 and 20, which correspond to a stepping delay of 250, 125, 63, 32 and 16 steps per second respectively.
                                 step_delay = 512 / strtol(ml_in + 2, NULL, 16);
+                                m_speed.attach(&m_step, (step_delay/1.024)/min_motor_pps/250);
                                 break;
 
                             case 'F': //N/A Set full-step mode.
@@ -245,7 +242,7 @@
 
 
     }
-
+}//while true
 }
 
 
@@ -253,14 +250,15 @@
 {
     mp6500();
 
-    //led_thread.start(led_blink);
-    pc.attach(read_serial, Serial::RxIrq);
+    //pc.attach(read_serial, Serial::RxIrq);
+    serial_thread.start(&read_serial);
 
-    motor_thread.start(motor);
+    //motor_thread.start(motor);
+    m_speed.attach(&m_step, (step_delay/1.024)/min_motor_pps/250);
+
 
     while(true) {
-        Thread::wait(100);
-        //read_serial();
+        Thread::wait(1);
     }
 
 }
--- a/mbed-os.lib	Wed Aug 01 18:26:56 2018 +0000
+++ b/mbed-os.lib	Sun Aug 05 09:19:46 2018 +0000
@@ -1,1 +1,1 @@
-https://github.com/ARMmbed/mbed-os/#50bd61a4a72332baa6b1bac6caccb44dc5423309
+https://github.com/ARMmbed/mbed-os/#485bdeee150e2bc8ed75e27d936060fb63a7a7d1