View Full Version : Having trouble adding a new object to an existing program and run it in a new cog

11-13-2011, 09:17 PM
I am very new at prop programming. I have created a program the will read the Parallax "5 position switch" and display the switch positions on a 2 X 16 Parallax LCD.(5PositionSwitch_LCD.spin) I ultimately want to control a servo with the switch.I found a Servo control object in the OBEX library called"Single Servo Spin". I have tried to incorporated it into my existing program but am not having luck. The program will compile and load into RAM but the LCD never initializes and no response from the switch or the servo.
If someone could look at my programs and give me a hint it would be greatly appreciated.Starting the Single Servo Spin in a new cog has got me confused and I suspect that is where the problem lies.


11-13-2011, 10:17 PM
I would try to just get the servo working first. Try this object with a 20 msec cycle time instead of 10msec.

VAR long servoBuff[10]
long servoPos
long servoPin
long cog

_clkmode = xtal1 + pll16x ' System clock ? 80 MHz
_xinfreq = 5_000_000 ' Using 5 MHz external crystal oscillator

' Servo signal to this I/O pin-change if needed

PUB Start(servo_Pin)

servoPin := servo_pin
cog := cognew(Servo, @servoBuff) + 1

PUB ServoChange(servo_Pos)

servoPos := 400 + 18 * servo_Pos ' scale servo between .4 to 2.2 milliseconds

PUB Servo | tInc,tHa, tc, t

ctra[30..26] := %00100 ' Configure Counter A to NCO
ctra[8..0] := servoPin

frqa := 1 ' add 1 to PHSA every clock cycle

' Set up cycle and high times
tInc := clkfreq/1_000_000 ' clock cycles in 1 microsecond

servoPos := 1500 ' 1500 microseconds = 90 degree position to start

t := cnt ' Mark counter time

repeat ' Repeat PWM signal
tHa := tInc * servoPos ' update the pulse on time in clock cycles
tC := tInc * 20_000 + tHa ' update the required interval in clock cycles
phsa := -tHa ' Set up the pulse
t += tC ' Calculate next cycle repeat
waitcnt(t) ' Wait for next cycle

11-13-2011, 11:20 PM
If someone could look at my programs and give me a hint it would be greatly appreciated.Starting the Single Servo Spin in a new cog has got me confused and I suspect that is where the problem lies.
Two things which stand out. One, the stack is too small. The MoveMotor method needs 7 longs in its current state (check the Stack Length object from the PropTool library). Which suggests that it interferes with other parts of the code. The second issue is that position is initially 0. After the motor cog is started it's at least a second before this variable is set to anything useful. This means the initial waitcnt in the MoveMotor method will be a waitcnt(cnt) which is effectively too late, i.e. you'll have to wait for a counter wrap-around (about a minute; this is just the way waitcnt works).

So I'd suggest increasing the stack (e.g. stack[32], you can always make it smaller later). And before you start the motor cog, give position a meaningful value.

11-14-2011, 12:16 AM
Thank you for the reply. I did get the servo to run by itself.

11-14-2011, 12:21 AM
Thank you for you suggestions. I will increase the stack and initialize the position to a value within the range given in the single servo spin program.

11-16-2011, 11:47 PM
Still not having success. I did increase the stack and initialized the servo position. Upon compile and download into RAM, the servo turns to the initialized position but the LCD never initialized and the switch apparently is not inputting to the Prop. If I comment out the cognew(MoveMotor(0),@Stack) line the LCD and switch work just fine. I guess Im going to have to try another servo object.

11-16-2011, 11:57 PM
What if you start an empty method with the cognew? E.g.

cognew(empty, @stack{0})

PRI empty


does the LCD still refuse to initialise? Also, could you do another test and keep the motor in the cognew but do something simple afterwards, like blinking an LED? For clarity, could you also attach your current code (preferably archived)?

11-19-2011, 01:23 PM

I "think" I may have found the problem. I noticed that if I did not plug the servo motor into the board that the LCD would go ahead and initialize. I increased the size of the power supply from 9.0v - 300ma to 9.0v - 500ma and the program appears to run now. There seems to still be a timing issue between the LCD displaying the switch position (doesn't always display) and the movement of the servo, but the servo is now moving in concert with the switch..
I appreciate your reply and help, it encouraged me to pursue a solution to the problem