Shop OBEX P1 Docs P2 Docs Learn Events
New Cog Spin Issue? — Parallax Forums

New Cog Spin Issue?

handymanhandyman Posts: 2
edited 2013-11-15 06:03 in Propeller 1
Hello All,
I am having a problem, that I cannot find the answer to, and would love it if someone could help. The issue is that as you can see in my code. I initialize the direction of a pin in my main procedure(running in cog 0), then at a later point in time, I initialize my motor controller object, which then sets the pin to high. What I have noticed is that this code does not work unless I define the direction of the pin somewhere in the code running on the new cog before setting it high. Is this a property I am not aware of with the propeller, or a bug in my code? By the way, the only functionality I am asking about is the led on pin 16 not lighting up when the MC object starts. I have verified it gets a cog successfully by the way(cog 2) Note that if I simply add DIRA[16]~~ before the OUTA[16]~~ in the RunMotors procedure it works, but I already set it to output in the Main Object, so I am wondering why that is not working. Thanks!!


Main Object:

{{
Name: Trevor Handermann
Abstract: Robot Main Control Class
}}

CON
'Clocking settings
_clkmode = xtal1 + pll16x
_xinfreq = 5_000_000

{
PINS
}
'LCD PINS
LCD_CLK = 0
LCD_MOSI = 1
LCD_DC = 2
LCD_CS = 3
LCD_RST = 4
LCD_BL = 5

'MOTOR CONTROLLER PINS
LEFT_ENCODER = 6
RIGHT_ENCODER = 7
LEFT_MOTOR_PWM = 8
LEFT_MOTOR_DIR = 9
RIGHT_MOTOR_PWM = 10
RIGHT_MOTOR_DIR = 11



'Sensor Pins
IR_REMOTE = 12


'Standard Output Pins
STATUS_LED = 16





VAR
byte key


OBJ

LCD : "GraphLCDTextOnly"
Utilities : "Utilities"
IR : "IR"
num : "Simple_Numbers"
MC : "R5MotorController"

PUB Main
{
Begin the robot program
}

InitRobot



'Main Control Loop
repeat
'Get IR Key
key := IR.GetKey
if key <> 32
LCD.PrintString(num.dec(key))
LCD.PrintString(string(" "))
case key
2:
'Move Straight Forward
MC.SetMotors(1,1,80,80)
5:
'Stop Motors
MC.SetMotors(1,1,0,0)
4:
'Rotate Left
MC.SetMotors(0,1,50,50)
6:
'Rotate Right
MC.SetMotors(1,0,50,50)
8:
'Move Staight Backward
MC.SetMotors(0,0,80,80)

IR.ClearKey
Utilities.DelayMS(30) 'Delay to avoid constant processing during button pressing
else
next


repeat
Utilities.DelayS(5)





PUB InitRobot | tmp
{
Perform startup proceedures
}
'Set Pin Dirs
DIRA[STATUS_LED]~~

'Start the lcd
LCD.Init(LCD_CLK, LCD_MOSI, LCD_DC, LCD_CS, LCD_RST, LCD_BL)

'Start IR in new cog
IR.StartIRMonitor(IR_REMOTE)

'Start Motor Controller
tmp := MC.Init(LEFT_ENCODER, RIGHT_ENCODER, LEFT_MOTOR_PWM, LEFT_MOTOR_DIR, RIGHT_MOTOR_PWM, RIGHT_MOTOR_DIR)





DAT





MC Object:


{{
Name: Trevor Handermann
Abstract: Dagu Rover 5 Motor Controller Class
}}

CON
'Clocking settings
_clkmode = xtal1 + pll16x
_xinfreq = 5_000_000

PWM_BASE_TIME = 100 'Time in microseconds that the PWM Keeps the motor on for each percent


VAR
long tstack[256]
byte leftenc, rightenc, leftpwm, leftdir, rightpwm, rightdir
byte lpwmpercent
byte rpwmpercent
byte cldir, crdir
byte cog
byte cogon

OBJ
Utilities : "Utilities"

PUB Init(leftencoder, rightencoder, leftmotorpwm, leftmotordir, rightmotorpwm, rightmotordir)

