Click here to Skip to main content
15,891,907 members
Articles / Programming Languages / C#

FuzzScript - A Fuzzy Logic Control Language

Rate me:
Please Sign up or sign in to vote.
4.97/5 (32 votes)
16 Dec 2009CPOL9 min read 63.3K   2.6K   73  
This article presents a Fuzzy Logic scripting language, FuzzScript, which can be used to include fuzzy controllers in C# applications. One interesting aspect is the possibility to generate an optimized version (hard-coded) of the controller under examination at run time.
//---------------------------------------------------------------------------------------
//	Fuzzy Robot Controller
//
//	Author:	cg
//	Date:	April 2009
//	Desc:	Fuzzy Robot Controller using FuzzScript version 1.0
//			Robot has 2 ultrasonic sensors.
//---------------------------------------------------------------------------------------

KnowledgeBase
{
	InputVariable SENSORLEFT [0, 2000]
	{
		LShoulder (vnear, 20, 40)
		Triangular (near, 10, 55, 100)
		Triangular (medium, 60, 100, 140)
		Triangular (far, 120, 150, 180)
		RShoulder (vfar, 160, 170)
	}

	InputVariable SENSORMIDDLE [0, 2000]
	{
		LShoulder (vnear, 20, 40)
		Triangular (near, 10, 55, 100)
		Triangular (medium, 60, 100, 140)
		Triangular (far, 120, 150, 180)
		RShoulder (vfar, 160, 170)
	}

	InputVariable SENSORRIGHT [0, 2000]
	{
		LShoulder (vnear, 20, 40)
		Triangular (near, 10, 55, 100)
		Triangular (medium, 60, 100, 140)
		Triangular (far, 120, 150, 180)
		RShoulder (vfar, 160, 170)
	}

	OutputVariable VELOCITYANGULAR [-0.4, 0.4]
	{
		LShoulder ( Bignegative, -0.3, -0.2)
		Triangular ( negative, -0.3, -0.2, -0.1)
		Triangular ( Smallnegative, -0.2, -0.1, 0)
		Triangular ( NoChange, -0.1, 0, 0.1)
		Triangular ( Smallpositive, 0, 0.1, 0.2)
		Triangular ( positive, 0.1, 0.2, 0.3)
		RShoulder ( Bigpositive, 0.2, 0.3)
	}


	OutputVariable VELOCITYLINEAR [-0.1, 5]
	{
		LShoulder (Reverse, -1, 0)
		Triangular (Stop, -0.1, 0, 0.1)
		Triangular (Slow, 0, 0.3, 0.6)
		Triangular (Medium, 0.3, 0.6, 0.9)
		RShoulder (Fast, 0.6, 5)
	}
}

Predicates
{
	Predicate far
	{
		(SENSORLEFT is vfar  and SENSORRIGHT is vfar) or 
		(SENSORLEFT is vfar  and SENSORRIGHT is far) or
		(SENSORLEFT is vfar  and SENSORRIGHT is medium) or

		(SENSORLEFT is far  and SENSORRIGHT is vfar) or
		(SENSORLEFT is far  and SENSORRIGHT is far) or
		(SENSORLEFT is far  and sensorright is medium)or
		
		(SENSORLEFT is medium  and sensorright is vfar) or
		(SENSORLEFT is medium  and sensorright is far) or
		(SENSORLEFT is medium  and sensorright is medium)
	}
	
	Predicate rightnear
	{
		(SENSORLEFT is vfar  and SENSORRIGHT is near) or
		(SENSORLEFT is far  and sensorright is near) or
		(SENSORLEFT is medium  and sensorright is near)
	}

	Predicate leftnear
	{
		(SENSORLEFT is near  and sensorright is vfar) or
		(SENSORLEFT is near  and sensorright is far) or
		(SENSORLEFT is near  and sensorright is medium)
	}
	
	Predicate rightdanger
	{
		(SENSORLEFT is vfar  and SENSORRIGHT is vnear) or
		(SENSORLEFT is far  and sensorright is vnear) or
		(SENSORLEFT is medium  and sensorright is vnear) or
		(SENSORLEFT is near  and sensorright is vnear)
	}
	
	Predicate leftdanger
	{
		(SENSORLEFT is vnear  and sensorright is vfar) or
		(SENSORLEFT is vnear  and sensorright is far ) or
		(SENSORLEFT is vnear  and sensorright is medium ) or
		(SENSORLEFT is vnear  and sensorright is near )
	}
	
	Predicate danger
	{
		SENSORLEFT is vnear  or sensorright is vnear or
		SENSORMIDDLE is near or
		SENSORMIDDLE is vnear
	}
	
	Predicate caution
	{
		(SENSORLEFT is near  and sensorright is near) and
		(SENSORMIDDLE is vfar or
		SENSORMIDDLE is far or
		SENSORMIDDLE is medium) 
	}
}

RuleBase 
{
	If very <far> Then VELOCITYANGULAR = 0 and VELOCITYLINEAR = 4.9
	If <rightnear> Then VELOCITYANGULAR is smallnegative and VELOCITYLINEAR is medium
	If <leftnear> Then VELOCITYANGULAR is smallpositive and VELOCITYLINEAR is medium
	If <rightdanger> Then VELOCITYANGULAR is negative and VELOCITYLINEAR is stop 
	If <leftdanger> Then VELOCITYANGULAR is positive and VELOCITYLINEAR is stop 
	If <danger> Then VELOCITYANGULAR = 0 and VELOCITYLINEAR = 0 
	If <caution> Then VELOCITYANGULAR is nochange and VELOCITYLINEAR is  medium
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader Crimsonwing (Malta) Ltd
Malta Malta
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions