pythonpython-3.xraspberry-pi

Python Return Invalid Syntax Error


I am writing a simple python program on a raspberry pi and I am quite new to python programming. I have defined a function called GetMessage which has no parameters and returns a variable which I called data, but I am getting an error which states

File "Raspberry_pi.py", line 39

return none

    ^

SyntaxError: invalid syntax

import os
import glob
import time
import RPi.GPIO as GPIO
from math import *
from bluetooth import *
from RPIO import PWM

os.system('sudo hciconfig hci0 pisca')
os.system('sudo hciconfig hci0 name "De Quadcoptur"')
servo = PWM.Servo()
StartSpin()

server_sock=BluetoothSocket( RFCOMM )
server_sock.bind(("",PORT_ANY))
server_sock.listen(1)

port = server_sock.getsockname()[1]

GetMessage()
DecodeInput()

uuid = "94f39d29-7d6d-437d-973b-fba39e49d4ee"
def GetMessage():
    advertise_service( server_sock, "XT1032", #phone bluetooth name
        service_id = uuid,
        service_classes = [ uuid, SERIAL_PORT_CLASS ],
        profiles = [ SERIAL_PORT_PROFILE ],
        #protocols = [ OBEX_UUID ]
        )
    client_sock, client_info = server_sock.accept()

    try:
            data = client_sock.recv(1024)
            if len(data) == 0: break
            print "received [%s]" % data
            client_sock.close()
            server_sock.close()
    except IOError:
        pass

        break
    return data


def StartSpin():
    # Set servo on GPIO17 to 1200µs (1.2ms)
    servo.set_servo(17, 1000)
    servo.set_servo(18, 1000)
    servo.set_servo(19, 1000)
    servo.set_servo(20, 1000)
    time.sleep(1)
    servo.stop_servo(17)
    servo.stop_servo(18)
    servo.stop_servo(19)
    servo.stop_servo(20)
    #Check if more pulses is faster
    time.sleep(2000)
    PWM.add_channel_pulse(0, 17, start = 1000, width = 100)
    PWM.add_channel_pulse(0, 17, start = 1000, width = 100)
    PWM.add_channel_pulse(0, 17, start = 1000, width = 100)
    PWM.add_channel_pulse(0, 17, start = 1000, width = 100)
    PWM.add_channel_pulse(0, 17, start = 1000, width = 100)
    servo.stop_servo(17)
    servo.stop_servo(18)
    servo.stop_servo(19)
    servo.stop_servo(20)
    return None

def DecodeInput():
    data = GetMessage()
    if(data == 'start')
        StartSpin()
        return 0
    else if(data[0] == 'U')
        data.strip('U')
        UpPower = int(data)
        SetUpPower(UpPower)
    else if(data[0] == 'P')
        data.strip('P')
        PitchPower = int(data)
        SetPitchPower
    else
        data.strip('P')
        RollPower = int(data)
    SetPower(UpPower, PitchPower, RollPower)
    return None

def SetPower(UpPower, PitchPower, RollPower):
    #Make Arbitrary Values
    Motor1Power = UpPower #Front Left
    Motor2Power = UpPower #Front Right
    Motor3Power = UpPower #Back  Left
    Motor4Power = UpPower #Back  Right
    PitchPower = PitchPower /2
    RollPower = RollPower /2
    if(PitchPower < 25)
        Motor1Power = Motor1Power + abs(25-PitchPower)
        Motor2Power = Motor1Power + abs(25-PitchPower)
    else
        Motor3Power = Motor3Power + (PitchPower-25)
        Motor4Power = Motor4Power + (PitchPower-25)
    if(RollPower < 25)
        Motor1Power = Motor1Power + abs(25-RollPower)
        Motor3Power = Motor3Power + abs(25-RollPower)
    else
        Motor2Power = Motor2Power + (RollPower - 25)
        Motor4Power = Motor4Power + (RollPower - 25)

What is causing this error and how can I fix it?

Edit: I have defined data as a global variable and the error now is

File "Raspberry_pi.py", line 39

return data

    ^

SyntaxError: invalid syntax


Solution

  • There are a number of syntax problems in your code. Because of the nature of SyntaxError exceptions (which are raised when the interpreter doesn't understand the code syntax), the error messages may not identify the right line as the source of the problem.

    The first syntax error I see is that you're using break in the GetMessage function without it being in a loop. A break statement is only useful within a for or while block, and using one elsewhere (in an except block in this case) is a syntax error.

    The next set of errors have to do with missing colons. Each of the conditional branches in DecodeInput and SetPower need to have a colon after the condition: if condition1:, elif condition2:, else:

    It's also an error to use else if rather than elif (you could make it work if you added a colon, a newline and an extra level of indentation after else:, then used a separate if statement, but that would be wasteful of space).

    There are some additional issues, but they're not syntax errors. For instance, you're calling your functions from top-level code before they've been defined, and DecodeInput has a line with the bare expression SetPower which doesn't do anything useful (you probably want to call SetPower with some argument).

    Hopefully this will get you on the right track.