hello everyone
I tried to build line follower robot with my fez pandaII but I confused is there anyone who can help me
I implemented P control but it can not work at the high speed
here is my code
using System;
using System.Threading;
using GHIElectronics.NETMF.Hardware;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using GHIElectronics.NETMF.FEZ;
namespace FEZ_Panda_II_Application1
{
public class Program
{
static InputPort sensor1 = new InputPort((Cpu.Pin)FEZ_Pin.Digital.Di35, false, Port.ResistorMode.PullUp);
static InputPort sensor2 = new InputPort((Cpu.Pin)FEZ_Pin.Digital.Di36, false, Port.ResistorMode.PullUp);
static InputPort sensor3 = new InputPort((Cpu.Pin)FEZ_Pin.Digital.Di38, false, Port.ResistorMode.PullUp);
static InputPort sensor4 = new InputPort((Cpu.Pin)FEZ_Pin.Digital.Di40, false, Port.ResistorMode.PullUp);
static InputPort sensor5 = new InputPort((Cpu.Pin)FEZ_Pin.Digital.Di42, false, Port.ResistorMode.PullUp);
static InputPort sensor6 = new InputPort((Cpu.Pin)FEZ_Pin.Digital.Di44, false, Port.ResistorMode.PullUp);
static InputPort sensor7 = new InputPort((Cpu.Pin)FEZ_Pin.Digital.Di46, false, Port.ResistorMode.PullUp);
static InputPort sensor8 = new InputPort((Cpu.Pin)FEZ_Pin.Digital.Di48, false, Port.ResistorMode.PullUp);
static OutputPort led = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.Di52, true);
static PWM leftpwm = new PWM((PWM.Pin)FEZ_Pin.PWM.Di8);
static PWM rightpwm = new PWM((PWM.Pin)FEZ_Pin.PWM.Di10);
public static int convert(bool value)
{
if (value == true)
return 1;
else
return 0;
}
public static int read_sensor()
{
int sensor1_state = convert(sensor1.Read());
int sensor2_state = convert(sensor2.Read());
int sensor3_state = convert(sensor3.Read());
int sensor4_state = convert(sensor4.Read());
int sensor5_state = convert(sensor5.Read());
int sensor6_state = convert(sensor6.Read());
int sensor7_state = convert(sensor7.Read());
int sensor8_state = convert(sensor8.Read());
int error = sensor1_state * (-8) + sensor2_state * (-6) + sensor3_state * (-4) + sensor4_state * (-2)
+ sensor5_state * 2 + sensor6_state * 4 + sensor7_state * 6 + sensor8_state * 8;
return 0;
}
public static void motor(int error)
{
if (error > 100) error = 100;
if (error < -100) error = -100;
if (error < 0)
{
leftpwm.Set(10000, (byte)(100));
rightpwm.Set(10000, (byte)(100 + error));
Debug.Print("right:" + (100 + error).ToString() + " left:100");
}
else
{
rightpwm.Set(10000, (byte)(100));
leftpwm.Set(10000, (byte)(100 - error));
Debug.Print("right:100 left:" + (100 - error).ToString());
}
}
public static void Main()
{
led.Write(true);
int Kp = 70;
int ki = 0;
int kd = 0;
int last_error = 0;
int error;
//int proportional = 0;
int integral = 0;
int derivate = 0;
int P;
int I;
int D;
int PID;
while (true)
{
error = read_sensor();
P = error * Kp/10;
integral += error;
I = integral * ki/10;
derivate = error - last_error;
D = derivate * kd/10;
last_error = error;
PID = P + I + D;
motor(PID);
}
}
}
}