{{ 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 = 13 ' RA motor enable RA_DIR = 12 ' RA motor phase DEC_SPEED = 15 ' Dec motor enable DEC_DIR = 14 ' Dec motor phase TX = 10 ' Transmit to handbox RX = 11 ' Receive from handbox PWM_FREQ = 50 ' 50Hz PWM frequency = 20ms pulse width SIDE_RATE = 7_5 ' Sidereal tracking rate 7.5% = 1.5ms out of 20ms pulse width ' Adjust as needed to counteract Earth's rotation AUTO_RATE = 7_5 ' Autoguider correction rate - currently set to same 7.5% value as the sidereal 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 motor : "dual_motor_ez_MOD" ' dual 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 motor.start(-1, RA_SPEED, RA_DIR, -1, DEC_SPEED, DEC_DIR, PWM_FREQ) ' Start the motor driver and assign pins, 50Hz PWM frequency ser.start(RX,TX,0,9600) cognew(debug,@debug_stack) waitcnt(CNT+80000) motor.set_speed(RA,SIDE_RATE) 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 => 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 motor.set_speed(DEC,SIDE_RATE * slew_rate) ' Set the DEC motor speed to new rate elseif rx_byte & SOUTH ' SOUTH handbox button pressed motor.set_speed(DEC,-SIDE_RATE * slew_rate) elseif auto_new & NORTH ' If the handbox is not controlling the DEC motor, check the autoguider motor.set_speed(DEC,AUTO_RATE) elseif auto_new & SOUTH motor.set_speed(DEC,-AUTO_RATE) else motor.set_speed(DEC,0) ' If neither the handbox nor autoguider is controlling the DEC motor, turn it off if rx_byte & WEST ' WEST handbox button pressed motor.set_speed(RA,SIDE_RATE * slew_rate + SIDE_RATE) ' Set the RA motor speed to new rate elseif rx_byte & EAST ' EAST handbox button pressed motor.set_speed(RA,-SIDE_RATE * slew_rate + SIDE_RATE) elseif auto_new & WEST ' If the handbox is not controlling the RA motor, check the autoguider motor.set_speed(RA,AUTO_RATE + SIDE_RATE) ' slew West, with the sidereal rate elseif auto_new & EAST motor.set_speed(RA,-AUTO_RATE + SIDE_RATE) ' slew East, against the sidereal rate else motor.set_speed(RA,SIDE_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 auto_last := 0 seconds := 0 count := cnt + clkfreq * 10 fds.start(31,30,0,115200) repeat ' 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