emsity
emsity
Reputation Top 1%
emsity
13 Snippets  (65th place)
Published
1 Channel
Created
2 Channels
Following
304 points  (100th place)
Reputation
Top 10% Publisher
Junior Code Generator
Serious Code Generator
Junior Publisher
Serious Publisher
Junior Topic Hub
Junior Trend Maker
Serious Trend Maker
Junior Judge
Junior Popular Coder
Senior Popular Coder
Junior Autobiographer
Serious Autobiographer
Senior Famous Coder
Junior Wise Coder

Recent Snippets See all snippets by emsity

public by emsity  2566  2  6  2

PHP to read the robot arm MySql and to run the arm Python Script

Reads arm table and can pass the data to Python

<?php
// Create Connection
$con=mysqli_connect("localhost","my_name","my_password","action");

//Check connection
if (mysqli_connect_errno()) {
  echo "failed to connect to mysql: " . mysqli_connect_error();
}

// escape variables for security
$id = mysqli_real_escape_string($con, $_POST['id']);
$duration = mysqli_real_escape_string($con, $_POST['duration']);
$direction = mysqli_real_escape_string($con, $_POST['direction']);
$status = mysqli_real_escape_string($con, $_POST['status']);

$result = mysqli_query($con,"SELECT * FROM arm where status='0'");

echo "<table border='1'>
<tr>
<th>_Id_</th>
<th>Duration</th>
<th>Direction</th>
<th>Status</th>
</tr>";

while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['duration'] . "</td>";
echo "<td>" . $row['direction'] . "</td>";
echo "<td>" . $row['status'] . "</td>";
echo "</tr>";
}
echo "</table>";

mysqli_close($con);

?>

<htlm>
<form action="/cgi-bin/run_mymove.php" method="get">
Run:<input type="hidden" name="run">
<input type="submit">
</form>
</html>
;

public by emsity  2053  1  6  1

PHP script to insert data into robot arm MySql

Passes Direction and Duration to a MySql table (arm) for Python to read
<?php
// Create Connection
$con=mysqli_connect("localhost","my_name","my_password","action");

//Check connection
if (mysqli_connect_errno()) {
  echo "failed to connect to mysql: " . mysqli_connect_error();
}

// escape variables for security
$id = mysqli_real_escape_string($con, $_POST['id']);
$duration = mysqli_real_escape_string($con, $_POST['duration']);
$direction = mysqli_real_escape_string($con, $_POST['direction']);
$status = mysqli_real_escape_string($con, $_POST['status']);

$sql="INSERT INTO arm (id, duration, direction, status)
VALUES ('$id', '$duration', '$direction', '$status')";

if (!mysqli_query($con,$sql)) {
  die('Error: ' . mysqli_error($con));
}

mysqli_close($con);	

/* Redirect browser */
$gohere = "http://192.168.169.15/myarm.html";
header("Location: $gohere");
/* Make sure that code below does not get executed when we redirect. */
exit;

?>
;

public by emsity  3150  3  6  0

Python script to drive robot arm using MySql Database

Reads MySql database (db=action, table=arm) to drive arm using pre-determined codes.
# import the USB and Time libraries into Python 
import usb.core, usb.util, time, MySQLdb

#Obtain sys from PHP 
#import sys

#Open connection to the action database (action/ arm table)
db = MySQLdb.connect("localhost", "your_name", "your_password", "action")
db.autocommit(True)
cursor = db.cursor()


#Allocate the name 'RoboArm' to the USB device
RoboArm=usb.core.find(idVendor=0x1267,idProduct=0x0000)

#Check if the arm is detected and warn if not found
if RoboArm is None:
	raise ValueError("Arm not found")


#Define a procedure to execute each movement
def MoveArm(Duration,ArmCmd):
	#Start the movement
	RoboArm.ctrl_transfer(0x40,6,0x100,0,ArmCmd,1000)
	#Stop the movement after waiting specified duration
	time.sleep(Duration)
	ArmCmd=[0,0,0]
	RoboArm.ctrl_transfer(0x40,6,0x100,0,ArmCmd,1000)

#get the data from arm table 
cursor.execute ("SELECT * FROM arm where Status='0' ")
print cursor.rowcount

data = cursor.fetchall()
for row in data:
     ID = row[0]
     Duration = row[1]
     Direction = row[2]
     Status = row[3]
     print ID, Duration, Direction, Status
     Code = Direction
     
