{{ This program first checks for a byte received from the handbox controller If a byte is present, the slew rate is extracted from it (a value from 0 to 3) If the byte was sent by the North or South button being pressed, the declination motor is driven in the appropriate direction at a speed of slew_rate x sidereal_rate If no byte was received, or the received byte doesn't pertain to the declination motor, then the autoguider is checked If the autoguider directs movement North or South, the declination motor is driven in the appropriate direction at 1x sidereal rate Otherwise, the declination motor remains unpowered. If a received byte was sent by the West or East button being pressed, the right-ascension motor is driven in the appropriate direction at a speed of slew_rate x sidereal_rate + sidereal rate If no byte was received, or the received byte doesn't pertain to the R.A. motor, then the autoguider is checked If the autoguider directs movement West or East, the R.A. motor is driven in the appropriate direction at 1x sidereal rate + sidereal rate Otherwise, the R.A. motor is driven Westward at 1x sidereal rate }} CON ' DON'T TOUCH! _xinfreq = 5_000_000 ' using 5MHz xtal _clkmode = xtal1 + pll16x RA = 0 DEC = 1 NORTH = %0001 SOUTH = %0010 WEST = %0100 EAST = %1000 CON ' Modify as needed 'PIN ASSIGNMENTS AUTO_N = 4 ' Autoguider North AUTO_S = 5 ' Autoguider South AUTO_W = 6 ' Autoguider West AUTO_E = 7 ' Autoguider East RA_SPEED = 16 ' RA motor enable RA_DIR = 17 ' RA motor phase DEC_SPEED = 18 ' Dec motor enable DEC_DIR = 19 ' Dec motor phase TX = 3 ' Transmit to handbox RX = 2 ' Receive from handbox PWM_FREQ = 50 ' 50hz frequency : 50% dutycycle DEC_RATE = 8_0 ' DEC base rate = 7.5% - Adjust as needed for desired slew rate DEC_AUTO = DEC_RATE ' DEC autoguider correction rate - currently set to base rate RA_RATE = 8_0 ' RA base rate = 9.5 Hz - Adjust as needed to counteract Earth's rotation RA_AUTO = RA_RATE ' RA autoguider correction rate - currently set to base rate SLEW_1 = 2 ' Slowest slew rate from handbox. These values are multiplied by the sidereal rate SLEW_2 = 4 SLEW_3 = 6 SLEW_4 = 8 ' Fastest slew rate from handbox MINVAL = "0" MAXVAL = "o" TIMEOUT = 80_000 * 300 ' 300 msec timeout on receive serial port VAR long rx_print ' Flag used to print received character or time out long auto_print ' Flag used to print auto state long debug_stack[200] OBJ step1 : "stepper" step2 : "stepper" ' stepper motor driver ser : "FullDuplexSerial" ' used for communicating with the handbox fds : "FullDuplexSerial" ' used for debugging PUB Main | rx_old, rx_new, update, rx_active, slew_rate, rx_time, auto_new, auto_old, rx_byte rx_old := 0 auto_old := 0 update := 0 rx_active := 0 step1.start(RA_SPEED, RA_DIR) step2.start(DEC_SPEED, DEC_DIR) ser.start(RX,TX,0,9600) cognew(debug,@debug_stack) waitcnt(CNT+80000) step1.set_speed(RA_RATE) step2.set_speed(DEC_RATE) '>>>>>>>>>>>>>>>>>> cognew(tracer, @stack2) repeat ' Check the receive serial port rx_byte := ser.rxcheck ' Read a byte from the handbox if rx_byte < MINVAL or rx_byte > MAXVAL rx_byte := fds.rxcheck ' Read a byte from the debug console if rx_byte == "t" dump_trace := 1 if rx_byte => MINVAL and rx_byte =< MAXVAL rx_new := rx_byte rx_time := CNT rx_active := 1 rx_print := rx_new if rx_new <> rx_old rx_old := rx_new update := 1 elseif rx_active AND (CNT - rx_time) > TIMEOUT rx_active := 0 update := 1 rx_new := MINVAL rx_old := 0 rx_print := -1 ' Check for new auto setting auto_new := ina[AUTO_E..AUTO_N] if auto_new <> auto_old auto_print := auto_new auto_old := auto_new update := 1 ' Update motor control if their is a change if update update := 0 rx_byte := rx_new - MINVAL slew_rate := lookupz(rx_byte >> 4:SLEW_1,SLEW_2,SLEW_3,SLEW_4) ' read the slew rate from the upper nibble if rx_byte & NORTH ' NORTH handbox button pressed step2.set_speed(Dec_rate * slew_rate) ' Set the DEC motor speed to new rate elseif rx_byte & SOUTH ' SOUTH handbox button pressed step2.set_speed(-DEC_RATE * slew_rate) elseif auto_new & NORTH ' If the handbox is not controlling the DEC motor, check the autogui step2.set_speed(DEC_AUTO) elseif auto_new & SOUTH step2.set_speed(-DEC_AUTO) else step2.set_speed(DEC_RATE) '>>>>>>>>>>>>>>>>> If neither the handbox nor autoguider is controlling the DEC motor, turn it off if rx_byte & WEST ' WEST handbox button pressed step1.set_speed(RA_RATE * slew_rate + RA_RATE) ' Set the RA motor speed to new rate elseif rx_byte & EAST ' EAST handbox button pressed step1.set_speed(-RA_RATE * slew_rate + RA_RATE) elseif auto_new & WEST ' If the handbox is not controlling the RA motor, check the autoguider step1.set_speed(RA_AUTO + RA_RATE) ' slew West, with the sidereal rate elseif auto_new & EAST step1.set_speed(-RA_AUTO - RA_RATE) ' slew East, against the sidereal rate else step1.set_speed(RA_RATE) ' If neither the handbox nor autoguider is controlling the RA motor, set to SIDE_RATE PRI debug | count, seconds, auto_last, rx_print1, auto_print1, cycles1, cycles2, cycles3, pflag auto_last := 0 seconds := 0 count := cnt + clkfreq * 10 fds.start(31,30,0,115200) repeat pflag := rx_print ' Get local copy of rx_print rx_print1 := rx_print if (rx_print1) rx_print := 0 ' Print elapsed time every 10 seconds if cnt - count > 0 count += clkfreq * 10 fds.dec(seconds += 10) fds.str(string(" Seconds", 13)) auto_last := -1 ' Print data if received, or time out if no data for 300 msec if rx_print1 == -1 fds.str(string("Serial time out", 13)) elseif rx_print1 fds.tx(rx_print1) rx_print1 -= MINVAL fds.tx(" ") fds.dec(rx_print1 >> 4) fds.tx(" ") if rx_print1 & NORTH fds.str(string("North ")) if rx_print1 & SOUTH fds.str(string("South ")) if rx_print1 & WEST fds.str(string("West ")) if rx_print1 & EAST fds.str(string("East ")) fds.tx($0D) ' Print state of auto pins if changed auto_print1 := auto_print if auto_print1 <> auto_last fds.str(string("AUTO ")) ifnot auto_print1 fds.str(string("None ")) if auto_print1 & NORTH fds.str(string("North ")) if auto_print1 & SOUTH fds.str(string("South ")) if auto_print1 & WEST fds.str(string("West ")) if auto_print1 & EAST fds.str(string("East ")) fds.tx($0D) auto_last := auto_print1 if pflag pflag := 0 repeat while ina[RA_SPEED] repeat while not ina[RA_SPEED] cycles1 := CNT repeat while ina[RA_SPEED] cycles2 := cnt repeat while not ina[RA_SPEED] cycles3 := cnt { fds.dec(cycles2 - cycles1) fds.tx(" ") fds.dec(cycles3 - cycles2) fds.tx(13) } fds.dec(clkfreq/(cycles3 - cycles1)) fds.tx(" ") fds.dec(100*(cycles2 - cycles1)/(cycles3 - cycles1)) fds.tx(13) if dump_trace DumpTrace con TRACE_SIZE = 6000 var long stack2[200] long trace[TRACE_SIZE] long tnum long dump_trace pub DumpTrace | index, base dump_trace := 0 base := trace[0] repeat while index < tnum fds.dec(trace[index++] - base) fds.tx(13) fds.tx(10) pub tracer | lastval lastval := 1 repeat while tnum < TRACE_SIZE if ina[RA_SPEED] <> lastval trace[tnum++] := cnt lastval ^= 1