i managed to do simple linefollower robot in python! however, its not working in c, due to the light sensor values. see the pic i uploaded in first post! If you have time, please comment on it! Thank you for your time!
G
Codes in C
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include "tick.h"
#include "BrickPi.h"
#include <linux/i2c-dev.h>
#include <wiringPi.h>
#include <fcntl.h>
int result;
int Left;
int Right;
int LLight;
int RLight;
#undef DEBUG
void fwd(void)
{
BrickPi.MotorSpeed[Left] = -50;
BrickPi.MotorSpeed[Right] = -50;
}
int main()
{
ClearTick();
result = BrickPiSetup();
if (result)
return 0;
BrickPi.Address[0] = 1;
BrickPi.Address[1] = 2;
Left = PORT_D;
Right = PORT_A;
LLight = PORT_1;
RLight = PORT_4;
BrickPi.SensorType[LLight] = TYPE_SENSOR_LIGHT_ON;
BrickPi.SensorType[RLight] = TYPE_SENSOR_LIGHT_ON;
BrickPi.MotorEnable[Left] = 1;
BrickPi.MotorEnable[Right] = 1;
result = BrickPiSetupSensors();
if(result)
return 0;
while(1)
{
if(!result)
{
printf("LS VALUE: %d %dn",BrickPi.Sensor[LLight],BrickPi.Sensor[RLight]);
fwd();
if (BrickPi.Sensor[LLight] < 650)
{
BrickPi.MotorSpeed[Right] = -75;
BrickPi.MotorSpeed[Left] = 0;
}
if (BrickPi.Sensor[RLight] < 650)
{
BrickPi.MotorSpeed[Right] = 0;
BrickPi.MotorSpeed[Left] = -75;
}
BrickPiUpdateValues();
usleep(10000); // delay for 10ms
}
}
}
Codes in Python
from BrickPi import *
BrickPiSetup()
BrickPi.SensorType[PORT_1] = TYPE_SENSOR_LIGHT_ON
BrickPi.SensorType[PORT_4] = TYPE_SENSOR_LIGHT_ON
BrickPi.MotorEnable[PORT_A] = 1
BrickPi.MotorEnable[PORT_D] = 1
BrickPiSetupSensors()
left = PORT_D
right = PORT_A
LLight = PORT_1
RLight = PORT_4
while True:
result = BrickPiUpdateValues()
if not result :
print BrickPi.Sensor[LLight],BrickPi.Sensor[RLight] #left right
BrickPi.MotorSpeed[right] = -50
BrickPi.MotorSpeed[left] = -50
if BrickPi.Sensor[LLight] < 650: #goes near to white tape
BrickPi.MotorSpeed[right] = -75
BrickPi.MotorSpeed[left] = 0
if BrickPi.Sensor[RLight] < 650: #goes near to white tape
BrickPi.MotorSpeed[right] = 0
BrickPi.MotorSpeed[left] = -75
time.sleep(.01) # sleep for 10 ms
Hey GBABY, from the picture, are you getting odd values or something? I take it that each time you get a ~1020 value, that’s an error. Is that right?
If that’s the error you’re getting, I would add a filter in around where you read the analog values. IE, I would filter out readings that are great than 1000, just reject them and take the reading again.
i followed my python linefollower and change it to C. The sensor value was fine now, however, the robot was not following the line.
I didnt add the filter in as this time round theres no more 1020 value. The picture shows the sensor values when robot is at stationary.
There shouldnt be such a big range, am i right? is there somewhere i did wrongly in my c code?
Thank you John for always helping me with my problem!
GBABY
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include "tick.h"
#include "BrickPi.h"
#include <linux/i2c-dev.h>
#include <fcntl.h>
#undef DEBUG
int result;
int leftm, rightm, llight, rlight;
void fwd(void)
{
BrickPi.MotorSpeed[rightm] = 70;
BrickPi.MotorSpeed[leftm] = 70;
}
void left(void)
{
BrickPi.MotorSpeed[rightm] = -200;
BrickPi.MotorSpeed[leftm] = 200;
}
void right(void)
{
BrickPi.MotorSpeed[rightm] = 200;
BrickPi.MotorSpeed[leftm] = -200;
}
int main()
{
ClearTick(); // in tick.h
result = BrickPiSetup(); // setup the serial port for communication
// printf("BrickPiSetup: %d\n", result); // Put this if you want to check setup.
if (result)
return 0; // check error
BrickPi.Address[0] = 1; // Communication addresses
BrickPi.Address[1] = 2;
llight = PORT_3;
rlight = PORT_1;
BrickPi.SensorType[llight] = TYPE_SENSOR_LIGHT_ON; // right light sensor
BrickPi.SensorType[rlight] = TYPE_SENSOR_LIGHT_ON; // left light sensor
leftm = PORT_D; // left
rightm = PORT_A; // right
BrickPi.MotorEnable[leftm] = 1; // Note that 1 is Enable, 0 is Disable.
BrickPi.MotorEnable[rightm] = 1; // Note that 1 is Enable, 0 is Disable.
result = BrickPiSetupSensors(); // Send the properties of sensors to BrickPi
// printf("BrickPiSetupSensors: %d\n",result); // Check if BrickPiSetupSensors work.
if (!result)
{
while(1)
{
printf("%d %d\n",BrickPi.Sensor[llight],BrickPi.Sensor[rlight]);
fwd();
if (BrickPi.Sensor[llight] < 580)
right();
if (BrickPi.Sensor[rlight] < 580)
left();
BrickPiUpdateValues(); // Ask BrickPi to update values result
usleep(10000); // Delay for 10ms
}
}
return 0;
}