#Code is obtained from the 'Direction' field in the MySql arm table
#Duration is obtained from the 'Duration' field in the arm table

     if Code==1:
	ArmCmd=[2,0,0] 
	print "grip open"
        MoveArm(Duration,ArmCmd)
        cursor.execute ("UPDATE arm SET status='1' ")
        print ID, Duration, Direction, Status

		
     elif Code==2:
	ArmCmd=[1,0,0]
	print "grip close"
        MoveArm(Duration,ArmCmd)
        cursor.execute ("UPDATE arm SET status='1' ")
        print ID, Duration, Direction, Status


     elif Code==3:
	ArmCmd=[0,0,1]
	print "light on"
        MoveArm(Duration,ArmCmd)
        cursor.execute ("UPDATE arm SET status='1' ")
        print ID, Duration, Direction, Status
 

     elif Code==4:
	ArmCmd=[0,0,0]
	print "light off"
        MoveArm(Duration,ArmCmd)
        cursor.execute ("UPDATE arm SET status='1' ")
        print ID, Duration, Direction, Status


     elif Code==5:
	ArmCmd=[0,1,0]
	print "clockwise"
        MoveArm(Duration,ArmCmd)
        cursor.execute ("UPDATE arm SET status='1' ")
        print ID, Duration, Direction, Status

     elif Code==6:
	ArmCmd=[0,2,0]
	print "anti clockwise"
        MoveArm(Duration,ArmCmd)
        cursor.execute ("UPDATE arm SET status='1' ")
        print ID, Duration, Direction, Status

     elif Code==7:
	ArmCmd=[128,0,0]
	print "shoulder up"
        MoveArm(Duration,ArmCmd)
        cursor.execute ("UPDATE arm SET status='1' ")
        print ID, Duration, Direction, Status

     elif Code==8:
	ArmCmd=[64,0,0]
	print "shoulder down"
        MoveArm(Duration,ArmCmd)
        cursor.execute ("UPDATE arm SET status='1' ")
        print ID, Duration, Direction, Status

     elif Code==9:
	ArmCmd=[16,0,0]
	print "elbow up"
        MoveArm(Duration,ArmCmd)
        cursor.execute ("UPDATE arm SET Status='1' ")
        print ID, Duration, Direction, Status


     elif Code==10:
	ArmCmd=[32,0,0]
	print "elbow down"
        MoveArm(Duration,ArmCmd)
        cursor.execute ("UPDATE arm SET Status='1' ")
        print ID, Duration, Direction, Status


     elif Code==11:
	ArmCmd=[4,0,0]
	print "wrist up"
        MoveArm(Duration,ArmCmd)
        cursor.execute ("UPDATE arm SET status='1' ")
        print ID, Duration, Direction, Status

     elif Code==12:
	ArmCmd=[8,0,0]
	print "wrist down"
        MoveArm(Duration,ArmCmd)
        cursor.execute ("UPDATE arm SET status='1' ")
        print ID, Duration, Direction, Status
	
else:
 Duration=1	
 ArmCmd=[0,0,0]
 print "exit" # Exit   
 MoveArm(Duration,ArmCmd)

exit


;

public by emsity  262973  2  6  0

PHP script to call Python robot arm command

HTML call PHP to execute python command
<?php
exec( " sudo python /home/pi/arm/baseanti.py &"  );

/* Redirect browser */
$gohere = "http://192.168.169.15/index3G.html";
header("Location: $gohere");
/* Make sure that code below does not get executed when we redirect. */
exit;
?>
;

public by emsity  2024  1  5  0

Python script takes data from the PHP script to drive the robot arm

Uses 'import sys' to receive the arguments from the PHP of action code (what to do) and duration (how long for). www.emsity.com
# import the USB and Time libraries into Python 
import usb.core, usb.util, time

#Obtain sys from PHP 
import sys

#Allocate the name 'RoboArm' to the USB device
RoboArm=usb.core.find(idVendor=0x1267,idProduct=0x0000)

#Check if the arm is detected and warn if not found
if RoboArm is None:
	raise ValueError("Arm not found")


#Define a procedure to execute each movement
def MoveArm(Duration,ArmCmd):
	#Start the movement
	RoboArm.ctrl_transfer(0x40,6,0x100,0,ArmCmd,1000)
	#Stop the movement after waiting specified duration
	time.sleep(Duration)
	ArmCmd=[0,0,0]
	RoboArm.ctrl_transfer(0x40,6,0x100,0,ArmCmd,1000)


Code = 0

#Create a variable for duration from PHP

while Code < 9:

#Create a variable for duration
	xString = sys.argv[1]
	Duration = int(xString)

#Create a variable for command code
	yString = sys.argv[2]
	Code = int(yString)

#Move arm anti if code is 1 

	if Code==1:
		ArmCmd=[2,0,0] 
		print "grip open"
		
	elif Code==2:
		ArmCmd=[1,0,0]
		print "grip close"

	elif Code==3:
		ArmCmd=[0,0,1]
		print "light on"

	elif Code==4:
		ArmCmd=[0,0,0]
		print "light off"

	elif Code==5:
		ArmCmd=[0,1,0]
		print "clockwise"

	elif Code==6:
		ArmCmd=[0,2,0]
		print "anti clockwise"

	elif Code==7:
		ArmCmd=[128,0,0]
		print "shoulder up"

	elif Code==8:
		ArmCmd=[64,0,0]
		print "shoulder down"

	elif Code==9:
		ArmCmd=[16,0,0]
		print "elbow up"

	elif Code==10:
		ArmCmd=[32,0,0]
		print "elbow down"

	elif Code==11:
		ArmCmd=[4,0,0]
		print "wrist up"

	elif Code==12:
		ArmCmd=[8,0,0]
		print "wrist down"
	else:
		ArmCmd=[0,0,0]
		print "exit" # Exit
	

	MoveArm(Duration,ArmCmd)
	exit(3)

#MoveArm(1,[0,1,0]) # Rotate Base Anti-clockwise
#MoveArm(1,[0,2,0]) # Rotate Base Clockwise
#MoveArm(1,[64,0,0]) # Shoulder Up
#MoveArm(1,[128,0,0]) # Shoulder Down
#MoveArm(1,[16,0,0]) # Elbow Up
#MoveArm(1,[32,0,0]) # Elbow Down
#MoveArm(1,[4,0,0]) #  Wrist Up
#MoveArm(1,[8,0,0]) # Wrist Down
#MoveArm(1,[2,0,0]) # Grip Open
#MoveArm(1,[1,0,0]) # Grip Close
#MoveArm(1,[0,0,1]) # Light On
#MoveArm(1,[0,0,0]) # Light Off

;