Newbie_Why didn't this work
Brianmcm
Posts: 33
Need some guidance. I came across a servo control code called "Single_Servo_Spin", by Gavin Garner·that I used to experiment with some servos. Thanks to Gavin for the code, it has been very helpful. I've just recently began to learn "Spin"·programming using the Propeller Education Kit.
I·made a modification to the code to see if·I could get two servos to operate simultaneously. I realize there are "objects"·already available in the·Exchange Library that allow the control·multiple·servos, but this is more of learning exercise in COG address, etc..
Below is my "modified" code. As you can see the intent is for·servo-1 on Pin 13 and·servo-2 on Pin 15 to go through the different positions at the same time. Presently as it is written when I run it, it skips the first "cognew" and operates the servo on Pin 15. If I swap pin assigments and run it again, servo on Pin-13 operates.
Is the problem a simple code·error or am I way·off base trying to do it this way.
CON
· _xinfreq=5_000_000···········
· _clkmode=xtal1+pll16x·······
VAR
· long position···············
······························
· long Stack[noparse][[/noparse]30]
······························
·
PUB Demo
··································
· cognew(Motor (13), @Stack[noparse][[/noparse]0])
· cognew(Motor (15), @Stack[noparse][[/noparse]15])·········
········································
· repeat················································································································
··· 'position:=100·············································································
··· 'waitcnt(clkfreq+cnt)·················································································
··· position:=138···········································································
··· waitcnt(clkfreq+cnt)···················································································
··· position:=50············································································
··· waitcnt(clkfreq+cnt)····················
··· position:=225···········································································
··· waitcnt(clkfreq+cnt)····················
························································································································
PUB Motor(Pin)
······································
· dira[noparse][[/noparse]Pin]~~···················
· repeat······························
··· outa[noparse][[/noparse]Pin]~~···························
··· waitcnt((clkfreq/100_000)*position+cnt)····
··· outa[noparse][[/noparse]Pin]~···························
··· waitcnt(clkfreq/100+cnt)
Thank you for your guidance.
Brian
·····
I·made a modification to the code to see if·I could get two servos to operate simultaneously. I realize there are "objects"·already available in the·Exchange Library that allow the control·multiple·servos, but this is more of learning exercise in COG address, etc..
Below is my "modified" code. As you can see the intent is for·servo-1 on Pin 13 and·servo-2 on Pin 15 to go through the different positions at the same time. Presently as it is written when I run it, it skips the first "cognew" and operates the servo on Pin 15. If I swap pin assigments and run it again, servo on Pin-13 operates.
Is the problem a simple code·error or am I way·off base trying to do it this way.
CON
· _xinfreq=5_000_000···········
· _clkmode=xtal1+pll16x·······
VAR
· long position···············
······························
· long Stack[noparse][[/noparse]30]
······························
·
PUB Demo
··································
· cognew(Motor (13), @Stack[noparse][[/noparse]0])
· cognew(Motor (15), @Stack[noparse][[/noparse]15])·········
········································
· repeat················································································································
··· 'position:=100·············································································
··· 'waitcnt(clkfreq+cnt)·················································································
··· position:=138···········································································
··· waitcnt(clkfreq+cnt)···················································································
··· position:=50············································································
··· waitcnt(clkfreq+cnt)····················
··· position:=225···········································································
··· waitcnt(clkfreq+cnt)····················
························································································································
PUB Motor(Pin)
······································
· dira[noparse][[/noparse]Pin]~~···················
· repeat······························
··· outa[noparse][[/noparse]Pin]~~···························
··· waitcnt((clkfreq/100_000)*position+cnt)····
··· outa[noparse][[/noparse]Pin]~···························
··· waitcnt(clkfreq/100+cnt)
Thank you for your guidance.
Brian
·····
Comments
So the memory you set aside to run is actually being over written by the second cog new command.
Try this.
but be wary of using very small stacks, the minimum is about 100 longs, otherwise cogs run into each other with very weird results.
Hope this helps,
TJ
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Cardinal Fang! Fetch the comfy chair.
Not according to the generated code
You can see it properly calculates the address for each instance. (21/22) & (2B/2D)
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Cardinal Fang! Fetch the comfy chair.
could you explain in detail what you coded ?
It seems to be just a code-snippet.
Does it only work in assembler ?
best regards
Stefan
G'day Stefan,
It's not assembler as such, it's the original spin source line followed by a disassembly of the bytecodes the compiler generates for that source line.
I did it only for these two source lines isolated to specifically prove that cognew will use the correct pointer given to it for stack data.
Here is the entire listing for the program Brianmcm posted.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Cardinal Fang! Fetch the comfy chair.