Search Unity

access static functions from threads

Discussion in 'Scripting' started by overthrowrobotics, Aug 1, 2015.

  1. overthrowrobotics

    overthrowrobotics

    Joined:
    Jun 19, 2015
    Posts:
    47
    I'm doing some gesture detection with a Kinect camera and then passing the gesture name into this script.

    Code (csharp):
    1.  
    2. public class databaseMoves : MonoBehaviour {
    3.  
    4.     public SimpleSQLManager dbManager;
    5.  
    6.  
    7.     public void Gesture_Move(string gestureName)
    8.     {
    9.         print ("Gesture_Move" + gestureName);
    10.         Thread threadMove = new Thread (() => Gesture_Move_Thread (gestureName));
    11.        
    12.         threadMove.Start ();
    13.  
    14.     }
    15.  
    16.     void Gesture_Move_Thread (string gestureName)
    17.     {
    18.        
    19.         print ("Gesture_Move_Thread");
    20.         string sql = "select sequence.delay, moves.start, moves.steps, moves.direction, moves.motor, moves.speed from moves ";
    21.         sql = sql + "inner join sequence on gesture_sequence.sequence_id = sequence.id ";
    22.         sql = sql + "inner join gesture_sequence on moves.id = sequence.move_id ";
    23.         sql = sql + "where gesture_sequence.name = '" + (string) gestureName + "' ";
    24.         sql = sql + "order by sequence.sequence ASC";
    25.        
    26.         print (sql);
    27.        
    28.         List<Moves> motor_moves = dbManager.Query<Moves>(sql);
    29.         foreach (Moves move in motor_moves)
    30.         {
    31.             print(move.start);
    32.             print (move.steps);
    33.             print (move.direction);
    34.             print (move.motor);
    35.             print (move.speed);
    36.             print (move.delay);
    37.            
    38.  
    39.             switch(move.motor)
    40.             {
    41.             case 1:
    42.                 print ("motor 1");
    43.                 motorcontroller.motor1Position ((uint)3200, 1024);
    44.                 //motorcontroller.motor1Position ((uint)move.speed, motorcontroller.targetMotor1 (move.steps, move.direction));
    45.                 if (move.delay > 0)
    46.                 {
    47.                 }
    48.                 break;
    49.  
    50.             case 2:
    51.                 print ("motor 1");
    52.                 motorcontroller.motor2Position ((uint)move.speed, motorcontroller.targetMotor2 (move.steps, move.direction));
    53.                 if (move.delay > 0)
    54.                 {
    55.                 }
    56.                 break;
    57.                
    58.             case 3:
    59.                 print ("motor 1");
    60.                 motorcontroller.motor3Position ((uint)move.speed, motorcontroller.targetMotor3 (move.steps, move.direction));
    61.                 if (move.delay > 0)
    62.                 {
    63.                 }
    64.                 break;
    65.             }
    66.         }
    67.     }
    68.  
    69. }
    I then launch a new thread and do a database connection using sqlite and I'm able to see the values in the print(move.*) lines without issue.

    move.motor = 1 so case 1 is run and I see the print ("motor 1") command. For some reason motorcontroller.motor1Position is not running.

    In another script called motorcontroller.cs I have this function:
    Code (csharp):
    1.  
    2. public static void motor1Position(uint speed, int position) {
    3.         print ("motor1position");
    4.         newSpeed1 = speed;
    5.         newMotorPosition1 = position;
    6.  
    7.         speedChange1 = true;
    8.         positionChange1 = true;
    9.     }
    print("motor1position") never shows up in the console so I'm assuming the function isn't being run. Ideas?
     
  2. overthrowrobotics

    overthrowrobotics

    Joined:
    Jun 19, 2015
    Posts:
    47
    Not really sure what I did, but it's fixed!