'Setup pins
leftenc := leftencoder
rightenc := rightencoder
leftpwm := leftmotorpwm
leftdir := leftmotordir
rightdir := rightmotordir
rightpwm := rightmotorpwm

'Set Pin Dirs
DIRA[leftenc]~
DIRA[rightenc]~
DIRA[leftpwm]~~
DIRA[rightpwm]~~
DIRA[leftdir]~~
DIRA[rightdir]~~


'Start motors at off, and forward
crdir := 1
cldir := 1
SetMotors(1, 1, 0, 0)

cog := cognew(RunMotors, @tstack) 'Init cog



return cog

PUB SetMotors(lftdir, rhtdir, lftpwmper, rhtpwmper) | rpwm, lpwm
lpwmpercent := lftpwmper
rpwmpercent := rhtpwmper
rpwm := 0
lpwm := 0


'Protect gears by disallowing immediate motor redirection
if cldir <> lftdir or crdir <> rhtdir
'If motor moving
if lpwmpercent > 0 or rpwmpercent > 0
'Stop Motors
rpwm := rpwmpercent
rpwmpercent := 0
lpwm := lpwmpercent
lpwmpercent := 0

'If either motor is changing direction, then wait for motors to stop, then set directions
if rpwm <> 0
Utilities.DelayMS(400)
OUTA[leftdir] := !lftdir 'Set dirs to inverse becuase motor control is hooked up reverse
OUTA[rightdir] := !rhtdir
cldir := lftdir
crdir := rhtdir
'Set new pwm's
lpwmpercent := lpwm
rpwmpercent := rpwm
rpwm := 0




PUB Stop
'Turn off cog
if cogon~
CogStop(cog)



PUB RunMotors

####Here is the issue, this does not light the led on pin 16#########
OUTA[16]~~


DIRA[leftpwm]~~
DIRA[rightpwm]~~

repeat
'Send pwm to motors
'LEFT MOTOR
if lpwmpercent <> 0 and lpwmpercent < 100
OUTA[leftpwm]~~ 'Turn on
Utilities.DelayUS(PWM_BASE_TIME*lpwmpercent) 'Keep on for multiple of percentage
OUTA[leftpwm]~ 'Turn off
Utilities.DelayUS(PWM_BASE_TIME*(100-lpwmpercent)) 'Keep off for multiple of 100-percentage
elseif lpwmpercent == 100
OUTA[leftpwm]~~ 'Full power!
elseif lpwmpercent == 0
OUTA[leftpwm]~ 'Motor Off
'RIGHT MOTOR
if rpwmpercent <> 0 and rpwmpercent < 100
OUTA[rightpwm]~~ 'Turn on
Utilities.DelayUS(PWM_BASE_TIME*rpwmpercent) 'Keep on for multiple of percentage
OUTA[rightpwm]~ 'Turn off
Utilities.DelayUS(PWM_BASE_TIME*(100-rpwmpercent)) 'Keep off for multiple of 100-percentage
elseif rpwmpercent == 100
OUTA[rightpwm]~~ 'Full power!
elseif rpwmpercent == 0
OUTA[rightpwm]~ 'Motor Off





DAT

Comments

  • Heater.Heater. Posts: 21,230
    edited 2013-11-15 05:24
    Is this a property I am not aware of with the propeller,
    It is a property of the Propeller that you are not aware of, hence it is a bug in your code.

    Each cog has it's own OUTA and DIRA registers.

    A COG cannot drive an output unless it's own DIRA is set to output.

    Do look at the Propeller IO architecture diagrams in the Propeller manual that make all this clear if you understand some logic operations (AND, OR, XOR...)

    If you don't understand those come back and ask.
  • handymanhandyman Posts: 2
    edited 2013-11-15 05:48
    Thanks very much for your input. I read it when I got my first propeller, but I guess I have since forgotten it. Most of my cog processes have not directly interacted with pins, so I guess I am just now finding out. Thank you!!
  • Heater.Heater. Posts: 21,230
    edited 2013-11-15 06:03
    My pleasure.

    Welcome to the forum and happy Propelling !
Sign In or Register to comment.